无法触发运行时单选按钮选中链接按钮单击事件中的已更改事件

本文关键字:事件 单击 按钮 链接 运行时 单选按钮 | 更新日期: 2023-09-27 18:33:01

我正在尝试在链接按钮单击事件上触发单选按钮检查更改事件,但它进入页面加载并且单选按钮选中更改事件没有触发。

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(( 方法。

希望这有帮助....它对我来说工作正常。只需使用此代码修改您的代码,您就可以实现所需的输出

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;
                }
            }
        }
    }
您需要

阅读 ASP.NET 页面生命周期 - http://msdn.microsoft.com/en-us/library/ms178472(VS.100(.aspx

粗略地说,以下事件在页面生命周期中被调用。

  • Init -> 控件创建并与事件处理程序连接
  • 加载视图状态/控件状态 -> 控件从上次往返重置为以前的状态(包括是否需要触发其事件(
  • 加载 -> 控件加载到页面的控件树中
  • 执行控制事件(单击等(。

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

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

您需要将单选按钮的创建移动到页面创建的早期阶段。在更新的代码中,尝试将Page_Load代码移动到 override oninit 方法中。