Reputation: 892
i have one dynamic gridview with two link button.if i click that link button event is not firing.but if i call "display" method in page load its working fine.code below
public void display()
{
GridView grdv = new GridView();
grdv.AutoGenerateColumns = false;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
BL.ESSBL bl = new BL.ESSBL();
ds = bl.GetContactList();//getting datatable
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
dt = ds.Tables[0];
grdv.RowDataBound += new GridViewRowEventHandler(grdv_RowDataBound);
grdv.DataSource = null;
grdv.DataBind();
grdv.Columns.Clear();
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField boundfield = new BoundField();
boundfield.DataField = dt.Columns[i].ColumnName.ToString();
boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
grdv.Columns.Add(boundfield);
}
TemplateField tmf = new TemplateField();
grdv.Columns.Add(tmf);
tmf = new TemplateField();
grdv.Columns.Add(tmf);
grdv.DataSource = dt;
grdv.DataBind();
pnlupdate.Controls.Add(grdv);
}
}
void grdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int count= e.Row.Cells.Count;
LinkButton lnkupdate= new LinkButton();
lnkupdate.ID = "Update";
lnkupdate.Text = "Update";
LinkButton lnkdelete = new LinkButton();
lnkdelete.ID = "delete";
lnkdelete.Text = "delete";
lnkdelete.Click += new EventHandler(lnkdelete_Click);
lnkupdate.CommandArgument = (e.Row.DataItem as DataRowView).Row[0].ToString();
lnkdelete.CommandArgument = (e.Row.DataItem as DataRowView).Row[0].ToString();
lnkupdate.Click += new EventHandler(lnkupdate_Click);
e.Row.Cells[count-2].Controls.Add(lnkupdate);
e.Row.Cells[count-1].Controls.Add(lnkdelete);
}
}
protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
{
dynamicgridview(); // not working
Clear();
}
void lnkupdate_Click(object sender, EventArgs e)
{
Response.Write(@"<script language=""javascript"">alert(""update details "");</script>");
}
if i put "display" method in page load it will call every postback.i don't want that .i want to call this method in drop down selection changed event. if i put "display" method inside that link click event is not firing. so what i have to do to overcome this.
Upvotes: 0
Views: 2347
Reputation: 1911
You have to create gridview before Page_Load, if you don't bind grid after postback. GridView needs to load ViewState.
private GridView gv;
protected void Page_Init(object sender, EventArgs e)
{
gv = new GridView();
gv.ID = "gv";
gv.AutoGenerateColumns = false;
gv.Columns.Add(new TemplateField());
gv.RowCreated += gv_RowCreated;
gv.RowDataBound += gv_RowDataBound;
pnl.Controls.Add(gv);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gv.DataSource = new object[] {
new object()
};
gv.DataBind();
}
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
var lb = e.Row.FindControl("Update") as LinkButton;
lb.CommandArgument = "1";
}
void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
// If you bind gridview after Page_Init,
// This event will not be fired after postback.
LinkButton lb = new LinkButton();
lb.ID = "Update";
lb.Text = "Update";
lb.Click += lb_Click;
e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(lb);
}
void lb_Click(object sender, EventArgs e)
{
var lb = (LinkButton)sender;
string arg = lb.CommandArgument;
}
Upvotes: 2
Reputation: 164
Instead of grdv.Columns.Clear(); can u try and see grdv.AutoGenerateColumns = false;. Because i don't thnik there is an error in your code
Upvotes: 0