madi virgo_007
madi virgo_007

Reputation: 21

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

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

Upvotes: 0

Views: 4663

Answers (1)

Devraj Gadhavi
Devraj Gadhavi

Reputation: 3611

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.

Upvotes: 1

Related Questions