0

我正在尝试在链接按钮单击事件上触发单选按钮检查更改事件,但它会转到页面加载并且单选按钮检查更改事件不会触发。

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);
            }
        }
}
4

3 回答 3

1

您的代码订阅了已检查的更改,但不调用它。
如果你想这样做,你可以直接调用 rb_CheckedChanged() 方法。

于 2013-09-26T07:51:47.893 回答
0

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

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;
                }

            }

        }
    }
于 2013-09-27T04:07:49.500 回答
0

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

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

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

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

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

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

于 2013-09-26T08:01:50.053 回答