0

我正在创建一个带有运行时链接按钮和单选按钮以及复选框列表的页面。在这种情况下,数据源是DataTable数据所在的位置。

在页面加载时,我将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";
    }
}
4

1 回答 1

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 
    }
}
于 2013-09-27T10:19:51.283 回答