C# 有了这个代码,我可以';无法更新任何更改
这是一个GridView更新,没有语法问题 更新C# 有了这个代码,我可以';无法更新任何更改,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,这是一个GridView更新,没有语法问题 更新((文本框)GridView1.Rows[e.RowIndex].FindControl(“txt”)).Text后它返回相同的文本无任何更改我无法在文本框中获取在更新模式下键入的文本 示例:如果我有text-someText protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int id = Convert.ToInt32((G
((文本框)GridView1.Rows[e.RowIndex].FindControl(“txt”)).Text后代码>它返回相同的文本无任何更改我无法在文本框中获取在更新模式下键入的文本
示例:如果我有text-someText
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
int id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label1ID") as Label).Text);
PersonData data = (from x in Domain.Instance.PersonDatas
where x.ID == id
select x).First();
data.Info = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt")).Text;
data.Info1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt1")).Text;
data.Info2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt2")).Text;
Domain.Instance.SaveChanges();
GridView1.EditIndex = -1;
DatBind();
}
在更新中,我键入了-newText
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
int id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label1ID") as Label).Text);
PersonData data = (from x in Domain.Instance.PersonDatas
where x.ID == id
select x).First();
data.Info = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt")).Text;
data.Info1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt1")).Text;
data.Info2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt2")).Text;
Domain.Instance.SaveChanges();
GridView1.EditIndex = -1;
DatBind();
}
最后返回-someText
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
int id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label1ID") as Label).Text);
PersonData data = (from x in Domain.Instance.PersonDatas
where x.ID == id
select x).First();
data.Info = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt")).Text;
data.Info1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt1")).Text;
data.Info2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt2")).Text;
Domain.Instance.SaveChanges();
GridView1.EditIndex = -1;
DatBind();
}
有一个属性:
<asp:TemplateField HeaderText="Info">
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%# Eval("Info") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelInfo" runat="server" Text='<%# Eval("Info") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
我认为问题可能出在gridview的绑定中。
您不应在页面加载时绑定gridview。
它应该如下所示
if(!IsPostBack) /// <<<<<<<<<<
{
GridView1.DataSource = yourDataSource;
GridView1.DataBind();
}
if(!IsPostBack)///这与页面加载无关。在RowUpdate事件中更新基础数据和editindex后,他正在重新绑定数据源。@jadarnel27,shekhar提到的是正确的,如果他在回发期间绑定数据,当您发出edit命令时,它将写入新值,顺便说一句,感谢我提到页面加载binding@jadarnel27如果您仅在页面加载时绑定gridview,它将为您提供旧值:)@Shekhar“如果只在页面加载上绑定gridview,它将为您提供旧值”-我知道=)OP只是没有提到任何关于页面加载函数的内容,也没有显示它。并且他们在RowUpdate事件中获取的新值不正确。@jadarnel27,OP所做的没有错,他的代码将返回在编辑模板的文本框中输入的新值。检查MSDN样品