1

我在后面的代码中有以下代码。目标是将 的值重置textbox为空白。但是,当我单击button时,没有任何反应。

protected void btnReset_Click(object sender, EventArgs e)
{
    Label srch_Title = (Label)FindControl("srch_Title");
    srch_Title.Text = String.Empty;
}

这是textbox来自主页的代码:

<asp:TextBox ID="srch_Title" AutoPostBack="true" runat="server" />

这是主页上的按钮代码:

<asp:Button ID="btnResetSearch" runat="server" OnClick="btnReset_Click" Height="35px" Text="Reset Search" Width="120px" />

我是一个新手/爱好者程序员,这是我的第一篇文章。猜测问题很明显,我只是没有看到它。

4

3 回答 3

0

您应该将控件定义为 TextBox 而不是 Label ,如下所示:

TextBox srch_Title = (TextBox )Form.FindControl("srch_Title");
srch_Title.Text = String.Empty;
于 2013-11-05T12:41:05.813 回答
0

您需要找到srch_Title控件作为 aTextBox而不是 a Label,如下所示:

// The as operator avoids a cast exception, 
// returns null if cast cannot be successfully performed
TextBox theSrchTitleTextBox = Form.FindControl("srch_Title") as TextBox;

// Verify that we found the text box before we try to use it,
// to avoid null reference exception
if(theSrchTitleTextBox != null)
{
    theSrchTitleTextBox.Text = String.Empty;
}
于 2013-11-05T12:45:35.983 回答
0

通过添加另一个 FindControl 来解决这个问题。第一个 FindControl 引用了 master 并使用它的结果来设置指向文本框的第二个 FindControl。当我问原始问题时,我没有发布足够的代码,因为我认为有经验的程序员会很快发现解决方案。感谢所有做出贡献的人。

于 2013-11-10T14:44:37.120 回答