Is it possible that Page_Unload event can reset textbox.text? I found that can’t!
Markup
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" Text="Do something1" />
<asp:Button ID="btn1" runat="server" Text="Do something2" />
<asp:Button ID="btn2" runat="server" Text="Do something3" />
</div>
</form>
</body>
</html>
Code behind
Protected Sub btn_Click(sender As Object, e As System.EventArgs) Handles btn.Click
Method1()
End Sub
Protected Sub btn1_Click(sender As Object, e As System.EventArgs) Handles btn1.Click
Method2()
End Sub
Protected Sub btn2_Click(sender As Object, e As System.EventArgs) Handles btn2.Click
Method3()
End Sub
Private Sub Method1()
'Do something
End Sub
Private Sub Method2()
'Do something
End Sub
Private Sub Method3()
'Do something
End Sub
Protected Sub Page_Unload(sender As Object, e As System.EventArgs) Handles Me.Unload
txt.Text = String.Empty
End Sub
You may think that putting the txt.Text = String.Empty in every method is the best way to solve this problem, but how about if I have 80 buttons, then I have to put the txt.Text = String.Empty in 80 button click event. Does there any better way to do this?
Look at the asp.net page life cycle.
The event that is called after all button clicks is PreRender.
You can override the OnPreRender to clear such text.