我是否知道如何使用GridView
从LinkButton
删除行?我在谷歌中找到的代码正在使用数据绑定GridView
。我根据用DropDownList
选择的信息绑定信息。感谢
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string username;
username = HttpContext.Current.User.Identity.Name;
if (DropDownList1.SelectedValue.Equals("Expired"))
{
SqlConnection conn4 = new SqlConnection(My connection);
SqlDataAdapter adapter;
string mySQL2;
mySQL2 =
"SELECT Title,MessageStatus From Table_Message WHERE Username ='"
+ username
+ "' AND MessageStatus = 'Expired' AND Method = 'Email'";
adapter = new SqlDataAdapter(mySQL2, conn4);
conn4.Open();
DataSet ds3 = new DataSet();
adapter.Fill(ds3);
//Execute the sql command
GridView1.DataSource = ds3;
GridView1.DataBind();
conn4.Close();
}
else if (DropDownList1.SelectedValue.Equals("Pending"))
{
SqlConnection conn3 = new SqlConnection(My connection);
SqlDataAdapter adapter1;
string mySQL;
mySQL =
"SELECT Title,MessageStatus From Table_Message WHERE Username ='"
+ username
+ "' AND MessageStatus = 'Pending' AND Method = 'Email'";
adapter1 = new SqlDataAdapter(mySQL, conn3);
conn3.Open();
DataSet ds2 = new DataSet();
adapter1.Fill(ds2);
//Execute the sql command
GridView1.DataSource = ds2;
GridView1.DataBind();
conn3.Close();
}
答案 0 :(得分:0)
如果您允许匿名用户访问该列表:
这可能是一个选择:
如果您只显示列表以授权用户,那么您可以使用ajax编写删除代码:
delete.aspx?Id='Id'
的javascript函数,删除那里的记录。答案 1 :(得分:0)
您可以执行以下步骤
1)将网格的DataKeyNames映射到表的主键
2)将LinkButton作为
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="/_layouts/images/DELETE.GIF"
AlternateText="Delete" CommandName="DeleteUser" CausesValidation="false" ToolTip="Delete"/>
</ItemTemplate>
3)在rowdatabound事件中绑定命令参数
protected void GvwUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
ImageButton imgBtnDelete;
if (e.Row.RowType == DataControlRowType.DataRow)
{
imgBtnDelete = (ImageButton)e.Row.FindControl("imgBtnDelete");
imgBtnDelete.CommandArgument = gvwUser.DataKeys[e.Row.RowIndex].Value.ToString();
}
}
4)在后面的代码中将实现写为
protected void GvwUser_RowCommand(object sender, GridViewCommandEventArgs e)
{
int userId = 0;
if (e.CommandName.Equals("DeleteUser"))
{
//get the user id
userId = Convert.ToInt32(e.CommandArgument.ToString());
//GetUser will delete the user
if (DeleteUser(userId) > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete", "alert('User Deleted.');", true);
}
}