通过 asp.net 中的保存按钮将数据绑定到 gridview 后,保存按钮单击事件会在刷新页面时触发
我正在使用 C# 和 ASP.NET 开发 Web 应用程序。我通过同一页面中的文本框将数据绑定到 gridview,并在“保存”按钮单击事件中编写了绑定方法。
现在,当我通过“save”button_click
事件将数据从文本框保存到 gridview 并刷新页面时,发现网格视图再次与重复行绑定,这真的很奇怪。我尝试在 firefox、chrome 和 IE 8 上加载页面...但结果是否定的...
任何人都可以让我知道为什么会发生并指导我解决它...
这是我的 C# 代码:
string con = ConfigurationSettings.AppSettings["ConnectionStrings"];
protected void Page_Load(对象发送者, EventArgs e) {
tbladdasset.Visible = false;
btnsaveasset.Enabled = false;
lblErrMsg.Text = "";
if (!IsPostBack)
{
bindassets("", "");
ViewState["sortOrder"] = "";
}
}
private void bindassets(string sortExp, string sortDir)
{
try
{
SqlConnection con1 = new SqlConnection(con);
con1.Open();
SqlCommand cmd = new SqlCommand("select Description,CONVERT(VARCHAR(10), RecievedDate, 101) as DateRecieved,cost,Modelno,Quantity from Asset", con1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con1.Close();
if (dt.Rows.Count > 0)
{
DataView dv = dt.DefaultView;
if (sortExp != string.Empty)
{
dv.Sort = string.Format("{0} {1}", sortExp, sortDir);
}
grdvAssets.DataSource = dv;
grdvAssets.DataBind();
}
else
{
lblErrMsg.Text = "No data found...";
}
btnsaveasset.Enabled = false;
tbladdasset.Visible = false;
}
catch
{
lblErrMsg.Text = "Failed to connect server...";
}
}
protected void btnaddnew_Click(object sender, EventArgs e)
{
tbladdasset.Visible = true;
btnsaveasset.Enabled = true;
lblErrMsg.Text = "";
txtdescription.Text = "";
txtdtrecieved.Text = "";
txtcost.Text = "";
txtmodelno.Text = "";
txtquantity.Text = "";
}
protected void btnsaveasset_Click(object sender, EventArgs e)
{
if (txtdescription.Text != "" && txtdtrecieved.Text != "" && txtcost.Text != "" && txtmodelno.Text != "" && txtquantity.Text != "")
{
try
{
string desc= txtdescription.Text;
DateTime dtrecd = Convert.ToDateTime(txtdtrecieved.Text);
string cost = txtcost.Text;
string modelno = txtmodelno.Text;
double quantity = Convert.ToDouble(txtquantity.Text);
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
string save = "Insert into Asset(Description,Recieveddate,cost,Modelno,Quantity)values(@desc,@date,@cost,@modelno,@quantity)";
SqlCommand cmd = new SqlCommand(save, sqlcon);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@desc", desc);
cmd.Parameters.Add("@date", dtrecd);
cmd.Parameters.Add("@cost", cost);
cmd.Parameters.Add("@modelno", modelno);
cmd.Parameters.Add("@quantity", quantity);
cmd.ExecuteNonQuery();
sqlcon.Close();
bindassets("", "");
btnsaveasset.Enabled = false;
txtdescription.Text = "";
txtdtrecieved.Text = "";
txtcost.Text = "";
txtmodelno.Text = "";
txtquantity.Text = "";
lblErrMsg.Text = "data inserted successfully..";
}
catch
{
lblErrMsg.Text = "Please enter valid data and try again...";
}
}
else
{
lblErrMsg.Text = "Please enter valid data and try again...";
}
}
protected void grdvAssets_Sorting(object sender, GridViewSortEventArgs e)
{
bindassets(e.SortExpression, sortOrder);
}
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}
任何人请帮助我......提前致谢..
I am working on a web application using C# and ASP.NET. I am binding the data to the gridview through the textboxes in the same page and I wrote the binding method in my "Save" button click event.
Now, it's really strange thing to find that the grid view is getting bound again with duplicate rows once I refresh the page up on saving the data to the gridview from textboxes through "save" button_click
event. I have tried loading the page on firefox, chrome and IE 8....but the result is negative....
Could anyone let me know why is it happening and guide me to resolve it....
This is my C# code:
string con = ConfigurationSettings.AppSettings["ConnectionStrings"];
protected void Page_Load(object sender, EventArgs e)
{
tbladdasset.Visible = false;
btnsaveasset.Enabled = false;
lblErrMsg.Text = "";
if (!IsPostBack)
{
bindassets("", "");
ViewState["sortOrder"] = "";
}
}
private void bindassets(string sortExp, string sortDir)
{
try
{
SqlConnection con1 = new SqlConnection(con);
con1.Open();
SqlCommand cmd = new SqlCommand("select Description,CONVERT(VARCHAR(10), RecievedDate, 101) as DateRecieved,cost,Modelno,Quantity from Asset", con1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con1.Close();
if (dt.Rows.Count > 0)
{
DataView dv = dt.DefaultView;
if (sortExp != string.Empty)
{
dv.Sort = string.Format("{0} {1}", sortExp, sortDir);
}
grdvAssets.DataSource = dv;
grdvAssets.DataBind();
}
else
{
lblErrMsg.Text = "No data found...";
}
btnsaveasset.Enabled = false;
tbladdasset.Visible = false;
}
catch
{
lblErrMsg.Text = "Failed to connect server...";
}
}
protected void btnaddnew_Click(object sender, EventArgs e)
{
tbladdasset.Visible = true;
btnsaveasset.Enabled = true;
lblErrMsg.Text = "";
txtdescription.Text = "";
txtdtrecieved.Text = "";
txtcost.Text = "";
txtmodelno.Text = "";
txtquantity.Text = "";
}
protected void btnsaveasset_Click(object sender, EventArgs e)
{
if (txtdescription.Text != "" && txtdtrecieved.Text != "" && txtcost.Text != "" && txtmodelno.Text != "" && txtquantity.Text != "")
{
try
{
string desc= txtdescription.Text;
DateTime dtrecd = Convert.ToDateTime(txtdtrecieved.Text);
string cost = txtcost.Text;
string modelno = txtmodelno.Text;
double quantity = Convert.ToDouble(txtquantity.Text);
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
string save = "Insert into Asset(Description,Recieveddate,cost,Modelno,Quantity)values(@desc,@date,@cost,@modelno,@quantity)";
SqlCommand cmd = new SqlCommand(save, sqlcon);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@desc", desc);
cmd.Parameters.Add("@date", dtrecd);
cmd.Parameters.Add("@cost", cost);
cmd.Parameters.Add("@modelno", modelno);
cmd.Parameters.Add("@quantity", quantity);
cmd.ExecuteNonQuery();
sqlcon.Close();
bindassets("", "");
btnsaveasset.Enabled = false;
txtdescription.Text = "";
txtdtrecieved.Text = "";
txtcost.Text = "";
txtmodelno.Text = "";
txtquantity.Text = "";
lblErrMsg.Text = "data inserted successfully..";
}
catch
{
lblErrMsg.Text = "Please enter valid data and try again...";
}
}
else
{
lblErrMsg.Text = "Please enter valid data and try again...";
}
}
protected void grdvAssets_Sorting(object sender, GridViewSortEventArgs e)
{
bindassets(e.SortExpression, sortOrder);
}
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}
Anyone please help me.....Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个常见问题。检查类似的SO问题:Webforms刷新问题,如何停止不需要的回发。
如果有几个词,网络浏览器刷新按钮只是将最后一个请求发送到服务器,在您的情况下,这是一个“保存”按钮单击事件,因此它会给出重复的行。使用Response.Redirect,这个这样最后一个请求只是导航到页面,因此刷新不会造成不良影响。
已编辑
我看到您添加了一些代码。这是为您提供的解决方法。将数据保存到数据库这一事实有很大帮助。页面加载事件的第一件事不需要检查页面是否IsPostBack,只需调用
bindassets("", "");
方法。至于保存按钮单击事件。无需调用
bindassets("", "");
它将在页面加载时调用。that's a common problem. check similar SO questions: Webforms Refresh problem, How to stop unwanted postback.
if a few words, web browser refresh button just sends the last request to the server in your case that's a
"Save" button click event
, thus it gives duplicate rows. use Response.Redirect, this way the last request will be just navigation to the page, so the refresh will not cause undesired effects.EDITED
I see you have added some code. here's a workaround for you. the fact that you are saving data to database helps a lot. first thing on page load event no need to check if page IsPostBack, just call the
bindassets("", "");
method.as for save button click event. no need to call the
bindassets("", "");
it will be called from page load.