Link to home
Start Free TrialLog in
Avatar of JS List
JS ListFlag for United States of America

asked on

Check Select Checkboxes in Panel

There's a page that displays 1 case for edit.  Every case has a state or states hooked up to it.  There is a label that shows all the state(s) that are with that case.  The page also has a panel that displays a checkbox for all the US states.  When the user clicks on the Modify List button the panel shows.  I'm trying to get those state(s), attached to the case, to display with the checkboxes checked.  

Here's the aspx page:

<%@ Page Title="" Language="VB" MasterPageFile="~/Masters/mstrIntranet.master" AutoEventWireup="false" CodeFile="CaseModify.aspx.vb" Inherits="Tools_Case_Modify" MaintainScrollPositionOnPostBack="True"%>

<asp:Content ID="Content1" ContentPlaceHolderID="contentplaceholder2" Runat="Server">
        <form id="Form1" name="form1" method="post" runat="server">
    <table cellpadding="0" cellspacing="0" class="text" border="0"
        style="width: 693px;">
        <tr valign="top">
            <td style="width: 174px" >
                &nbsp;</td>
            <td height="24" colspan="3" >
                &nbsp;</td>
        </tr>
            <tr>
            <td style="width: 174px">States</td>
            <td colspan="3">
                <asp:Label ID="lblStates" runat="server" CssClass="text"></asp:Label>
                <asp:Button ID="btnModifyList" runat="server" Text="Modify List" />
            </td>
        </tr>
         <tr>
            <td colspan="4">
                <asp:Panel ID="pnlStates" runat="server">
                <asp:DataList ID="dlStates" runat="server" RepeatDirection="Horizontal" RepeatColumns="4"
                EnableViewState="True" Width="623px">
                   <ItemTemplate>
                      <asp:CheckBox ID="chkStateID" runat="server" Text='<%# Eval("State") %>' />
                    </ItemTemplate>  
                   </asp:DataList>
                    <div align="right">
                        <asp:Button ID="btnForeign" runat="server" Text="Show Foreign" />
                        &nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btnCloseStates" runat="server" Text="Hide States" />
                    </div>
                    <hr></hr>
                </asp:Panel> <p>                                                  
                    &nbsp;<asp:Panel ID="pnlStatesF" runat="server">
                <asp:DataList ID="dlStatesF" runat="server" RepeatDirection="Horizontal" RepeatColumns="4"
                EnableViewState="True" Width="655px">
                   <ItemTemplate>
                      <asp:CheckBox ID="chkStateID" runat="server" Text='<%# Eval("State") %>' />
                    </ItemTemplate>  
                   </asp:DataList><p><div align="right">
                <asp:Button ID="btnCloseForeign" runat="server" Text="Hide Foreign" align="right"/></div>
                </asp:Panel>
                &nbsp;</td>
        </tr>
        <tr>
            <td style="width: 174px">&nbsp;</td>
            <td colspan="2">&nbsp;</td>
            <td>&nbsp;<asp:Button ID="btnUpdate" runat="server" Text="Update" />
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
            </td>
        </tr>
    </table>
    </form>
</asp:Content>

Here's the aspx.vb page:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports AreaNameNumber
Imports Cases
Imports States_All

Partial Class Tools_Case_Modify
    Inherits System.Web.UI.Page
    Public dr As OleDbDataReader
    Public ds As DataSet

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim strSQL As String
        Dim dsStates As DataSet
        Dim dRow As DataRow

        If Not (Page.IsPostBack) Then
            Dim thisID, thisType As Integer
            thisID = Request.QueryString("j")
            lbl1.Text = thisID

            strSQL = "SELECT * "
            strSQL = strSQL + "FROM Cases"
            strSQL = strSQL + "WHERE (Cases.caseID = " & thisID & ")"

            ds = Cases.GetData(strSQL)

            dsStates = Cases.GetData("Select * FROM RelatedStates WHERE caseID = '" & thisID & "' ORDER BY RelatedState")

            '~~~~~~~~~~ Panel for States
            dlStates.DataSource = States_All.GetData("Select  *  From StateForeignCodes WHERE Foreign = False ORDER BY State")
            dlStates.DataBind()

            For Each dRow In dsStates.Tables(0).Rows
                lblStatesInvolved.Text = lblStatesInvolved.Text & dsStates.Tables(0).Rows(0).Item("relatedState") & ", "
            Next

            pnlStates.Visible = False
            btnForeign.Visible = False
            btnCloseStates.Visible = False

                     
            '~~~~~~~~~~~ Panel for Foreign States
            dlStatesF.DataSource = States_All.GetData("Select  *  From StateForeignCodes WHERE Foreign = True ORDER BY State")
            dlStatesF.DataBind()
            pnlStatesF.Visible = False

            dsAlerts = MajorCases.GetData("Select * FROM addlInfo WHERE caseID = " & thisID & "")
            For Each dRow In dsAlerts.Tables(0).Rows
                lblCaseAlerts.Text = lblCaseAlerts.Text & dsAlerts.Tables(0).Rows(0).Item("info") & ", "
            Next

        End If
    End Sub

    Protected Sub btnModifyList_Click(sender As Object, e As System.EventArgs) Handles btnModifyList.Click
        pnlStates.Visible = True
        btnForeign.Visible = True
        btnCloseStates.Visible = True
        btnModifyList.Visible = False

    End Sub

    Protected Sub btnForeign_Click(sender As Object, e As System.EventArgs) Handles btnForeign.Click
        pnlStatesF.Visible = True
        btnCloseForeign.Visible = True
    End Sub

    Protected Sub btnCloseForeign_Click(sender As Object, e As System.EventArgs) Handles btnCloseForeign.Click
        pnlStatesF.Visible = False
        btnCloseForeign.Visible = False
    End Sub

    Protected Sub btnCloseStates_Click(sender As Object, e As System.EventArgs) Handles btnCloseStates.Click
        btnModifyList.Visible = True
        btnCloseForeign.Visible = False
        btnCloseStates.Visible = False
        pnlStates.Visible = False

    End Sub
End Class

 User generated image2.JPG
Avatar of jagssidurala
jagssidurala
Flag of India image

I hope this code will helps you

foreach (DataListItem item in dlStates.Items) {
      CheckBox chkStateID= (CheckBox)item.FindControl("chkStateID");            
      if(chkStateID != null & chkStateID.checked)
      {
      }
}

based on the panel visibility you need to retrieve from different datalist.........
Avatar of JS List

ASKER

Thanks for the quick reply.  
This is where I pull the states that belong to a case.

dsStates = Cases.GetData("Select * FROM RelatedStates WHERE caseID = '" & thisID & "' ORDER BY RelatedState")
            For Each dRow In dsStates.Tables(0).Rows
                lblStatesInvolved.Text = lblStatesInvolved.Text & dsStates.Tables(0).Rows(0).Item("relatedState") & ", "
            Next

This is where I populate the panel.

dlStates.DataSource = States_All.GetData("Select  *  From StateForeignCodes WHERE Foreign = False ORDER BY State")
            dlStates.DataBind()

So in this case lets say only Iowa is attached to the case.  When the panel shows only want Iowa should be checked.  I think your example would check them all?
Are you storing the checked values in the Database. if so, while retrieving if matches(some where you have to joins for your tables) with saved data get true as another column(Lets say column name as stateChecked) in the result set and assign the same column to the checkbox in the datalist like below.

<ItemTemplate>
                      <asp:CheckBox ID="chkStateID" runat="server" Text='<%# Eval("State") %>'
Checked='<%# Eval("stateChecked") %>'
 />
                    </ItemTemplate>  
Avatar of JS List

ASKER

Yes the states that apply to the case are in a different table.  
If I set the panel of checkboxes all the states up first
When I do this:
 For Each dRow In dsStates.Tables(0).Rows
                lblStatesInvolved.Text = lblStatesInvolved.Text & dsStates.Tables(0).Rows(0).Item("relatedState") & ", "

            Next

Is there some type of findControl I can do?  The checkbox value would be the state abbreviation that's in the separate table.
ASKER CERTIFIED SOLUTION
Avatar of JS List
JS List
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JS List

ASKER

I found the solution myself