gpt4 book ai didi

c# - GridView RowUpdating 在 PostBack 后返回旧值

转载 作者:太空宇宙 更新时间:2023-11-03 19:15:41 25 4
gpt4 key购买 nike

我的 GridView 有问题。当我尝试编辑我的 GridView 时,我只得到旧值作为返回。

这是 RowUpdating 事件:

protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox nVorname = (TextBox)row.FindControl("newVorname");
TextBox nNachname = (TextBox)row.FindControl("newNachname");
TextBox nTelnr = (TextBox)row.FindControl("newTelnr");
TextBox nEmail = (TextBox)row.FindControl("newEmail");
HiddenField tid = (HiddenField)row.FindControl("id");

grid.EditIndex = -1;

SqlConnection sqlConn = new SqlConnection("server=localhost;Integrated Security=true;database=Telefonbuch;");
sqlConn.Open();
SqlCommand cmd = new SqlCommand("update dasOertliche set vorname= @vorname, nachname=@nachname, telefonnr =@telnr, email =@email where id = @id", sqlConn);
cmd.Parameters.Add("@vorname", SqlDbType.VarChar);
cmd.Parameters["@vorname"].Value = nVorname;
cmd.Parameters.Add("@nachname", SqlDbType.VarChar);
cmd.Parameters["@nachname"].Value = nNachname.Text;
cmd.Parameters.Add("@email", SqlDbType.VarChar);
cmd.Parameters["@email"].Value = nEmail.Text;
cmd.Parameters.Add("@telnr", SqlDbType.VarChar);
cmd.Parameters["@telnr"].Value = nTelnr.Text;
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = tid.Value;

cmd.ExecuteNonQuery();
sqlConn.Close();
bind();
}

.aspx 中的 TemplateField:

 <Columns>
<asp:TemplateField HeaderText = "Vorname">
<ItemTemplate> <%#Eval ("vorname") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="newVorname" runat="server" Text='<%#Eval ("vorname") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</columns>

还有我的 GridView 代码:

<asp:GridView runat="server" ID="grid" BorderWidth="0px" CellPadding="10" 
CellSpacing="10" HorizontalAlign="Center" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" onrowcancelingedit="grid_RowCancelingEdit"
onrowediting="grid_RowEditing" onrowupdating="grid_RowUpdating"
AutoGenerateColumns="False">

就像我说的,它总是返回旧值。有什么建议吗?

我的页面_加载:

protected void Page_Load(object sender, EventArgs e)
{
bind();
if (!Page.IsPostBack)
{
bind();
}
}

我的绑定(bind)():

 public void bind()
{
SqlConnection sqlConn = new SqlConnection("server=localhost;Integrated Security=true;database=Telefonbuch;");
sqlConn.Open();
SqlDataAdapter sqlComm = new SqlDataAdapter("SELECT id, vorname AS 'Vorname', nachname AS 'Nachname', telefonnr, email AS 'E-Mail' FROM dasOertliche ORDER BY nachname ASC", sqlConn);
DataSet ds = new DataSet();
sqlComm.Fill(ds, "dasOertliche");
grid.DataSource = ds.Tables[0];
grid.DataBind();
sqlConn.Close();
}

最佳答案

protected void Page_Load(object sender, EventArgs e)
{
bind();
if (!Page.IsPostBack)
{
bind();
}
}

错了。

应该是:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();
}
}

关于c# - GridView RowUpdating 在 PostBack 后返回旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16918768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com