0

我已经搜索了两天,找不到解决我的问题的方法。我正在使用 Visual Studio 编写一个连接到数据库的 Web 部件。

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

2 回答 2

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"> 
于 2013-07-04T11:07:15.343 回答
0

您可以像在您的 aspx 文件中一样声明您的数据源,然后就完成了。如果您需要在单击按钮后绑定您的网格,那么您必须在某处(例如视图状态)存储一个标志(布尔值)以指示您的网格是否必须绑定。然后仅当该标志为真时才绑定网格。根本不需要使用会话。

例如:

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

于 2013-07-04T12:20:28.857 回答