Button_Click 没有回发

发布于 2024-12-03 10:03:05 字数 1761 浏览 0 评论 0原文

在我下面的代码中,当单击按钮时,文本框值将被添加到数据库中,并且在重新加载页面的 DataBinder 中也会显示相同的值。好吧,就像聊天脚本一样,我需要在没有回发的情况下完成整个过程。那么,如何在不重新加载页面的情况下将值从文本框传递到 DataBinder。即如何执行按钮单击以使其不回发。

protected void Page_Load(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);
            try
            {
                MyConnection.Open();
                OdbcCommand cmd = new OdbcCommand("Select message from table1", MyConnection);
                OdbcDataReader dr = cmd.ExecuteReader();
                ArrayList values = new ArrayList();
                while (dr.Read())
                {
                    if (!IsPostBack)
                    {
                    string ep = dr[0].ToString();
                    values.Add(new PositionData(ep));
                    Shout_Box.DataSource = values;
                    Shout_Box.DataBind();
                    }
                }
            }
            catch
            {
            }
    }
    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();
    }

In my below code when is button clicked the textbox value is being added to the database and the same is displayed in the DataBinder are reloading the page. Well, just like chat script I need whole process to be done without postback. So, how to pass the value from textbox to DataBinder without reloading the page. i.e. How to perform Button Click such that it do not postback.

protected void Page_Load(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);
            try
            {
                MyConnection.Open();
                OdbcCommand cmd = new OdbcCommand("Select message from table1", MyConnection);
                OdbcDataReader dr = cmd.ExecuteReader();
                ArrayList values = new ArrayList();
                while (dr.Read())
                {
                    if (!IsPostBack)
                    {
                    string ep = dr[0].ToString();
                    values.Add(new PositionData(ep));
                    Shout_Box.DataSource = values;
                    Shout_Box.DataBind();
                    }
                }
            }
            catch
            {
            }
    }
    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();
    }

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

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

发布评论

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

评论(2

最美的太阳 2024-12-10 10:03:05

一种快速而肮脏的方法是使用更新面板。对于您的最终用户来说,这似乎是在没有回发的情况下发生的,您只需要对页面进行微小的更改。

详细信息:UpdatePanel 控件简介 (MSDN)

A quick and dirty way would be to use an Update Panel. It would appear to your end-users to occur without a postback and you would only need to make minor changes to your page.

More information: Introduction to the UpdatePanel control (MSDN)

陌若浮生 2024-12-10 10:03:05

简而言之:JavaScript、JQuery、AJAX,

您必须使用其中之一才能成功;因为 ASP.NET 页面上的任何按钮如果在服务器上运行,都已经发送回发。

UpdatePanel是一个AJAX组件,你可以使用它,这样你的按钮就不会发送postBack(或者你不受影响)

Shortly: JavaScript, JQuery, AJAX

you have to use one of them, to succeed that; because any button on asp.net page if it runs on server, will already send postBack.

UpdatePanel is an AJAX component, you can use it, so that your button does not send postBack (or you are not effected)

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