2013-06-04 263 views
1

我的GridView出現問題。當我嘗試編輯我的GridView時,我只返回舊值。GridView RowUpdating在回傳後返回舊值

這裏是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, [email protected], telefonnr [email protected], email [email protected] 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:

<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"> 

就像我說的,它總是返回舊的價值觀。有什麼建議麼?

我的Page_Load:

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

回答

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

你需要調用GridView1.DataBind()到底。

+0

到底去哪裏?我是在RowUpdating的最後一部分做的。 –

1

在頁面加載把你綁定網格代碼以下條件

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

我把我的Page_Load放好了。還是行不通。 –

+1

我已更新答案,請嘗試像這樣刪除bind();它在if(!Page.IsPostBack)行的上方,並且只保留bind();這是在條件。 –