GridView中的pbm删除按钮 [英] pbm in gridview delete button

查看:75
本文介绍了GridView中的pbm删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridview上有一个要删除的按钮,我想单击该按钮,当前行将被删除.所以请帮忙...

i have a button on a gridview for delete, i want wen i click the button ,the current row is deleted .so plz help wat ll i do...

推荐答案

像这样设计您的asp
Design Your asp like this
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ConfirmBox in Gridview Row Delete</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;

}
</style>
<script type="text/javascript">
function ConfirmationBox(username) {

var result = confirm('Are you sure you want to delete '+username+' Details' );
if (result) {

return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvrecords" CssClass="Gridview"

DataKeyNames="UserId" AutoGenerateColumns="false"

HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White"

onrowdatabound="gvrecords_RowDataBound">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField HeaderText="Delete User Details">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete User</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>





然后在aspx.cs中编写此代码





then write this code in aspx.cs

SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
//connection open
con.Open();
//sql command to execute query from database
SqlCommand cmd = new SqlCommand("Select * from UserDetails",con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//Binding data to gridview
gvrecords.DataSource = ds;
gvrecords.DataBind();
con.Close();
}
protected void gvrecords_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//getting username from particular row
string username =Convert.ToString(DataBinder.Eval(e.Row.DataItem,"UserName"));
//identifying the control in gridview
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
//raising javascript confirmationbox whenver user clicks on link button
lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox(''" + username+ "'')");
}
}
protected void lnkdelete_Click(object sender, EventArgs e)
{

LinkButton lnkbtn= sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int userid = Convert.ToInt32(gvrecords.DataKeys[gvrow.RowIndex].Value.ToString());
string username = gvrow.Cells[0].Text;
con.Open();
SqlCommand cmd = new SqlCommand("delete from UserDetails where UserId="+userid,con);
int result=cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindUserDetails();
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(''" + username + " details deleted successfully'')", true);
}
}




获取Sql数据源的属性.然后在其中指定删除查询.然后转到您的gridview.在gridview任务中,选中启用删除".

使用此方法,您可以删除特定的行,而无需添加单独的删除按钮.
Hi,

Take the properties of the Sql data source. Then specify the delete query in that. Then goto your gridview. In the gridview tasks check enable deleting.

Using this method you can delete a specific row without adding a separate button for delete.


这篇关于GridView中的pbm删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆