如何在单击ASP.NET中的提交按钮时获取转发器控件中存在的单选按钮选中值,C# [英] How to get radio button checked value present in repeater controls while click the submit button in ASP.NET, C#

查看:62
本文介绍了如何在单击ASP.NET中的提交按钮时获取转发器控件中存在的单选按钮选中值,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在前端页面我正在使用转发器控制打印许多无线电我的前端是

in front end page i'm using repeater control for printing many number of radios my front end is

<asp:Repeater ID="QuestionRepeater" runat="server" >
               <HeaderTemplate>
                   <table>
               </HeaderTemplate>
               <SeparatorTemplate>
                   <tr>
                       <td>
                           <br />
                       </td>
                   </tr>
               </SeparatorTemplate>
               <ItemTemplate>
                   <tr>
                       <td>
                          <%#Eval("Question1") %>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb1" GroupName="Rb_Choice" Text='<%#Eval("Choice_1") %>' Checked="false" OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb2" GroupName="Rb_Choice" Text='<%#Eval("Choice_2") %>' OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb3" GroupName="Rb_Choice" Text='<%#Eval("Choice_3") %>' OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb4" GroupName="Rb_Choice" Text='<%#Eval("Choice_4") %>' OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
               </ItemTemplate>
               <FooterTemplate>
                   </table>
               </FooterTemplate>
           </asp:Repeater>



我希望在后端页面找到单选按钮选中的值



< b>我尝试了什么:



我的后端我只是显示收音机的msg框被选中或不是像


and i want to find out in back end page radio button checked value

What I have tried:

my back end i'm just display msg box for the radio is checked or not like

protected void Get_Answers(object sender, EventArgs e)
       {
           //var text = "Your Answers will be submitted successfully";
           //Response.Write(text);
           foreach (RepeaterItem item in QuestionRepeater.Items)
           {
               RadioButton rb1 = (RadioButton)item.FindControl("rb1");
               //Response.Write(rb1.GroupName+"<br>");
               RadioButton rb2 = (RadioButton)item.FindControl("rb2");
               RadioButton rb3 = (RadioButton)item.FindControl("rb3");
               RadioButton rb4 = (RadioButton)item.FindControl("rb4");
               ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('"+ rb1.Text + ":" + rb1.Checked + rb2.Text + ":" + rb2.Checked + rb3.Text + ":" + rb3.Checked + rb4.Text + ":" + rb4.Checked + "')", true);



即使选中了单选按钮,我也总是假的

请帮我如何获得检查值


i'm always get false even though the radio button is checked
please help me how to get checked value

推荐答案

首先确保你没有在回发时绑定转发器,否则你只是将所有值重置为初始状态



First of all make sure you are not binding the repeater on postback otherwise you're just resetting all the values to their initial state

if (!Page.IsPostBack)
{
    // bind your repeater here
}





接下来,您只对转发器中的最后一个问题执行js警报。 RegisterClientScriptBlock中的msg参数是该脚本块的键,键是唯一的,所以如果你有5个问题,你试图添加5个名为msg的脚本块,但是msg是关键.net只会使用您配置的最后一个,因为该密钥的每个块都替换了以前的密钥。如果你想为每个问题提供一个警报,你需要一个唯一的密钥。





Next you're only doing a js alert for the last question in the repeater. The "msg" param in RegisterClientScriptBlock is the key for that script block, and keys are unique, so if you have 5 questions you are trying to add 5 script blocks called "msg" but as msg is the key .net will only use the last one you configure as each block of that key replaces the previous ones. If you want an alert per question you need a unique key.

int i = 0;
foreach (RepeaterItem item in QuestionRepeater.Items)
{
    RadioButton rb1 = (RadioButton)item.FindControl("rb1");
    //Response.Write(rb1.GroupName+"<br>");
    RadioButton rb2 = (RadioButton)item.FindControl("rb2");
    RadioButton rb3 = (RadioButton)item.FindControl("rb3");
    RadioButton rb4 = (RadioButton)item.FindControl("rb4");
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg" + (i++).ToString(), "alert('" + rb1.Text + ":" + rb1.Checked + rb2.Text + ":" + rb2.Checked + rb3.Text + ":" + rb3.Checked + rb4.Text + ":" + rb4.Checked + "');", true);
}





请注意,除了唯一键我还添加了;警报之后。



Note that as well as the unique key I also added a ";" after the alert.


这篇关于如何在单击ASP.NET中的提交按钮时获取转发器控件中存在的单选按钮选中值,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆