我设计一个网页管理员从数据库表中使用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