簡體   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