Reputation: 4179
I want when I click button my textbox1 must change, but textbox3 not Why does not it work?
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = DateTime.Now.ToLongTimeString();
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToLongTimeString();
}
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</asp:UpdatePanel>
When I Click Button1 my TextBox3 changes Why?
Upvotes: 0
Views: 21
Reputation: 45058
Because when you click the button you submit a request to the server, which results in a round-trip, which results in Page_Load
being executed again. You can avoid this by detecting whether the request is part of the postback cycle using the IsPostBack
property:
if (IsPostBack) {
}
Or, as is in most cases, doing stuff when it's not a postback:
if (!IsPostBack) {
TextBox3.Text = DateTime.Now.ToLongTimeString();
}
Upvotes: 3