链接按钮不在asp.net中的动态网格视图中触发

时间:2014-05-14 05:52:54

标签: c# asp.net gridview

我有一个带有两个链接按钮的动态网格视图。如果我点击链接按钮事件没有激活。但是如果我打电话"显示"页面中的方法加载其下面的工作fine.code

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

如果我把&#34;显示&#34;在页面加载的方法,它将调用每个postback.i不要这样。我想在下拉选择更改事件中调用此方法。如果我把&#34;显示&#34;链接点击事件内部的方法未触发。 所以我必须做些什么来克服这一点。

2 个答案:

答案 0 :(得分:2)

如果您在回发后没有绑定网格,则必须在Page_Load之前创建gridview。 GridView需要加载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;
    }

答案 1 :(得分:0)

而不是grdv.Columns.Clear();你可以尝试看看grdv.AutoGenerateColumns = false;。因为我不知道你的代码中有错误