繁体   English   中英

无法在链接按钮单击事件内触发运行时单选按钮checkedchanged事件

[英]Cannot fire runtime radiobutton checkedchanged event inside linkbutton click event

我正在尝试在linkbutton click事件上触发单选按钮检查的已更改事件,但改为转到页面加载,并且不会触发单选按钮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);

    lnkbtn.Text = "Click Here";
    lnkbtn.Visible = true;
    lnkbtn.CommandName = "Test";
    lnkbtn.CommandArgument = "Hi";
    lnkbtn.ID = "Hi";
    PlaceHolder2.Controls.Add(lnkbtn);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        rb = new RadioButton();
        rb.AutoPostBack = true;
        rb.ID = "m" +i;
        rb.GroupName = "a";
        rb.Text = dt.Rows[0][0].ToString();

        CbxList = new CheckBoxList();
        CbxList.ID = "Cbx"+i;
        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);

        if (i ==0)
            rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
        else
            lnkbtn.Click += new EventHandler(lnkbtn_Click);
    }
}


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

        lnkbtn = (LinkButton)PlaceHolder2.FindControl("Hi");
        string str=((LinkButton)sender).CommandArgument;
        //lnkbtn.Enabled = true;
        if (lnkbtn.ID == str)
        {
            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 i = 0; i < options.Length; i++)
            {
                CbxList.Items.Add(new ListItem(options[i], options[i]));
            }

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

            if (lnkbtn.CommandName == "Test")
            {
                rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
            }
        }
}

您的代码订阅了已检查的更改,但未调用它。
如果要执行此操作,可以直接调用rb_CheckedChanged()方法。

您需要阅读ASP.NET页面生命周期-http: //msdn.microsoft.com/zh-cn/library/ms178472(VS.100).aspx

大致来说,以下事件在页面生命周期内被调用。

  • 初始化->控件已创建并与事件处理程序关联
  • 加载ViewState / ControlState->从上次往返行程将控件重置为其以前的状态(包括是否需要触发其事件)
  • 加载->控件被加载到页面的控件树中
  • 执行控制事件(点击等)。

您遇到的问题是,您正在创建控件并将其连接起来以在第4步动态触发,这在页面生命周期中为时已晚。

在下一次往返中,如果有人确实与该控件进行交互,并且生命周期重新开始,则该控件在页面准备执行命令时将不存在。

您需要将RadioButton的创建移至页面创建的较早阶段。 在更新的代码中,尝试将Page_Load代码移到override oninit方法中。

希望这对您有帮助...。 只需使用此代码修改您的代码,您就可以实现所需的输出

private void InitPage()
    {
        string a1, b,a2,b2;
        _objSession = (ClsSession)Session["Login"];
        ds = _objSession._DataSet;
        dtAll = ds.Tables[3];
        dr = dtAll.NewRow();
       string str2 = (string)ViewState["str1"];
       if (str2 != null)
       {
           string[] str3 = str2.Split('~');
           a2 = str3[0];
           b2 = str3[1];
       }
       else
       {
           a2 = "a0";
           b2 = "b0";
       }
            str = (string)ViewState["str"];
        if (str == null)
        {
            a1 = "a0";
            b = "b";
        }
        else
        {
            string[] str1 = str.Split('~');
            a1 = str1[0];
            b = str1[1];
        }
        if (str==null)
        {
            for (int j = 0; j < dtAll.Rows.Count; j++)
            {
                Table t = new Table();
                TableRow r = new TableRow();
                t.Rows.Add(r);
                TableCell c = new TableCell();
                lnkbtn = new LinkButton();
                r.Cells.Add(c);
                lnkbtn.Text = (j + 1).ToString();
                lnkbtn.Visible = true;
                lnkbtn.CommandName = "Test";
                lnkbtn.CommandArgument = "Hi" + j;
                lnkbtn.ID = "Hi" + j;
                lnkbtn.ForeColor = Color.Blue;
                lnkbtn.Width = 30;
                lnkbtn.Font.Bold = true;
                lnkbtn.Font.Size = 14;
                lnkbtn.Font.Underline = false;
                lnkbtn.Click += new EventHandler(lnkbtn_Click);
                c.Controls.Add(lnkbtn);
                plcHdrLinkButton.Controls.Add(lnkbtn);

            }
            ViewState["a"] = 0;
        }

        if (str2 != null)
        {
            string[] str3 = str2.Split('~');
            a2 = str3[0];
            a1 = a2;
        }
                string resultString = Regex.Match(a1, @"\d+").Value;
                int a = int.Parse(resultString);
                ViewState["a"] = a;
                plcHdrQuestion.Controls.Clear();
                rb = new RadioButton();
                rb.ID = "m" + a;
                rb.AutoPostBack = true;
                rb.GroupName = "a";
                rb.Text = (a + 1) + "." + "&nbsp;&nbsp;&nbsp;" + dtAll.Rows[a][4].ToString();
                CbxList = new CheckBoxList();
                CbxList.ID = "Cbx" + a;
                CbxList.Enabled = false;
                CbxList.RepeatDirection = RepeatDirection.Horizontal;
                CbxList.RepeatColumns = 2;
                CbxList.CellPadding = 10;
                CbxList.CellSpacing = 5;
                CbxList.RepeatLayout = RepeatLayout.Table;
                options = dtAll.Rows[a][5].ToString().Split('~');
                plcHdrQuestion.Controls.Add(new LiteralControl("<br/>"));
                for (int i = 0; i < options.Length; i++)
                {
                    CbxList.Items.Add(new ListItem(options[i], options[i]));
                }
                plcHdrQuestion.Controls.Add(rb);
                plcHdrQuestion.Controls.Add(CbxList);
                rb.CheckedChanged += new EventHandler(lnkbtn_Click);
                string st = (string)ViewState["str"];
                ViewState["str1"] = st;
                ViewState["str"] = null;

    }


    protected void lnkbtn_Click(object sender, EventArgs e)
    {
        Boolean _flag=true;
        ds = _objSession._DataSet;
        dt1 = ds.Tables[3];
        dr = dt1.NewRow();
        StringBuilder sb=new StringBuilder ();
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            Cbx = (RadioButton)plcHdrQuestion.FindControl("m" + i);
            Cbx1 = (CheckBoxList)plcHdrQuestion.FindControl("Cbx" + i);
            if (Cbx != null)
            {
                if (Cbx.Checked == true)
                {
                    Cbx1.Enabled = true;
                    _flag = false;
                    string st1 = (string)ViewState["st"];
                    string st="c";
                    if (st1 != null)
                        st = st1 + "~" + st;
                    ViewState["st"] = st;
                     st1 = (string)ViewState["st"];
                    sb.Append(st);
                }
                break;
            }

        }
        int count=(sb.ToString().Count());
        if (count>2)
        {
            _flag = true;
        }
        if ((lnkbtn.CommandName == "Test") && (_flag ==true))
        {
            for (int j = 0; j < dt1.Rows.Count; j++)
            {

                lnkbtn = (LinkButton)plcHdrLinkButton.FindControl("Hi" + j);
                string str = ((LinkButton)sender).CommandArgument;

                lnkbtn.Enabled = true;
                if (lnkbtn.ID == str)
                {
                    ViewState["str"] = str + "~" + lnkbtn.ID;
                    InitPage();
                    ViewState["st"] = null;
                    _flag = false;
                    break;
                }

            }

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM