Page_unload重置文本框

时间:2012-08-03 04:06:48

标签: asp.net

Page_Unload事件是否可以重置textbox.text?我发现不行!

标记     

<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>

背后的代码

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

您可能认为在每种方法中放置txt.Text = String.Empty是解决此问题的最佳方法,但如果我有80个按钮,那么我必须将txt.Text = String.Empty置于80按钮中事件。有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

查看asp.net page life cycle

所有按钮点击后调用的事件是PreRender。

您可以覆盖OnPreRender以清除此类文本。

Page life cycle