mcheng
mcheng

Reputation: 153

How to generate checkbox field according to integer value in database?

I want to generate checkbox field using GridView and system will calculate how many field to display according to number of totalDay. If totalDay stored in database is 7, so upon calling the eventID it will generate 7 checkboxes, D1-D7 checkboxes field will appear; if totalDay is 5, it will generate 5 checkboxes, D1-D5 checkboxes field will appear.

In PHP, I manage to auto generate number of D field I want according to value totalDay. But I do not know how to code in VB.

The PC Fair Event has duration 7 days, so it will display 7 checkboxes after user select event and booth to book: enter image description here

The Carnival 2012 has duration 5 days, so it will display 5 checkboxes after user select event and booth to book: enter image description here

The code in PHP which can generate the checkbox:

$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>";
}
}

I do not how to call the totalDay value and do the looping to check D field in VB. Is there any better solution to generate checkbox? Can it reused only one itemtemplate to generate checkbox and bind with D field?

Following is my current VB code:

<%@ 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>

Upvotes: 0

Views: 940

Answers (1)

James Johnson
James Johnson

Reputation: 46067

I think you'd have a much easier time using the CheckBoxList control:

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

Then, in the code-behind, you just bind the list:

Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not Page.IsPostBack Then
        CheckList1.DataSource = GetData()
        CheckList1.DataBind()
    End If
End Sub                                   

Upvotes: 2

Related Questions