2013-07-04 125 views
0

我已經搜索了兩天了,無法爲我的問題找到解決方案。我正在使用Visual Studio編寫與數據庫連接的Web部件。當點擊編輯按鈕時,GridView消失

所以我做了一個GridView來顯示數據庫中列出的一些數據,用戶可以編輯和更新數據。在你看到GridView之前,你必須點擊按鈕「加載表」。但是當我點擊GridView上的「編輯」時,GridView消失並且只有當我再次單擊按鈕「加載表」時才顯示,並且GridView處於編輯模式。

ASP

<asp:Button ID="btnload" runat="server" Text="load table" 
      onclick="btnload_Click" /> 
<asp:GridView ID="gridtable" runat="server" BackColor="White" 
       BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
       CellPadding="4" EnableModelValidation="True" 
       OnRowCancelingEdit="gridtable_RowCancelingEdit" 
       OnRowEditing="gridtable_RowEditing" 
       OnRowUpdating="gridtable_RowUpdating" > 
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> 
    <RowStyle BackColor="White" ForeColor="#330099" /> 
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
    <Columns> 
    <asp:TemplateField HeaderText="P_number"> 
     <ItemTemplate> 
     <%#Eval("P_number")%> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox ID="textbox1" runat="server" Text='<%#Eval("P_number")%>'> 
     </asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="lastname"> 
     <ItemTemplate> 
     <%#Eval("lastname")%> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox ID="textbox2" runat="server" Text='<%#Eval("lastname")%>'> 
     </asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="PN_ch"> 
     <ItemTemplate> 
     <%#Eval("PN_ch")%> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox ID="textbox3" runat="server" Text='<%#Eval("PN_ch")%>'> 
     </asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="lastname_ch"> 
     <ItemTemplate> 
     <%#Eval("lastname_ch")%> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox ID="textbox4" runat="server" Text='<%#Eval("lastname_ch")%>'> 
     </asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="workplace"> 
     <ItemTemplate> 
     <%#Eval("workplace")%> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox ID="textbox5" runat="server" Text='<%#Eval("workplace")%>'> 
     </asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

C#

public SqlDataSource datasource; 

protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void btnload_Click(object sender, EventArgs e) 
{   

    openConnection(getconstring()); 
    gridtable.AutoGenerateColumns = false; 
    gridtable.AutoGenerateEditButton = true; 
    datasource = new SqlDataSource(getconstring(), "SELECT * FROM T_Employees"); 
    BindData(); 
} 

public void BindData() 
{ 

    try 
    { 
     gridtable.DataSource = datasource; 
     gridtable.DataBind(); 
    } 
    catch (Exception e) 
    { 
     //Do something 
    } 
} 

protected void gridtable_RowEditing(object sender, GridViewEditEventArgs e) 
{ 

    gridtable.EditIndex = e.NewEditIndex; 
    BindData(); 
} 

protected void gridtable_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 

    //get EditIndex 
    GridViewRow row = gridtable.Rows[e.RowIndex]; 

    // save changes 
    string pnumber = ((row.Cells[1].Controls[0]).ToString()); 
    string lastname = ((row.Cells[2].Controls[0]).ToString()); 
    string pn_ch = ((row.Cells[3].Controls[0]).ToString()); 
    string lastname_ch = ((row.Cells[4].Controls[0]).ToString()); 
    string workplace = ((row.Cells[5].Controls[0]).ToString()); 

    //Update 
    datasource.UpdateCommand = "UPDATE T_Employees SET P_number='"+pnumber+"', lastname='" 
           +lastname+"', PN_ch='"+pn_ch+"', lastname_ch='" 
           +lastname_ch+"', workplace='"+workplace+"'"; 
    datasource.Update(); 

    //reset EditIndex 
    gridtabelle.EditIndex = -1; 

    //Bind data 
    BindData(); 
} 

protected void gridtable_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
{ 

    gridtable.EditIndex = -1; 
    BindData(); 
} 
+0

您是否啓用了viewstate?如果沒有viewstate,那麼GridView會重置爲在每個帖子後面清空,除非您再次使用相同的數據綁定它。 – user1429080

回答

2

您需要將數據源存儲在會話中的按鈕點擊:

protected void btnload_Click(object sender, EventArgs e) 
    {   
      openConnection(getconstring()); 
      gridtable.AutoGenerateColumns = false; 
      gridtable.AutoGenerateEditButton = true; 
      datasource = new SqlDataSource(getconstring(), "SELECT * FROM T_Employees"); 
      Session["datasource "] = datasource 
      BindData(); 
    } 

您綁定然後改成這樣:

public void BindData() 
    { 
     try 
     { 
      gridtable.DataSource = Session["datasource "] ; 
      gridtable.DataBind(); 

     } 
     catch (Exception e) 
     { 
      //Do something 
     } 
    } 

您還需要將其添加到web.config中:

<system.web> 
     <pages enableSessionState="true"> 
</system.web> 

或者這個頁面:

<%@Page enableSessionState="true"> 
+0

我試過你的解決方案,但我得到一個錯誤,我必須啓用會話狀態。 Control Language =「C#」AutoEventWireup =「true」EnableSessionState =「true」似乎不起作用 – user2508738

+0

您可以使用viewstate而不是session來存儲數據 –

0

你可能只是聲明你的數據源,如您的aspx文件和你完成。如果你需要在點擊按鈕後綁定你的網格,那麼你必須存儲某個地方(例如viewstate)一個標誌(布爾值)來指示你的網格是否必須被綁定。然後只在該標誌爲真時綁定網格。 根本不需要使用會話。

如:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!isPostBack){ 
     ViewState["FLAG"] = false; 
    } 
    else{ 
     if ((bool)ViewState["FLAG"]) BindData(); 
    } 
} 

protected void btnload_Click(object sender, EventArgs e) 
{   
    ViewState["FLAG"] = true; 
} 

尋找@以前的答案,請考慮,即使你選擇的ViewState或Session(每一種都有自己的優勢/劣勢),你並不需要存儲的數據ridundant。在你的情況下,只需要一點就足夠了,因爲你有代碼 - 在你的頁面中 - 建立/綁定數據源而不是保存它。

相關問題