Gridview删除无法正常工作.. [英] Gridview Delete not working..
本文介绍了Gridview删除无法正常工作..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的sql表有3列。用户名,密码,用户类型。
在gridview im中显示具有gridview列的用户:Username,usertype。当我点击删除行时,应该从sql和gridview中删除。没有错误,请告诉我我的代码中的错误。
My sql table has 3 columns. Username,Password,Usertype.
In a gridview im showing the users with gridview columns: Username, usertype. When i click delete rows should be deleted from sql as well as gridview. There is no error, pls let me know whats the mistake in my code.
<asp:GridView ID="GridView1" runat="server" Width="40%" CellSpacing="2" AutoGenerateColumns="false" CellPadding="4" AllowPaging="true"
PageSize="10" ForeColor="#333333"
GridLines="Vertical" OnPageIndexChanging="GridView1_PageIndexChanging" DataKeyNames="Username"
OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="lbl2" runat="server" Text='<%#Bind("Username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Width="140px" Text='<%#Bind("Username")%>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="UserType">
<ItemTemplate>
<asp:Label ID="lbl3" runat="server" Text='<%#Bind("UserType") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Width="140px" Text='<%#Bind("UserType")%>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete User">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:Button ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField >
<EditItemTemplate>
<asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView
>
>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
Label Username = GridView1.Rows[e.RowIndex].FindControl("lbl2") as Label;
Label Usertype = GridView1.Rows[e.RowIndex].FindControl("lbl3") as Label;
string constr = WebConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("[Sp_InsertUserRegisteration]"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "DELETE");
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Usertype", Usertype);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
this.BindData();
}
推荐答案
protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
如果单击删除命令按钮,将触发上述事件。
但我在代码中找不到任何命令按钮。项目模板中只有一个删除按钮。
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting(v = VS .110).aspx [ ^ ]
请参阅以下链接:
http://aspsnippets.com/ Blue / Articles / ASPNet-GridView --- Delete-Row-with-Confirmation.aspx [ ^ ]
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
The above event will be triggered if Delete command button is clicked.
But I couldn't find any command button in your code. You only have a delete button inside Item Template.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting(v=vs.110).aspx[^]
Refer to the below link:
http://aspsnippets.com/Blue/Articles/ASPNet-GridView---Delete-Row-with-Confirmation.aspx[^]
这篇关于Gridview删除无法正常工作..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文