我已經搜索了兩天了,無法爲我的問題找到解決方案。我正在使用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();
}
您是否啓用了viewstate?如果沒有viewstate,那麼GridView會重置爲在每個帖子後面清空,除非您再次使用相同的數據綁定它。 – user1429080