文本框值未清除

发布于 2024-12-03 16:15:01 字数 2325 浏览 0 评论 0原文

在下面的代码中,单击按钮后,文本框值不会被清除。

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 标记之外。我需要在不回发页面的情况下完成该活动。

如果文本框位于内部,由于计时器触发了 20 飞秒,文本框会过于闪烁且不允许编辑。简单来说TextBox就是让人耳目一新,甚至不允许添加一点文字。

我不想将其扩展到 JavaScript 和其他内容以清除文本框中的值。谁能告诉我如何使用 C# 清除文本框值。

In the below code TextBox value is not getting cleared after clicking on Button.

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# code for button click.

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 = "";
}

I tried adding a line as above TextBox1.Text = ""; to clear text but no use.
Please do not ask me to keep Button outside the updatepanel tag. I need the event to be done without posting back the page.

If TextBox is inside Due to timer trigger for ever 20 fsecs the textbox is too blinking and not allowing to edit. To say simply TextBox is refreshing and not allowing even a single bit of text to add.

I don't want to extend this to JavaScripts and other to clear a value in textbox. Can anybody tell me how to clear the textbox value using C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空名 2024-12-10 16:15:01

使用另一个更新面板并在 Page_Load 函数中添加 UpdatePanedid.Update(); 希望这对您有帮助。

Use another update panel and in Page_Load function add UpdatePanedid.Update(); Hope this helps you.

国际总奸 2024-12-10 16:15:01

textbox1 需要位于 内,只有在单击按钮时 updatepanel 内的文本才会更新

the textbox1 needs to be inside the </ContentTemplate></asp:UpdatePanel> Only that within the updatepanel will get updated when you click the button

末骤雨初歇 2024-12-10 16:15:01

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

Move the Textbox inside the UpdatePanel. Your button only triggers the contents of UpdatePanel to be reloaded, not the ones outside of it.

始终不够 2024-12-10 16:15:01

您说您不想使用 Javascript 而想使用 C#,但是如果您可以用 C# 编写 Javascript 并在 UpdatePanel 的部分回发完成后在客户端上执行它会怎么样?

您可以将以下内容添加到 Button1_Click 方法中:

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

上面的代码片段在部分回发后在客户端上执行 Javascript,并将 TextBox1 的值设置为空字符串。

You say you don't want to use Javascript and you want to use C# but what if you could write the Javascript in C# and have it executed on the client after the partial postback from the UpdatePanel has been completed?

You could add the following to your Button1_Click method:

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

The above snippet executes Javascript on the client after the partial postback and sets the value of TextBox1 to an empty string.

橪书 2024-12-10 16:15:01

对我有用的是使用 chromes 并检查浏览器提供的 html 名称的组合框元素并使用以下代码:

ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(), "myScript", “document.getElementById('ctl00_m_g_8559e7aa_7fc2_4e23_b9b5_bf638f7e2699_ctl00_cboCustomers_TextBox').value = '';”, True)

ctl00_m_g_8559e7aa_7fc2_4e23_b9b5_bf638f7e2699_ctl00_cboCustomers_TextBox

是渲染期间给出的唯一名称,我很确定该名称不会更改

What worked for me was to use chromes and inspect the combobox element for the html name the browser gives it and using this code:

ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(), "myScript", "document.getElementById('ctl00_m_g_8559e7aa_7fc2_4e23_b9b5_bf638f7e2699_ctl00_cboCustomers_TextBox').value = '';", True)

ctl00_m_g_8559e7aa_7fc2_4e23_b9b5_bf638f7e2699_ctl00_cboCustomers_TextBox

is the unique name given during rendering, i'm pretty sure the name does not change

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文