SnehaRK
SnehaRK

Reputation: 1

dynamic linkbutton on dynamic gridview disapears after clicking once

<asp:GridView ID="gvCountryRisk" runat="server" SkinID="gridviewSkin" AllowPaging="false"
                                OnRowDataBound="gvCountryRisk_RowDataBound">
                            </asp:GridView>

protected void gvCountryRisk_RowDataBound(object sender, GridViewRowEventArgs e)
{

    int count = (int)ViewState["Count"];
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 1; i <= count - 1; i++)
        {
            if (e.Row.Cells[i].Text != "&nbsp;")
            {
                e.Row.Cells[i].Text = Convert.ToDouble(e.Row.Cells[i].Text).ToString("N2");
                e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;

                // ----------------dynamic link ----------------

                LinkButton lnkView = new LinkButton();                  
                lnkView.Text = e.Row.Cells[i].Text;
                lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row[i].ToString();

                string Country = gvCountryRisk.HeaderRow.Cells[i].Text;
                string classfn = e.Row.Cells[0].Text;

                ViewState["strClassification"] = classfn;
                ViewState["strCounrty"] = Country;
                int Month = Convert.ToInt16(ddlMonth.SelectedValue);
                int Year = Convert.ToInt16(ddlYear.SelectedValue);
                lnkView.Attributes.Add("onclick", "javascript:ShowDetails('" + Month + "','" + Year + "','" + Country + "','" + classfn + "')");
                e.Row.Cells[i].Controls.Add(lnkView);

            }
        }
    }

}

Here , on each column of gridview linkbutton is displayed , I have to do something on click of linkbutton, its achieved but only for first click, on second time linkbutton disappears but value remain in columns as a label

Upvotes: 0

Views: 490

Answers (1)

pravprab
pravprab

Reputation: 2293

This is because the linkbutton is added in _RowDataBound event and it is not fired on the linkbutton's postback. Please put bind grid out side Ispostback checking. For example

protected void Page_Load(object sender, EventArgs e)
{
    Bindgrid();  // Method to bind grid
    if(!IsPostBack)
    {
        ...

    }
}

If further assistance needed , Please provide the grid datasource binding method. And expose where it is called.

Upvotes: 1

Related Questions