单击编辑按钮时GridView消失

时间:2013-07-04 11:00:45

标签: c# asp.net gridview

我现在搜索了两天,找不到解决问题的方法。我正在使用Visual Studio编写与数据库连接的webpart。

所以我制作了一个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();
}

2 个答案:

答案 0 :(得分: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"> 

答案 1 :(得分: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(每个都有自己的优点/缺点),您也不需要存储有用的数据。在您的情况下,只需一个位就足够了,因为您在页面中拥有代码来构建/绑定数据源而无需将其存储在一起。