【问题标题】:GridView RowUpdating returns old values after PostBackGridView RowUpdating 在 PostBack 之后返回旧值
【发布时间】:2013-06-04 13:11:12
【问题描述】:

我的 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();
    }
}

我的绑定():

 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();
    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:
    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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      在页面加载时,将您的绑定网格代码置于以下条件

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

      【讨论】:

      • 我已经更新了答案,请尝试这样删除绑定();它在 if (!Page.IsPostBack) 行上方。并且只保留 bind();这是if条件。
      【解决方案3】:

      最后您需要致电GridView1.DataBind()

      【讨论】:

      • 到底在哪里?我在 RowUpdating 的最后一部分执行此操作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多