我已经搜索了两天,找不到解决我的问题的方法。我正在使用 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();
}