2011-09-08 50 views
2

在下面的代码中,单击按钮后,TextBox值没有被清除。文本框值未被清除

的Default.aspx的按钮点击

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <br /> 
    <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="20"> 
    </asp:Timer> 
    <br /> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <Triggers> 
       <asp:AsyncPostBackTrigger controlid="Timer1" eventname="Tick" /> 
      </Triggers> 
     <ContentTemplate> 
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    <asp:Repeater ID="Shout_Box" runat="server"> 
      <ItemTemplate> 
       <%# DataBinder.Eval(Container.DataItem, "Message") %> 
      </ItemTemplate> 
     </asp:Repeater> 

     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
     </ContentTemplate> 
    </asp:UpdatePanel> 
    <asp:TextBox ID="TextBox1" runat="server"> 
     </asp:TextBox> 
    </form> 
</body> 
</html> 

C#代码。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=repeater;" + "UID=root;" + "PASSWORD=********;" + "OPTION=3"; 
    OdbcConnection MyConnection = new OdbcConnection(MyConString); 
    OdbcCommand cmd = new OdbcCommand("INSERT INTO table1(message)VALUES(?)", MyConnection); 
    cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = TextBox1.Text; 
    MyConnection.Open(); 
    cmd.ExecuteNonQuery(); 
    MyConnection.Close(); 
    TextBox1.Text = ""; 
} 

我试着添加一行如上TextBox1.Text = "";来清除文本但没用。 请不要让我将Button保持在updatepanel标签之外。我需要完成该事件而不回发该页面。

如果TextBox在里面由于定时器触发了20个fsecs,文本框太闪烁,不允许编辑。简单地说,TextBox正在刷新,并且不允许添加一点文本。

我不希望将其扩展到JavaScript和其他文本框中清除值。任何人都可以告诉我如何使用C#清除文本框的值。

回答

3

使用另一个更新面板和在Page_Load功能添加UpdatePanedid.Update();希望这可以帮助你。

1

将文本框移到UpdatePanel中。您的按钮只会触发UpdatePanel的内容重新加载,而不是其外部的内容。

2

的TextBox1的需要是</ContentTemplate></asp:UpdatePanel>只有在UpdatePanel中,当您单击该按钮将得到更新内部

+0

如果TextBox在''由于定时器触发了20个fsecs,文本框也是如此闪烁并且不允许编辑。简单地说,TextBox正在刷新,并且不允许添加一点文本。 – Mal

+0

那么你需要找出另一种解决方案,是吗?第二个没有定时器的更新面板怎么样? –

1

你说你不想使用JavaScript和要使用C#,但如果你能在C#中写入JavaScript并在UpdatePanel部分回发完成后在客户端执行?

您可以添加以下到您的button1_Click方法:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myScript", "document.getElementById('" + TextBox1.ClientID + "').value = '';", true); 

上面的代码中的部分回发后在客户端上执行Javascript和设置TextBox1中的值设置为空字符串。

1

什么工作对我来说是使用铬合金和检查组合框元素的HTML命名浏览器给它,并使用此代码:

ScriptManager.RegisterStartupScript(Me.Page,Me.Page.GetType() “myScript的”, “的document.getElementById( 'ctl00_m_g_8559e7aa_7fc2_4e23_b9b5_bf638f7e2699_ctl00_cboCustomers_TextBox')。价值= '';”,真)

ctl00_m_g_8559e7aa_7fc2_4e23_b9b5_bf638f7e2699_ctl00_cboCustomers_TextBox

是在渲染过程中给出的唯一名称,我敢肯定名称不会改变