我的问题是,当我点击编辑时,文本框出现在所有列中,我写任何我想要更新,然后我点击更新按钮,行在数据库中更新,但在页面上它显示以前的值。请帮助
<Columns>
<asp:TemplateField HeaderText="Email ID">
<ItemTemplate>
<asp:Label ID="Label1" ReadOnly="true" runat="server" Text='<%#Eval("EmailID")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("EmailID")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("LastName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("LastName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("Password")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("Password")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CurrentLocation">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("CurrentLocation")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("CurrentLocation")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile No.">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#Eval("MobileNo")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%#Eval("MobileNo")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%#Eval("Address")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%#Eval("Address")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%#Eval("Gender")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%#Eval("Gender")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="true"/>
代码背后的代码 -
protected void GridView1_Edit(object sender,GridViewEditEventArgs e) {
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox ac = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
TextBox a = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox b = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
TextBox c = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
TextBox d = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
TextBox k = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
TextBox f = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
TextBox g = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8");
ru.EmailID1=ac.Text;
ru.FirstName1=a.Text;
ru.LastName1=b.Text;
ru.Password1=c.Text;
ru.Location=d.Text;
ru.MobileNo=k.Text;
ru.Address=f.Text;
ru.gender = g.Text;
答案 0 :(得分:0)
您可以为gridview中的每个TextBox将EnableViewState属性设置为True。这将确保在页面回发后看到您在编辑模式下输入的值。此外,您可以将Eval更改为Bind,因为Eval是只读的,而Bind是双向的。
答案 1 :(得分:0)
我认为您的问题是您没有更新DataSource
的{{1}},因此当您在网格上调用GridView
时,它正在使用其在非网格中的数据更新数据源,因此出现“旧”值。
相反,一旦您成功将更改保存到数据库,然后执行查询数据库的逻辑以构建数据源,然后重新绑定网格,如下所示:
DataBind()
答案 2 :(得分:0)
您可以使用Response.Redirect并在代码后插入它。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox ac = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
TextBox a = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox b = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
TextBox c = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
TextBox d = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
TextBox k = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
TextBox f = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
TextBox g = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8");
ru.EmailID1=ac.Text;
ru.FirstName1=a.Text;
ru.LastName1=b.Text;
ru.Password1=c.Text;
ru.Location=d.Text;
ru.MobileNo=k.Text;
ru.Address=f.Text;
ru.gender = g.Text;
Response.Redirect("currentpage.aspx" + "?urlparam1=" + value1 + "&urlparam2=" + value2);
答案 3 :(得分:0)
试一试:
protected void GridViewUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
var id = e.NewValues["Id"] + string.Empty;
var login = e.NewValues["Login"] + string.Empty;
var password = e.NewValues["Password"] + string.Empty;
Validate(false,login, password);
var dto = new UserDto();
dto.Id = Convert.ToInt32(id);
dto.Login = (login + string.Empty).Trim();
dto.Password = password;
_userBo.Save(dto);
GridViewUser.EditIndex = -1;
}
catch (Exception ex)
{
Core.Util.Util.ShowPopUpMsg(this, "Erro não esperado: " + ex.Message);
}
finally
{
FillGrid();
}
}