简体   繁体   中英

Cannot fire runtime radiobutton checkedchanged event inside linkbutton click event

I am trying to fire radiobutton checked changed event on linkbutton click event but instead it goes to the page load and the radiobutton checkedchanged event does not fires.

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

Your code subscribes to the checked changed but does not invoke it.
If you want to do this, you could call the rb_CheckedChanged() method directly.

You need to read up on your ASP.NET Page Life Cycle - http://msdn.microsoft.com/en-us/library/ms178472(VS.100).aspx

Very roughly, the following events get called during page lifecycle.

  • Init -> Controls are created & wired up with event handlers
  • Load ViewState/ControlState -> Controls are reset to their previous state from the last round trip (including whether or not they need to fire their events)
  • Load -> Controls are loaded into the Page's Control Tree
  • Control Events (Clicks etc...) are executed.

The problem you have is that you are creating your control and wiring it up to fire dynamically in the 4th step there, which is too late in the page lifecycle.

On the next round trip, if someone does interact with that control, and the lifecycle starts over, that control won't exist by the time the page is preparing to execute commands.

You'll need to move the creation of your RadioButton to a much earlier stage in the pages creation. In your updated code, try moving your Page_Load code into the override oninit method instead.

hope this helps....its working fine for me. Just Modify your code with this code and You will be able to achieve your desired output

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

            }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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