Problem of design value of asp .Net questionnaire.

108 4

All right, we're doing a data analysis system for the teacher, one of which is a survey. Because the subject of the questionnaire is fixed, it isn't involved in the database, as long as the questionnaire is inserted into the database. The problem I've come with is to use multiple radiobuttonlist to let the user choose, but how to get each value to multiple radiobuttonlist at once. My original method is a value, and then inserted into the database, felt that this method is very clumsy, but I'm not familiar with my asp. Net. Thank you.
My code is as follows:
Background:

string value1 ="";
 for (int i = 0; i <RadioButtonList1.Items.Count; i++)
 {
 if (RadioButtonList1.Items[i].Selected)
 {
 value1 = RadioButtonList1.Items[i].Value;
 }
 }
 string value2 ="";
 for (int i = 0; i <RadioButtonList2.Items.Count; i++)
 {
 if (RadioButtonList2.Items[i].Selected)
 {
 value2 = RadioButtonList2.Items[i].Value;
 }
 }
. . ....此后还有很多行类似上面的代码

Front desk:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
 <asp:ListItem Text="非常重要" Value="0.75,1,1"></asp:ListItem>
 <asp:ListItem Text="明显重要" Value="0.5,0.75,1"></asp:ListItem>
 <asp:ListItem Text="重要" Value="0,0.25,5"></asp:ListItem>
 <asp:ListItem Text="稍微重要" Value="0,0,0.25"></asp:ListItem>
 <asp:ListItem Text="同等重要" Value="1"></asp:ListItem>
 </asp:RadioButtonList>
3 Answers

80 5

Your problem contains two content:
1 ) how to get the selected value for each radiobuttonlist ( the following referred short )
2 ) you don't want to follow the id value of each radiobuttonlist to implement the traversal of all occurrences?
If it's:
1 ) the first question jkey has answered.
2 ) idea: get the collection of controls for all radiobuttonlist, and then iterate through the collection.

List<string> selectedValues=new List<string>();
 foreach(Control c in pnlContainer.Controls)
 {
 if(c is RadioButtonList)
 {
 RadioButtonList rbl=(RadioButtonList)c;
 selectedValues.Add(rbl.SelectedValue);
 }
 }

In the aspx page, it's better to place all occurrences within a parent control

<asp:Panel runat="server" id="pnlContainer">
 <asp:RadioButtonList ID="RadioButtonList1" runat="server">
 <asp:ListItem Text="非常重要" Value="0.75,1,1"></asp:ListItem>
. . ....
 </asp:RadioButtonList>
 <asp:RadioButtonList ID="RadioButtonList2" runat="server">
 <asp:ListItem Text="非常重要" Value="0.75,1,1"></asp:ListItem>
. . ....
 </asp:RadioButtonList>
</asp:Panel>
133 0

Your problem contains two content:
1 ) how to get the selected value for each radiobuttonlist ( the following referred short )
2 ) you don't want to follow the id value of each radiobuttonlist to implement the traversal of all occurrences?
If it's:
1 ) the first question jkey has answered.
2 ) idea: get the collection of controls for all radiobuttonlist, and then iterate through the collection.

List<string> selectedValues=new List<string>();
 foreach(Control c in pnlContainer.Controls)
 {
 if(c is RadioButtonList)
 {
 RadioButtonList rbl=(RadioButtonList)c;
 selectedValues.Add(rbl.SelectedValue);
 }
 }

In the aspx page, it's better to place all occurrences within a parent control

<asp:Panel runat="server" id="pnlContainer">
 <asp:RadioButtonList ID="RadioButtonList1" runat="server">
 <asp:ListItem Text="非常重要" Value="0.75,1,1"></asp:ListItem>
. . ....
 </asp:RadioButtonList>
 <asp:RadioButtonList ID="RadioButtonList2" runat="server">
 <asp:ListItem Text="非常重要" Value="0.75,1,1"></asp:ListItem>
. . ....
 </asp:RadioButtonList>
</asp:Panel>
55 1
string value1 = RadioButtonList1.SelectedValue;
string value2 = RadioButtonList2.SelectedValue;
140 5
string value1 = RadioButtonList1.SelectedValue;
string value2 = RadioButtonList2.SelectedValue;
...