【发布时间】:2016-08-30 10:56:04
【问题描述】:
当单击任何动态创建的链接按钮时,如何动态找到所选链接按钮的列索引。所有按钮都是动态创建的,并且行和列也是在 gridview 中动态创建的。单击相关的链接按钮时,我需要删除gridview的单个单元格。所以我需要找到列和行索引。这个屏幕截图给出了问题的想法。
此代码用于在列不固定的情况下动态创建链接按钮。当单击gridview的相关单元格的删除按钮时,我需要删除单个单元格。
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
【问题讨论】: