2014-09-23 44 views
1

selectede我有一個gridview在我所6 radionbuttons。在提交按鈕單擊我需要確定哪些radiobuttongrid每一行中被選中。現在即時通訊使用下面的代碼:標識哪個單選按鈕是在GridViewRow

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("OrderID"); 
     dt.Columns.Add("QuestionID"); 
     dt.Columns.Add("Rating"); 
     foreach (GridViewRow gr in grdSurvey.Rows) 
     { 
      DataRow dr = dt.NewRow(); 
      dr["QuestionID"] = ((HiddenField)gr.FindControl("hfQuestionID")).Value; 
      dr["OrderID"] = ViewState["_ORDERID"].ToString(); 
      if (((RadioButton)gr.FindControl("rbSD")).Checked) 
      { 
       dr["Rating"] = "1"; 
      } 
      else if (((RadioButton)gr.FindControl("rbSD")).Checked) 
      { 
       dr["Rating"] = "2"; 
      } 
      else if (((RadioButton)gr.FindControl("rbDS")).Checked) 
      { 
       dr["Rating"] = "3"; 
      } 
      else if (((RadioButton)gr.FindControl("rbNAND")).Checked) 
      { 
       dr["Rating"] = "4"; 
      } 
      else if (((RadioButton)gr.FindControl("rbAS")).Checked) 
      { 
       dr["Rating"] = "5"; 
      } 
      else if (((RadioButton)gr.FindControl("rbSA")).Checked) 
      { 
       dr["Rating"] = "6"; 
      } 
      else if (((RadioButton)gr.FindControl("rbNA")).Checked) 
      { 
       dr["Rating"] = "0"; 
      } 
      dt.Rows.Add(dr); 
     } 
     int Res = InsertSurveyResponse(dt); 
     if (Res > 0) 
     { 
      Response.Write(Res + " Response Submitted."); 
     } 
    } 

前端低於:

<div id="divForm" runat="server"> 
    <table width="80%" style="text-align:center;"> 
     <tr> 
      <td>Your feedback is important and we promise it will take less than a minute of your time. All you have to do is – read the questions below and select the rating that you feel best describes your experience with us. 
      </td> 
     </tr> 
     <tr> 
      <td>Kindly give us your feedback below 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <asp:gridview id="grdSurvey" runat="server" autogeneratecolumns="false" clientidmode="static"> 
        <Columns> 
         <asp:TemplateField HeaderText="Question"> 
          <ItemTemplate> 
           <asp:HiddenField ID="hfQuestionID" runat="server" Value='<%#Eval("QuestionID") %>'></asp:HiddenField> 
           <asp:Label ID="lblQuestion" runat="server" Text='<%#Eval("Question") %>'></asp:Label> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Strongly Disagree"> 
          <ItemTemplate> 
           <asp:RadioButton ID="rbSD" runat="server" GroupName="Survey" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Disagree Somewhat"> 
          <ItemTemplate> 
           <asp:RadioButton ID="rbDS" runat="server" GroupName="Survey" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Neither Agree nor disagree"> 
          <ItemTemplate> 
           <asp:RadioButton ID="rbNAND" runat="server" GroupName="Survey" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Agree Somewhat"> 
          <ItemTemplate> 
           <asp:RadioButton ID="rbAS" runat="server" GroupName="Survey" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Strongly Agree"> 
          <ItemTemplate> 
           <asp:RadioButton ID="rbSA" runat="server" GroupName="Survey" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Not applicable"> 
          <ItemTemplate> 
           <asp:RadioButton ID="rbNA" runat="server" GroupName="Survey" Checked="true" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
       </asp:gridview> 
      </td> 
     </tr> 
     <tr> 
      <td align="center"> 
       <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> 
      </td> 
     </tr> 
    </table> 
</div> 

我需要避免的if else我使用的上提交按鈕我也不能使用單選按鈕名單。請幫忙。我想要服務器端代碼。沒有jQuery或JavaScript。

+0

利用交換機..案例而不是If..ElseIf .. – 2014-09-23 08:49:30

回答

0

添加下面的腳本在頁面上

<script type="text/javascript"> 
function GetAge() { 
    var Row1 = $("input:radio[id$='rbSD']:checked").val(); 
    var Row2 = $("input:radio[id$='rbDS']:checked").val(); 
    var Row3 = $("input:radio[id$='rbNAND']:checked").val(); 
    var Row4 = $("input:radio[id$='rbAS']:checked").val(); 
    var Row5 = $("input:radio[id$='rbSA']:checked").val(); 
    var Row6 = $("input:radio[id$='rbNA']:checked").val(); 

} 
</script> 

添加 「的OnClientClick」 GetAge()」事件提交按鈕

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" OnClientClick"GetAge()" /> 
1

類似:

var selectedButton = Master.FindControl("grdSurvey").Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked); 
相關問題