1

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

3 回答 3

4
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();
    }
}
于 2013-06-04T13:31:12.317 回答
1

你需要GridView1.DataBind()最后打电话。

于 2013-06-04T13:13:37.263 回答
1

在 Pageload 上,将您的绑定网格代码置于以下条件

 protected void Page_Load(object sender, EventArgs e)
        {
             if (!Page.IsPostBack)
               {
                  bind();
               }
        }
于 2013-06-04T13:18:04.153 回答