在vb.net中使用RadioButtonList控件嵌套GridView

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (34)

我有2个GridViews相互嵌套,带有单选按钮列表控件。第一个网格视图的单选按钮列表包含列表项,如是,否,N / A. 单击“否”列表项时,它应显示嵌套的网格视图及其关联的单选按钮。

当页面加载时我可以看到所有网格视图及其相关的radiobutton,但是当点击“no”单选按钮时gridview2不会显示和隐藏。我使用javascript来实现该功能但不能按预期工作。我提供我的vb.net代码和aspx代码。任何帮助表示赞赏。

Vb.net代码

公共类_Default

Inherits Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Not IsPostBack Then

        GridView1.DataSource = GetData("select * from Questions")

        GridView1.DataBind()


    End If

End Sub

Private Shared Function GetData(query As String) As DataTable

    Dim strConnString As String = ConfigurationManager.ConnectionStrings("String").ConnectionString

    Using con As New SqlConnection(strConnString)

        Using cmd As New SqlCommand()

            cmd.CommandText = query

            Using sda As New SqlDataAdapter()

                cmd.Connection = con

                sda.SelectCommand = cmd

                Using ds As New DataSet()

                    Dim dt As New DataTable()

                    sda.Fill(dt)

                    Return dt

                End Using

            End Using

        End Using

    End Using

End Function



Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim questid As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()

        Dim GridView2 As GridView = TryCast(e.Row.FindControl("GridView2"), GridView)

        GridView2.DataSource = GetData(String.Format("SELECT * from Standards where Questions= '{0}'", questid))

        GridView2.DataBind()

    End If

End Sub

结束类

Aspx

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" CssClass="Grid"
 DataKeyNames="QstnID" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Question" HeaderText="Questions" />  
   <asp:TemplateField>
    <ItemTemplate> 
            <asp:RadioButtonList ID="QuestList" runat="server" DataValueField="Question" RepeatDirection="Horizontal">
              <asp:ListItem Enabled="False" Style="display: none" Text="" Value="0"></asp:ListItem>
              <asp:ListItem Value="Yes">Yes</asp:ListItem>
              <asp:ListItem Value="No">No</asp:ListItem>
              <asp:ListItem Value="N/A">N/A</asp:ListItem>
             </asp:RadioButtonList>
        <br> 
       </br>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid" GridLines="None">
            <Columns>
                <asp:BoundField DataField="Description" HeaderText="Standards" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:RadioButtonList ID="StandList" runat="server" DataValueField="Description" RepeatDirection="Horizontal">
                            <asp:ListItem Enabled="False" Style="display: none" Text="" Value="0"></asp:ListItem>
                            <asp:ListItem Value="Yes">Yes</asp:ListItem>
                            <asp:ListItem Value="No" Selected="False">No</asp:ListItem>
                            <asp:ListItem Value="N/A">N/A</asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br>
        </br>

    </ItemTemplate>
   </asp:TemplateField>
  </Columns>
</asp:GridView> 

<asp:SqlDataSource ID="Response" runat="server" ConnectionString="<%$ ConnectionStrings:String1%>" SelectCommand="SELECT [Description] FROM [Standards]"></asp:SqlDataSource>
<asp:SqlDataSource ID="WcmQC" runat="server" ConnectionString="<%$ ConnectionStrings:String2 %>" SelectCommand="SELECT [Question] FROM [Questions]"></asp:SqlDataSource>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
$(document).ready(function () {
        $('#QuestList input').click(function () {
            var value = $('#QuestList input:checked').val();
            if (value == "Yes") {
                $("#GridView2").show();
             else {
                 $("#GridView2").hide();  
            }
            }
        });
    });
</script>

</asp:Content>
提问于
用户回答回答于

在你的JS中你使用id“QuestList”:

$('#QuestList input').click(function () {

由于它是服务器控件,因此它将具有由“QuestList”组成的客户端ID,前面是容器名称的层次结构。它也在转发器/网格内,这进一步使其id复杂化。

而不是id,使用类来定位您的列表。像这样的东西:

<asp:RadioButtonList ID="QuestList" CssClass="MyListClass" runat="server" DataValueField="Question" RepeatDirection="Horizontal">

$('.MyListClass input').click(function () {

扫码关注云+社区

领取腾讯云代金券