0

尝试制作一个简单的应用程序,其中我有一个下拉列表,其中包含 1 到 4 的数字。

根据用户选择的数量 - 我使用绑定的checkedchanged事件动态创建这个数量的复选框。因此,当用户检查某些复选框时,会引发 checkchanged 事件,并且我将选中复选框的文本存储在会话中,然后当我单击按钮时,我只想从选中的复选框中查看文本。

但似乎在 checkedchanged 事件处理程序中我应该重新创建动态控制,但我还没有找到解决方案。先感谢您。

public partial class proba : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dd1.Items.Add("1");
        dd1.Items.Add("2");
        dd1.Items.Add("3");
        dd1.Items.Add("4");
}

protected void dd1_SelectedIndexChanged1(object sender, EventArgs e)
{
    int numTourists = Convert.ToInt32(dd1.SelectedItem.Text);

    for (int i = 0; i < numTourists; i++)
    {
        CheckBox chk = new CheckBox();
        chk.ID = "chk" + i;
        chk.Text = "box" + i;

         chk.AutoPostBack = true;

        chk.CheckedChanged += new EventHandler(checkChanged);
        Page.FindControl("form1").Controls.Add(chk);


    }
}

protected void checkChanged(object sender, EventArgs e)
{
     // here I should recteate the control

    CheckBox chk = (CheckBox)sender;

    lblpr.Text += chk.Text;

    Srolession["chk"] = chk.Text;


}
4

1 回答 1

1
     static bool chkddlchange=false; //define this....to check you Drop Changed Or Not.
    protected void Page_Load(object sender, EventArgs e)
        {
            dd1.Items.Add("1");
            dd1.Items.Add("2");
            dd1.Items.Add("3");
            dd1.Items.Add("4");
    if(Page.IsPostBack && chkddl==true)
    {
     int numTourists = Convert.ToInt32(dd1.SelectedItem.Text);
       chkddl=true;//make true so you can know that you ddlindex is changed..
        for (int i = 0; i < numTourists; i++)
        {
            CheckBox chk = new CheckBox();
            chk.ID = "chk" + i;
            chk.Text = "box" + i;

             chk.AutoPostBack = true;

            chk.CheckedChanged += new EventHandler(checkChanged);
            Page.FindControl("form1").Controls.Add(chk);


        }
    }
    }

    protected void dd1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int numTourists = Convert.ToInt32(dd1.SelectedItem.Text);
       chkddl=true;//make true so you can know that you ddlindex is changed..you have to 
checkboxex on page load
        for (int i = 0; i < numTourists; i++)
        {
            CheckBox chk = new CheckBox();
            chk.ID = "chk" + i;
            chk.Text = "box" + i;

             chk.AutoPostBack = true;

            chk.CheckedChanged += new EventHandler(checkChanged);
            Page.FindControl("form1").Controls.Add(chk);


        }
    }
于 2013-05-18T12:49:21.373 回答