我設計一個網頁管理員從數據庫表中使用Get按鈕進行搜索的用戶和GridView控件刪除用戶刪除在GridView控件不工作
這是我的信源編碼..
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username" EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" />
<asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" />
<asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" />
<asp:BoundField DataField="year" HeaderText="year" SortExpression="year" />
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CanteenConnectionString %>" SelectCommand="SELECT * FROM [Hosteller]" DeleteCommand="DELETE FROM Hosteller WHERE (username = @username)">
<DeleteParameters>
<asp:Parameter Name="username" />
</DeleteParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
protected void btnget_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string ConString = "Data Source=sheikha-pc\\sqlexpress;Initial Catalog=Canteen;Integrated Security=True";
SqlConnection con = new SqlConnection(ConString);
string qry = "select id,name,adm,mobno,branch,year,username,password,usertype from Hosteller where adm='" + txtsearch1.Text + "'";
SqlDataAdapter adpt = new SqlDataAdapter(qry, con);
adpt.Fill(dt);
if (dt.Rows.Count == 0)
{
GridView1.EmptyDataText = "No data found";
GridView1.DataBind();
}
else
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
這是我爲GET編寫的編碼按鈕
問題是當我clic k刪除它不起作用!請幫忙.........
您將需要共享您的代碼GridView1_RowDeleting() - 你是否重新綁定在這個處理程序? – CalC
@ Cal279 no theres nothing in ..實際上當我得到一個錯誤「DataSource和DataSourceID都在'GridView1'上定義。刪除一個定義。」我刪除了datasource = gridview1中定義的sqldatasource1 ...然後他們告訴我rowdeleting沒有定義...所以我試過但沒有工作,,請你幫我 – sheikha
你能告訴我們GridView1_RowDeleting事件嗎? – Sankar