在asp.net中的嵌套转发器控件中查找单选按钮控件?

时间:2013-09-06 13:20:43

标签: asp.net c#-4.0

我有两个转发器控件,一个在另一个内部转发器中,有一个占位符,动态生成单选按钮。我想找到单选按钮控件来检查它是否被检查?我想要在代码中定义的按钮提交/点击事件中执行上述所有功能吗?

    if (!Page.IsPostBack)
        {       
        //1) Load SomeDatatable from Database somehow
        // Just for testing : replace with query to DB

        strqry = "select * from Quiz_tblQsnsLimitMaster where Qsnexamname='" + Request.QueryString["QsnEname"].ToString() + "'";
        SqlDataAdapter adp = new SqlDataAdapter(strqry, sCon);
        DataSet ds = new DataSet();
        try
        {
            adp.Fill(ds);
            int total = ds.Tables[0].Rows.Count;
            for (int i = 0; i < total; i++)
            {
                string QuesID = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                //SubName = ds.Tables[0].Rows[i].ItemArray[3].ToString();
                DataSet oDs = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_OnlineTest_QuestionsWithOptions_Get", QuesID);
                SomeDatatable.Merge(oDs.Tables[0]);
            }
            removeDuplicatesRows(SomeDatatable);
            System.Data.DataColumn newColumn = new System.Data.DataColumn("ContentIndex", typeof(System.String));
            newColumn.DefaultValue = "0";
            SomeDatatable.Columns.Add(newColumn);
            for (int i = 0; i < Math.Ceiling((decimal)SomeDatatable.Rows.Count); i++)
                SomeDatatable.Rows[i]["ContentIndex"] = i + 1;
        }
        catch
        {
        }

        ////2) Create a dummy data source for the tab repeater using a list of anonymous types
        List<object> TabList = new List<object>();
        //BindSubject();
        for (int I = 0; I < Math.Ceiling((decimal)SomeDatatable.Rows.Count / (decimal)ContentPerTab); I++)
        {
            TabList.Add(new { TabIndex = I });
        }

        TabRepeater.ItemDataBound += TabRepeater_ItemDataBound;
        TabRepeater.DataSource = TabList;
        TabRepeater.DataBind();

        //TablLinkRepeater.DataSource = TabList;
        //TablLinkRepeater.DataBind();
        //}
    }


    public void removeDuplicatesRows(DataTable dt)
    {
        SomeDatatable = dt.DefaultView.ToTable(true, "QuestionId");
    }
    protected void TabRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
        {
            int TabIndex = -1;
            int.TryParse(DataBinder.Eval(e.Item.DataItem, "TabIndex").ToString(), out TabIndex);

            //Copy Content Rows from SomeDatable that belong to this tab
            DataTable Dt = SomeDatatable.Clone();
            for (Int32 i = TabIndex * ContentPerTab; i <= (TabIndex + 1) * ContentPerTab - 1; i++)
            {
                if (i >= SomeDatatable.Rows.Count) break;

                Dt.ImportRow(SomeDatatable.Rows[i]);
            }

            // Find the content repeater in this item and use the new datatable as source
            Repeater ContentRepeater = (Repeater)e.Item.FindControl("ContentRepeater");

            ContentRepeater.ItemDataBound += ContentRepeater_ItemDataBound;

            ContentRepeater.DataSource = Dt;
            ContentRepeater.DataBind();
        }
    }

    // This handler might be needed for content repeater, included just for testing 
    protected void ContentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //Read coulmn from Datarow
            int ContentIndex = -1;
            int.TryParse(DataBinder.Eval(e.Item.DataItem, "ContentIndex").ToString(), out ContentIndex);
            var parsed = "'" + HttpUtility.ParseQueryString(DataBinder.Eval(e.Item.DataItem, "QuestionID").ToString()) + "'";
            //Add Question 
            DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get", Convert.ToString(parsed));

            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    int iCnt = 0;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        Table tblQsn = new Table();

                        //.....Begin Text Qsn Creation.....//
                        tblQsn.Width = new Unit("98%");

                        TableRow trQsn = new TableRow();
                        iRowCounter++;
                        trQsn.ID = "Row_" + iRowCounter.ToString();


                        TableCell tcQsn = new TableCell();
                        TableCell tcQsnSNo = new TableCell();

                        tcQsn.CssClass = "Label";
                        tcQsn.BackColor = System.Drawing.Color.Gainsboro;
                        tcQsn.Font.Bold = true;
                        tcQsn.Font.Size = 12;
                        tcQsn.Text = ds.Tables[0].Rows[i][1].ToString();
                        tcQsn.Width = Unit.Percentage(99.5);
                        iCellCounter++;
                        tcQsn.ID = "Cell_" + iCellCounter.ToString();

                        tcQsnSNo.CssClass = "Label";
                        tcQsnSNo.Attributes.Add("valign", "top");
                        tcQsnSNo.BackColor = System.Drawing.Color.Gainsboro;
                        tcQsnSNo.Font.Bold = true;
                        tcQsnSNo.Width = Unit.Percentage(0.5);
                        iCellCounter++;
                        tcQsnSNo.ID = "Cell_" + iCellCounter.ToString();
                        iCnt++;
                        tcQsnSNo.Text = ContentIndex.ToString() + ".";

                        trQsn.Cells.Add(tcQsnSNo);
                        trQsn.Cells.Add(tcQsn);
                        tblQsn.Rows.Add(trQsn);

                        int rcnt = i;
                        int iOptCnt = 0;
                        string sStatus = "N";
                        while ((rcnt >= 0) && (rcnt < ds.Tables[0].Rows.Count))
                        {
                            if (ds.Tables[0].Rows[rcnt][2].ToString() == ds.Tables[0].Rows[i][2].ToString())
                            {
                                if (sStatus == "N")
                                {
                                    sStatus = "Y";
                                }

                                TableRow trQsnOpt = new TableRow();
                                iRowCounter++;
                                trQsnOpt.ID = "Row_" + iRowCounter.ToString();
                                TableCell tcQsnOpt = new TableCell();
                                tcQsnOpt.CssClass = "Label";
                                iCellCounter++;
                                tcQsnOpt.ID = "Cell_" + iCellCounter.ToString();
                                tcQsnOpt.Attributes.Add("valign", "top");
                                tcQsnOpt.VerticalAlign = VerticalAlign.Middle;
                                TableCell tcQsnOptSNo = new TableCell();
                                tcQsnOptSNo.CssClass = "Label";
                                iCellCounter++;
                                tcQsnOptSNo.ID = "Cell_" + iCellCounter.ToString();

                                iOptCnt++;
                                RadioButton oRbOptions = new RadioButton();
                                oRbOptions.GroupName = ds.Tables[0].Rows[rcnt][2].ToString() + "_Group";
                                oRbOptions.Text = ds.Tables[0].Rows[rcnt][3].ToString().Trim();
                                oRbOptions.Font.Size = 11;
                                iRbTCounter++;
                                oRbOptions.ID = ds.Tables[0].Rows[i][0].ToString() + "_" + ds.Tables[0].Rows[rcnt][2].ToString() + "_" + "Option" + iOptCnt.ToString() + "_" + iRbTCounter.ToString();
                                //oRbOptions.Enabled = false;

                                //if (ds.Tables[0].Rows[i][2].ToString() == "Option" + iRbTCounter.ToString())
                                //{
                                //    oRbOptions.Checked = true;
                                //}

                                oRbOptions.InputAttributes.Add("data-info", Convert.ToString(ContentIndex));
                                oRbOptions.CssClass = "Label";
                                tcQsnOpt.Controls.Add(oRbOptions);
                                tcQsnOptSNo.Text = iOptCnt.ToString() + ".";
                                trQsnOpt.Cells.Add(tcQsnOptSNo);
                                trQsnOpt.Cells.Add(tcQsnOpt);
                                rcnt++;
                                //.....Add Option Image.....//
                                tblQsn.Rows.Add(trQsnOpt);

                            }
                            else
                                break;
                        }
                        i = rcnt - 1;
                        PlPreview = (PlaceHolder)e.Item.FindControl("PlPreview");
                        PlPreview.Controls.Add(tblQsn);
                    }
                }
            }
        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        SubmitQuestion();
    }

    public void SubmitQuestion()
    {
        for (int c = 0; c < SomeDatatable.Rows.Count; c++)
        {
            string sQsnOptId = SomeDatatable.Rows[c]["QuestionID"].ToString();
            DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get", "'" + sQsnOptId + "'");           
            for (int i = 1; i <= 4; i++)
            {
                string rdboption = ds.Tables[0].Rows[c][0].ToString() + "_" + ds.Tables[0].Rows[i-1][2].ToString() + "_" + "Option" + i + "_" + i;
                Repeater rpt = (Repeater)TabRepeater.NamingContainer.FindControl("ContentRepeater");
                RadioButton rbt = (RadioButton)rpt.NamingContainer.FindControl(rdboption);             
                if (rbt.Checked)
                {
                    strqry = "update Quiz_tblOnlineTest_Detail set UserAns='Option" + i + "', DeletionStatus='A' where CreationLogInId='AMITSAMBYAL@HOTMAIL.COM' and ExamName='" + Request.QueryString["QsnEname"].ToString() + "' and QsnId=" + Session["quesid"].ToString() + "";
                    cmd = new SqlCommand(strqry, con);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        con.Close();
                    }
                    break;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了答案。感谢

        foreach (RepeaterItem repeater in TabRepeater.Items)
        {
            Repeater repeater1 = (Repeater)repeater.FindControl("ContentRepeater");
            foreach (RepeaterItem repItem in repeater1.Items)
            {
                for (int i = 1; i <= 4; i++)
                {
                    string rdboption = ds.Tables[0].Rows[c][0].ToString() + "_" + ds.Tables[0].Rows[i - 1][2].ToString() + "_" + "Option" + i + "_" + i;
                    PlaceHolder PlPreview = (PlaceHolder)repItem.FindControl("PlPreview");
                    rbt = (RadioButton)PlPreview.FindControl(rdboption);
                    if (rbt.Checked)
                    {
                            // statement
                    }
                }

            }
        }

    }
}