How to find control with in repeater on button click event and repeater is placed with in gridview in asp.net C#

跟風遠走 提交于 2019-12-13 06:17:34

问题


I need to check wheather the radionbutton is checked Or NOT then need to have its value to a variable .

How can i loop through the radioButtonList in repeater to check that user choose True or false on button click and button is placed outside the gridview and repeater.

I tried this: protected void btnsave_Click(object sender, EventArgs e) { if (!Page.IsValid) return; int tcounter = 0; int fcounter = 0;

        for (int i = 0; i < gridMainSurveyQuestion.Rows.Count; i++)
        {
            GridView grid = (GridView)gridMainSurveyQuestion.Rows[i].FindControl("RptQuestions");

            Repeater repea = (Repeater)grid.Rows[i].FindControl("RptQuestions");
            for (int j = 0; j < repea.Items.Count; j++)
            {
                RepeaterItem currentitem = repea.Items[j];
                RadioButtonList rlist = (RadioButtonList)currentitem.FindControl("rblanswer");
                if (rlist.SelectedItem.Value == "0")
                {
                    fcounter++;
                    lblfalse.Text = fcounter.ToString();
                }
                if (rlist.SelectedItem.Value == "1")
                {
                    tcounter++;
                    lbltrue.Text = tcounter.ToString();
                }
            }
        }
    }

but shows error: Unable to cast object of type 'System.Web.UI.WebControls.Repeater' to type 'System.Web.UI.WebControls.GridView'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.Repeater' to type 'System.Web.UI.WebControls.GridView'.

Source Error:

Line 89: for (int i = 0; i < gridMainSurveyQuestion.Rows.Count; i++) Line 90: { Line 91: GridView grid = (GridView)gridMainSurveyQuestion.Rows[i].FindControl("RptQuestions"); Line 92: Line 93: Repeater repea = (Repeater)grid.Rows[i].FindControl("RptQuestions");

Source File: C:\Users\madiha.rahman\Desktop\PICG_SurveyModule\PICG_SurveyModule\Survey.aspx.cs Line: 91

can you correct it


回答1:


You will first need to iterate through GridView Rows for finding each repeater. Then from each repeater find each radiobuttonlist, like shown below.

  protected void Button1_Click(object sender, EventArgs e)
  {
        foreach (GridViewRow gvRow in gvTemp.Rows)
        {
            Repeater repeater = (Repeater)gvRow.FindControl("repeater1");

            foreach (RepeaterItem repItem in repeater.Items)
            {
                RadioButtonList rbList = (RadioButtonList)repItem.FindControl("radiobuttonlist1");

                foreach (ListItem item in rbList.Items)
                {
                    if (item.Selected)
                    {
                        //code for selected items goes here...
                    }
                    else
                    {
                        //code for not selected items goes here...
                    }

                    if (item.Value == "0")
                    { 
                        //code for items with value == "0" goes here...
                    }

                    if (item.Value == "1")
                    {
                        //code for items with value == "1" goes here...
                    }
                }
            }
        }
  }

Happy Coding...;)

EDIT : Removed the checkbox & placed radiobuttonlist as required by the questioner.

EDIT : Added inner foreach loop that iterates through each radiobutton in the radiobuttonlist, as requested by the Questioner.



来源:https://stackoverflow.com/questions/13468594/how-to-find-control-with-in-repeater-on-button-click-event-and-repeater-is-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!