单击动态创建的任何链接按钮时,如何动态查找所选Linkbutton的列索引。所有按钮都是动态创建的,行和列也是在gridview中动态创建的。当点击相关的Linkbutton时,我需要删除gridview的单个单元格。所以我需要找到列和行索引。这个屏幕截图给出了问题的概念。
此代码用于动态创建链接按钮,其中不修复列。当我点击gridview相关Cell的删除按钮时,我需要删除单个单元格。
protected void gv_TT_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[2].Visible = false;
e.Row.Cells[1].Attributes.Add("colspan", "2");
}
int index = e.Row.RowIndex;
int k = 0;
GVdata = (DataTable)ViewState["GVdata"];
GridViewTable = (DataTable)ViewState["GridViewTable"];
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GVdata.Rows.Count; i++)
{
// ADD LINK BUTTON IF row.text != Empty(Blank)
if (e.Row.Cells[i + 3].ToString() != " ")
{
if (e.Row.Cells[i + 3].ToString() != " ")
{
if (e.Row.Cells[i + 3].ToString() != string.Empty)
{
Label txt = new Label();
LinkButton lb1 = new LinkButton();
lb1.ID = k + "_" + (i + 3) + "Btn1";
lb1.Text = " Update ";
lb1.ForeColor = System.Drawing.Color.Blue;
LinkButton lb2 = new LinkButton();
lb2.ID = k + "_" + (i + 3) + "Btn2";
lb2.Text = " Delete ";
lb2.ForeColor = System.Drawing.Color.Red;
lb1.CausesValidation = false;
lb1.Click += new EventHandler(Update_Click);
lb2.CausesValidation = false;
lb2.Click += new EventHandler(Delete_Click);
txt.Text = GridViewTable.Rows[index][i + 3].ToString();
e.Row.Cells[i + 3].Controls.Add(txt);
e.Row.Cells[i + 3].Controls.Add(lb1);
e.Row.Cells[i + 3].Controls.Add(lb2);
}
}
}
}
k++;
}
}
Click here for Screen Shot image output of Sample Code enter image description here
答案 0 :(得分:0)
您可以执行此操作以从网格中删除行。
在GridView模板中:
<asp:Button ID="Button1" runat="server" Text="Delete" CommandName="delRow" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "itemID").ToString() %>' />
然后在RowCommand
后面的代码中
protected void gv_TT_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delRow")
{
//get the id from the button and covert it
int itemID = Convert.ToInt32(e.CommandArgument);
//cast the viewstate as a datatable
DataTable dt = ViewState["GridViewTable"] as DataTable;
//filter out the row with the itemID to be deleted with Linq
DataTable dtFiltered = dt.AsEnumerable().Where(row => row.Field<int>("itemID") != itemID).CopyToDataTable();
//set the viewstate with the filtered datatable
ViewState["GridViewTable"] = dtFiltered;
//rebind the grid
GridView1.DataSource = dtFiltered;
GridView1.DataBind();
}
}
然而,您的问题是动态控件。添加动态控件时,您必须在按钮上添加CommandName
和CommandArgument
,以使其像上面代码段中的按钮一样工作:
protected void gv_TT_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb1 = new LinkButton();
lb1.ID = "Btn1";
lb1.Text = "DeleteRow";
lb1.CommandName = "delRow";
//value cannot be assigned yet
//lb1.CommandArgument = "";
lb1.ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[1].Controls.Add(lb1);
}
}
只有一个问题。为了分配CommandArgument
,我们必须使用GridView中的数据,但RowCreated
中的数据尚未出现。所以我们必须在RowDataBound
事件中分配值:
protected void gv_TT_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
LinkButton lb1 = e.Row.FindControl("Btn1") as LinkButton;
lb1.CommandArgument = drv["itemID"].ToString();
}
}