单击一次后动态gridview上的动态链接按钮消失

时间:2014-04-16 05:55:22

标签: c# asp.net gridview

<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);

            }
        }
    }

}

在这里,在gridview链接按钮的每一列上显示,我必须点击linkbutton做一些事情,它实现但只有第一次点击,第二次linkbutton消失但值仍作为标签保留在列中

1 个答案:

答案 0 :(得分:1)

这是因为在linkbutton事件中添加了_RowDataBound,并且不会在linkbutton的回发中触发它。请将绑定网格放在Ispostback侧检查。例如

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

    }
}

如果需要进一步的帮助,请提供网格数据源绑定方法。并揭露其所在的位置。