2014-05-14 101 views
0

我有兩個鏈接一個動態的GridView button.if我按一下,如果我調用頁面「顯示」方法加載其工作罰款鏈接按鈕事件不firing.but。下面鏈接按鈕,在動態的GridView不費一槍在asp.net

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>"); 
    } 
如果我把

代碼頁「顯示」方法加載,它會調用每一個postback.i不希望這樣。我想打電話給在下拉選項更改事件此方法。如果我將「顯示」方法放入該鏈接點擊事件不會觸發。 所以我必須做的,以克服這一點。

+0

如何加載您的'dropdownlist'並顯示您選擇的代碼更改事件。我認爲不處理回傳是實際問題。 – Hassan

+0

@hassan我用dropdownlist更新了代碼,我不想在頁面load.is中調用顯示方法。 – nichu09

+0

好吧,你是如何填充'ddlProcess'?如果我點擊GridView的鏈接按鈕不觸發其消失 – Hassan

回答

0

相反grdv.Columns.Clear的();可以試試看grdv.AutoGenerateColumns = false ;.因爲我不thnik有一個錯誤在你的代碼

+0

其工作代碼而已。不過後回傳動態的GridView會,如果我們沒有在頁面調用disapperar load.is有任何otherway以保留gridview.i認爲BCSE這個環節不是射擊 – nichu09

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; 
    } 
相關問題