1

我有包含 60 行的 gridview,每行有 4 个单选按钮选项(选择任何一个)。要操作该代码,在提交按钮上编写代码如下。我没有工作。我的设计观点:

<asp:GridView ID="gvSurvey" runat="server" CellPadding = "4"  OnRowDataBound ="gvSurvey_RowDataBound" BorderWidth ="2"
AutoGenerateColumns="False" EmptyDataText="No data Available" GridLines="None"
HorizontalAlign="Center" ForeColor="#333333" Font-Names="Verdana" ShowFooter="True">
<RowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="HEADER" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="QTN_NO" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SQTN_NO" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField Visible="true" HeaderText ="Strongly Disagree">
<ItemTemplate>

<asp:RadioButton ID="rdbtn1" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
  <%--<input name="MyRadioButton1" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Agree">
<ItemTemplate>
  <asp:RadioButton ID="rdbtn2" runat ="server"  GroupName ="MyRadioGroup"/>
  <%--<asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
 <%--<input name="MyRadioButton2" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Disagree">
<ItemTemplate>
  <asp:RadioButton ID="rdbtn3" runat ="server" GroupName ="MyRadioGroup"/>
  <%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
  <%--<input name="MyRadioButton3" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Strongly Agree">
<ItemTemplate>
  <asp:RadioButton ID="rdbtn4" runat ="server"  GroupName ="MyRadioGroup"/>
      <%--  <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%-- <input name="MyRadioButton4" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>

</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />

</asp:GridView>

C#

 for (i = 0; i < gvSurvey.Rows.Count; i++)
            {
                RadioButton rdbtn1 = (RadioButton)gvSurvey.Rows[i].Cells[3].FindControl("rdbtn1");
                RadioButton rdbtn2 = (RadioButton)gvSurvey.Rows[i].Cells[4].FindControl("rdbtn2");
                RadioButton rdbtn3 = (RadioButton)gvSurvey.Rows[i].Cells[5].FindControl("rdbtn3");
                RadioButton rdbtn4 = (RadioButton)gvSurvey.Rows[i].Cells[6].FindControl("rdbtn4");


          //      RadioButton rdbtn1=(RadioButton) gvSurvey.Rows[i].ClientID  

                qtn_no = dtSurvey.Rows[i]["QTN_NO"].ToString();
                sqtn_no = dtSurvey.Rows[i]["SQTN_NO"].ToString();

                if (rdbtn1.Checked)
                {
                    rdbtn1.Text = "1";
                    lblMsg.Text = nominationsBiz.SaveSuggestion(ticketNo.ToString(), qtn_no, sqtn_no, rdbtn1.Text);
                }
4

1 回答 1

2

请通过以下代码,您将找到解决方案

 //  It will be on your submit button click 
    protected void getPinCode()
    {
        foreach (GridViewRow grdRows in gvSurvey.Rows)
        {
            RadioButton rbt1 = (RadioButton)grdRows.FindControl("rdbtn1");
            RadioButton rbt2 = (RadioButton)grdRows.FindControl("rdbtn2");
            RadioButton rbt3 = (RadioButton)grdRows.FindControl("rdbtn3");
            RadioButton rbt4 = (RadioButton)grdRows.FindControl("rdbtn4");

            string Value = RadioValue(rbt1);
           //Similarly you can do for all radio button 
            if(!string.IsNullOrEmpty(Value))
            {

                lblMsg.Text = nominationsBiz.SaveSuggestion(ticketNo.ToString(), qtn_no, sqtn_no, Value);
            }

        }
    }


    protected string RadioValue(RadioButton Rbtlst)
    {
        string Value = "";

        if (Rbtlst.Checked == true)
        {
            Value = Rbtlst.Text;
        }
        return Value;
    }
于 2012-05-10T10:25:45.333 回答