首页 文章

如何在按钮点击事件和转发器中使用转发器查找控件在asp.net C#中的gridview中放置

提问于
浏览
0

我需要检查radionbutton是否被检查或者不需要将其值赋予变量 .

如何在转发器中循环通过radioButtonList来检查用户在按钮单击时选择True或false,并将按钮放在gridview和repeater之外 .

我试过这个: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();
                }
            }
        }
    }

但显示错误:无法将类型为“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 <gridMainSurveyQuestion.Rows.Count; i)第90行:{第91行:GridView网格=(GridView)gridMainSurveyQuestion.Rows [i] .FindControl(“RptQuestions”);第92行:第93行:Repeater repea =(Repeater)grid.Rows [i] .FindControl(“RptQuestions”);

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

你能改正吗

1 回答

  • 1

    您首先需要遍历GridView Rows以查找每个转发器 . 然后从每个中继器找到每个radiobuttonlist,如下所示 .

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

    快乐编码...;)

    EDIT : 删除了提问者要求的复选框和放置的radiobuttonlist .

    EDIT : 根据提问者的要求,添加了内部foreach循环,该循环遍历radiobuttonlist中的每个radiobutton .

相关问题