0

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.

7
  • How you are loading your dropdownlist and show your code for selection changed event. I think not handling postback is actual issue. Commented May 14, 2014 at 6:06
  • @hassan i updated the code with dropdownlist.i don't want to call display method in page load.is there any otherway to do this. Commented May 14, 2014 at 6:11
  • Ok and how you are populating ddlProcess? Commented May 14, 2014 at 6:11
  • @HassanNisar i binded with one datatable in pageload.gridview is showing.but if i click gridview link button not firing and its disappearing. Commented May 14, 2014 at 6:13
  • 1
    All dynamic controls on postback need to be recreated at their respective positions in their control tree for their postback and viewstate to work correctly... Commented May 14, 2014 at 6:18

2 Answers 2

2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

its working code only.but after post back dynamic gridview will disapperar if we are not calling in page load.is there any otherway to retain that gridview.i think bcse of this link is not firing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.