0

我想使用 GridView 生成复选框字段,系统将根据 totalDay 的数量计算要显示的字段数。如果数据库中存储的totalDay是7,那么调用eventID时会生成7个checkbox,D1-D7 checkboxes字段会出现;如果 totalDay 为 5,它将生成 5 个复选框,将出现 D1-D5 复选框字段。

在 PHP 中,我设法根据值 totalDay 自动生成我想要的 D 字段数。但我不知道如何在 VB 中编写代码。

PC Fair Event 为期 7 天,因此在用户选择要预订的活动和展位后,它将显示 7 个复选框: 在此处输入图像描述

嘉年华 2012 的持续时间为 5 天,因此在用户选择要预订的活动和展位后,它将显示 5 个复选框: 在此处输入图像描述

PHP中可以生成复选框的代码:

$query = "select * from booths, eventinfo where booths.eventID=eventinfo.eventID && booths.eventID = ".$id."";

$_SESSION['EVENT_ID']=$id;
$result = mysql_query($query);
$result2= mysql_query($query);


echo "<table border='0' style='width:400px;table-layout:fixed'>";


$rows2 = mysql_fetch_array($result);
$Day=$rows2['totalDay'];
echo "<table>";
for ($day = 0; $day <= $Day; ++$day) {
    if($day==0){
        echo "<th>Booth</th>";
    }else{
         echo "<th>D".$day."</th>";
    }
}

while($rows = mysql_fetch_array($result2)){
    $boothAlias=$rows['boothAlias'];
    $totalDay=$rows['totalDay'];
    $boothID=$rows['boothID'];


    echo "<tr><td>$boothAlias</td>";
    for ($day2 = 1; $day2 <= $totalDay; ++$day2) {

        $d=$rows["D$day2"];

        if ($d==0){
            echo "<td><input name='totalDay[]' type='checkbox' value='$boothAlias $boothID $day2'/></td>";

        }else{
            echo "<td><input name='totalDay[]' type='checkbox' value='$boothAlias $day2' disabled='true'/></td>";
            }
        }
        echo "</tr>";
}

echo "</table>";
}
}

我不知道如何调用 totalDay 值并循环检查 VB 中的 D 字段。有没有更好的解决方案来生成复选框?可以只重用一个itemtemplate来生成checkbox并绑定D字段吗?

以下是我当前的 VB 代码:

<%@ Page Language="VB" MasterPageFile="~/MasterPageMember.master" AutoEventWireup="false" CodeFile="member_view_event_list.aspx.vb" Inherits="member_view_event_list" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">



</asp:Content>

<asp:Content ID="ContentPlaceHolder2" runat="server" 
contentplaceholderid="ContentPlaceHolder2">



<table width="80%">
    <tr><td class="style10">&nbsp;</td><td class="style8">&nbsp;</td><td>&nbsp;</td></tr>
    <tr><td class="style10">
        <asp:Label ID="Label1" runat="server" Text="Select Event:"></asp:Label>
    </td><td class="style8">
        <asp:DropDownList ID="ddlEventList" runat="server" 
            DataSourceID="SqlDataSourceEvent" DataTextField="eventTitle" 
            DataValueField="eventID" AutoPostBack="True" Width="200">

        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSourceEvent" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
            SelectCommand="SELECT DISTINCT [eventTitle], [eventID] FROM [booth_eventinfo]">
        </asp:SqlDataSource>
    </td><td>
            &nbsp;</td></tr>


    <tr><td class="style10">
        <asp:Label ID="Label2" runat="server" Text="Select Booth:"></asp:Label>
        </td><td class="style8">
        <asp:DropDownList ID="ddlBoothList" runat="server" 
            DataSourceID="SqlDataSourceBooth" DataTextField="boothAlias" 
            DataValueField="boothID" AutoPostBack="True" Width="200">

        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSourceBooth" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
            SelectCommand="SELECT [boothAlias], [boothID] FROM [booth_eventinfo] WHERE ([eventID] = @eventID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlEventList" Name="eventID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        </td><td>
             </td></tr>


    <tr><td class="style10">
        &nbsp;</td><td class="style8">
            &nbsp;</td><td>
             &nbsp;</td></tr>


    <tr><td class="style9" colspan="3">

        <asp:GridView ID="GridViewDay" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" DataSourceID="SqlDataSourceDay" ForeColor="#333333" 
            GridLines="None">
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <Columns>

                <asp:TemplateField HeaderText="Day1" SortExpression="D1" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("D1") %>' />
                </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Day2" SortExpression="D2" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("D2") %>' />
                </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Day3" SortExpression="D3" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Bind("D3") %>' />
                </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Day4" SortExpression="D4" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Bind("D4") %>' />
                </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Day5" SortExpression="D5" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Bind("D5") %>' />
                </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Day6" SortExpression="D6" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox6" runat="server" Checked='<%# Bind("D6") %>' />
                </ItemTemplate>
                </asp:TemplateField>




            </Columns>


            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSourceDay" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
            SelectCommand="SELECT [D1], [D7], [D6], [D5], [D4], [D3], [D2] FROM [booth_eventinfo] WHERE ([boothID] = @boothID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlBoothList" Name="boothID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        </td></tr>


    <tr><td class="style10">
        &nbsp;</td><td class="style8">
            &nbsp;</td><td>
            &nbsp;</td></tr>

4

1 回答 1

2

我认为您可以更轻松地使用CheckBoxList控件:

<asp:CheckBoxList ID="CheckList1" runat="server" 
    RepeatLayout="Table" 
    RepeatDirection="Horizontal" 
    RepeatColumns="7" 
    DataTextField="DayName" 
    DataValueField="DayNumber">
</asp:CheckBoxList>                  

然后,在代码隐藏中,您只需绑定列表:

Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not Page.IsPostBack Then
        CheckList1.DataSource = GetData()
        CheckList1.DataBind()
    End If
End Sub                                   
于 2012-05-09T14:44:21.820 回答