Button_Click 没有回发
在我下面的代码中,当单击按钮时,文本框值将被添加到数据库中,并且在重新加载页面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种快速而肮脏的方法是使用更新面板。对于您的最终用户来说,这似乎是在没有回发的情况下发生的,您只需要对页面进行微小的更改。
详细信息: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)
简而言之: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)