2013-09-26 26 views
0

我正在創建一個包含運行時鏈接按鈕和單選按鈕以及複選框列表的頁面。這種情況下的數據源是數據存在的DataTable已更改Radiobutton已更改創建問題

在頁面加載時,我將RadioButton文本部分與DataTable的第一行進行綁定,並隨之創建運行時鏈接按鈕。現在在第二個鏈接按鈕的Click上,單選按鈕文本屬性與第二行DataTable綁定。

最初,與每個單選按鈕關聯的複選框列表處於非活動狀態。我的主要動機是啓用單選按鈕CheckedChanged屬性上的複選框列表。但是,無論何時點擊單選按鈕,單選按鈕和複選框列表的全部文本部分都會消失。但在第一種情況下(在頁面加載時)單擊單選按鈕時,與其相對應的複選框列表已啓用,但在其他任何情況下均未啓用。我詳細的代碼是:

protected void Page_Load(object sender, EventArgs e) 
{ 
    string Query = "select Q101004,Q101005 from Q101 where Q101001<110000013"; 
    DataTable dt = ExecuteDataset(Query).Tables[0]; 
    ViewState["dt"] = dt; 

    Table t = new Table(); 
    TableRow r = new TableRow(); 
    t.Rows.Add(r); 
    TableCell c = new TableCell(); 
    lnkbtn = new LinkButton(); 
    r.Cells.Add(c); 

    rb = new RadioButton(); 
    rb.AutoPostBack = true; 
    rb.ID = "m"; 
    rb.GroupName = "a"; 
    rb.Text = dt.Rows[0][0].ToString(); 

    CbxList = new CheckBoxList(); 
    CbxList.ID = "Cbx"; 
    CbxList.Enabled = false; 
    CbxList.RepeatDirection = RepeatDirection.Horizontal; 
    CbxList.RepeatColumns = 2; 
    CbxList.CellPadding = 10; 
    CbxList.CellSpacing = 5; 
    CbxList.RepeatLayout = RepeatLayout.Table; 
    options = dt.Rows[0][1].ToString().Split('~'); 
    PlaceHolder2.Controls.Add(new LiteralControl("<br/>")); 
    for (int j = 0; j < options.Length; j++) 
    { 
     CbxList.Items.Add(new ListItem(options[j], options[j])); 
    } 

    PlaceHolder2.Controls.Add(rb); 
    PlaceHolder2.Controls.Add(CbxList); 

    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     lnkbtn = new LinkButton(); 
     lnkbtn.Text = (i + 1).ToString(); 
     lnkbtn.Width = 22; 
     lnkbtn.Visible = true; 
     lnkbtn.CommandName = "Test" + i; 
     lnkbtn.CommandArgument = "Hi" + i; 
     lnkbtn.ID = "Hi" + i; 
     PlaceHolder2.Controls.Add(lnkbtn); 
     lnkbtn.Click += new EventHandler(lnkbtn_Click); 
    } 
    rb.CheckedChanged += new EventHandler(rb_CheckedChanged); 
} 

void lnkbtn_Click(object sender, EventArgs e) 
{ 
    DataTable dt = (DataTable)ViewState["dt"]; 

    for (int j = 1; j < dt.Rows.Count; j++) 
    { 
     lnkbtn = (LinkButton)PlaceHolder2.FindControl("Hi"+j); 
     string str = ((LinkButton)sender).CommandArgument; 
     //lnkbtn.Enabled = true; 
     if (lnkbtn.ID == str) 
     { 
      rb = new RadioButton(); 
      rb.AutoPostBack = true; 
      rb.ID = "m"+j; 
      rb.GroupName = "a"; 
      rb.Text = dt.Rows[j][0].ToString(); 

      CbxList = new CheckBoxList(); 
      CbxList.ID = "Cbx"+j; 
      CbxList.Enabled = false; 
      CbxList.RepeatDirection = RepeatDirection.Horizontal; 
      CbxList.RepeatColumns = 2; 
      CbxList.CellPadding = 10; 
      CbxList.CellSpacing = 5; 
      CbxList.RepeatLayout = RepeatLayout.Table; 
      options = dt.Rows[j][1].ToString().Split('~'); 
      PlaceHolder2.Controls.Add(new LiteralControl("<br/>")); 
      for (int i = 0; i < options.Length; i++) 
      { 
       CbxList.Items.Add(new ListItem(options[i], options[i])); 
      } 

      PlaceHolder2.Controls.Add(rb); 
      PlaceHolder2.Controls.Add(CbxList); 
     } 
    } 
} 

void rb_CheckedChanged(object sender, EventArgs e) 
{ 
    Cbx = (RadioButton)PlaceHolder2.FindControl("m"); 
    Cbx1 = (CheckBoxList)PlaceHolder2.FindControl("Cbx"); 

    if (Cbx.Checked == true) 
    { 
     Cbx1.Enabled = true; 
     ViewState["Cbx1"] = "Cbx"; 
    } 
} 

回答

2

每次單擊該按鈕(或回發),Page_Load() fires before Click/Action event。因此,您需要確保僅在頁面未回傳時才執行初始頁面加載代碼。您可以使用if(!Page.IsPostBack){}進行檢查。

我想這可能會解決您的問題。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!Page.IsPostBack){ 
     //your initial page load code should go here 
     //this code will not be executed when page is posting back 
    } 
}