C# 在Gridview中插入到。刷新时进行插入..asp.net
我有一个customer Gridview,可以在footertemplate中插入一个新客户 当我按下“插入”按钮时,我重定向到同一页面,我可以在gridview中看到我的新客户 a注意到的是,当我直接在页面上刷新时,它会再次添加相同的客户。这对我来说真的很奇怪,因为在重定向之后,我的必填字段(如firstname lastname等)当然都是空的,但页面仍然从之前获取值并进行另一次插入 为什么会这样?我该如何解决这个问题 aspxC# 在Gridview中插入到。刷新时进行插入..asp.net,c#,asp.net,gridview,insert,refresh,C#,Asp.net,Gridview,Insert,Refresh,我有一个customer Gridview,可以在footertemplate中插入一个新客户 当我按下“插入”按钮时,我重定向到同一页面,我可以在gridview中看到我的新客户 a注意到的是,当我直接在页面上刷新时,它会再次添加相同的客户。这对我来说真的很奇怪,因为在重定向之后,我的必填字段(如firstname lastname等)当然都是空的,但页面仍然从之前获取值并进行另一次插入 为什么会这样?我该如何解决这个问题 aspx 创建一个方法来绑定网格,当您将数据插入网格时,不要只调用
创建一个方法来绑定网格,当您将数据插入网格时,不要只调用此方法来绑定网格…试试这个…您需要的是使用
private void BindGrid()
{
//Your Code For Fill the Grid...Possibly If You Use Database than Fired Query to fetch data and fill Grid...
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dr = e.Row.DataItem as DataRowView;
if (dr["CustID"].ToString() == "60" || dr["CustID"].ToString() == "62")
{
e.Row.Enabled = false; //OR dr.Enabled = false;
//DISABLED Controls only
//((TextBox)e.Row.FindControl("TextBox1")).Enabled = false;
}
}
BindGrid();
}
Response.Redirect(Request.Url.AbsolutePath);
在按钮底部使用它,然后单击事件代码
protected void lbInsert_Click(object sender, EventArgs e)
{
Event.InsertParameters["Gender"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
Event.InsertParameters["LastName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
Event.InsertParameters["FirstName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
Event.InsertParameters["Street"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtStreet")).Text;
Event.InsertParameters["HouseNr"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtHouseNr")).Text;
Event.InsertParameters["Zip"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtZip")).Text;
Event.InsertParameters["City"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
Event.InsertParameters["Phone"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtPhone")).Text;
Event.InsertParameters["Email"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtEmail")).Text;
Event.InsertParameters["Company"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
Event.InsertParameters["Active"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlActive")).SelectedValue;
Event.Insert();
Response.Redirect(Request.Url.AbsolutePath);
}
谢谢你的回复。我在哪里以及如何做到这一点?我真的是个新手。你能给我一些代码吗?我没有任何代码来填充codebehind中的Gridview。我能在不在codebehind中创建Gridview的情况下解决这个问题吗?
Response.Redirect(Request.Url.AbsolutePath);
protected void lbInsert_Click(object sender, EventArgs e)
{
Event.InsertParameters["Gender"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
Event.InsertParameters["LastName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
Event.InsertParameters["FirstName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
Event.InsertParameters["Street"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtStreet")).Text;
Event.InsertParameters["HouseNr"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtHouseNr")).Text;
Event.InsertParameters["Zip"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtZip")).Text;
Event.InsertParameters["City"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
Event.InsertParameters["Phone"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtPhone")).Text;
Event.InsertParameters["Email"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtEmail")).Text;
Event.InsertParameters["Company"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
Event.InsertParameters["Active"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlActive")).SelectedValue;
Event.Insert();
Response.Redirect(Request.Url.AbsolutePath);
}