【问题标题】:How to find control with in repeater on button click event and repeater is placed with in gridview in asp.net C#如何在按钮单击事件的中继器中找到控件,并将中继器放置在asp.net C#的gridview中
【发布时间】:2012-11-20 19:10:30
【问题描述】:

我需要检查是否选中了单选按钮,或者是否需要将其值设置为变量。

我如何循环遍历转发器中的 radioButtonList 以检查用户在单击按钮时选择 True 或 false 并且按钮放置在 gridview 和转发器之外。

我试过这个: protected void btnsave_Click(对象发送者,EventArgs e) { if (!Page.IsValid) 返回; 整数 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();
                }
            }
        }
    }

但显示错误: 无法将“System.Web.UI.WebControls.Repeater”类型的对象转换为“System.Web.UI.WebControls.GridView”类型。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidCastException:无法将“System.Web.UI.WebControls.Repeater”类型的对象转换为“System.Web.UI.WebControls.GridView”类型。

来源错误:

第 89 行:for (int i = 0; i

源文件:C:\Users\madiha.rahman\Desktop\PICG_SurveyModule\PICG_SurveyModule\Survey.aspx.cs 行:91

你能改正吗

【问题讨论】:

  • 从您的代码中删除这一行 GridView grid = (GridView)gridMainSurveyQuestion.Rows[i].FindControl("RptQuestions");.
  • 同时编辑你的行 Repeater repea = (Repeater)grid.Rows[i].FindControl("RptQuestions"); to 中继器重复 = (Repeater)gridMainSurveyQuestion.Rows[i].FindControl("RptQuestions");

标签: c# asp.net button gridview repeater


【解决方案1】:

您首先需要遍历 GridView Rows 以查找每个中继器。然后从每个中继器中找到每个单选按钮列表,如下所示。

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

快乐编码...;)

编辑:根据提问者的要求删除复选框并放置单选按钮列表。

编辑:根据提问者的要求,添加了遍历单选按钮列表中每个单选按钮的内部 foreach 循环。

【讨论】:

  • 我需要找到单选按钮列表并需要计算选中和未选中的单选按钮
  • 我已根据您的要求更改了代码。对于错误,请参阅我的 cmets 关于您的问题。如果这对您有帮助,请接受作为答案。
  • 它只返回 o =false 和 1=true 的数量,我还需要有一次未选择的数量并在 gridview 中进行分页,我需要为此做些什么吗?
  • 我已经编辑了我的答案并添加了一个内部 foreachloop 用于遍历单选按钮列表中的列表项
  • 如果满足您的要求,请将答案标记为已接受的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
相关资源
最近更新 更多