Solved

Check Select Checkboxes in Panel

Posted on 2011-09-26
6
268 Views
Last Modified: 2012-05-12
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

 Before with label2.JPG
0
Comment
Question by:jshesek
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36600554
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.........
0
 
LVL 1

Author Comment

by:jshesek
ID: 36600917
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?
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36600979
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>  
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:jshesek
ID: 36601555
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.
0
 
LVL 1

Accepted Solution

by:
jshesek earned 0 total points
ID: 36711517
Found the answer:  The database that holds the case states has it as abbreviated.  The checkboxes have the full state name.  So they never match to check off.  
What I did was add the state abbreviation in at the end enclosed in ().  Then pulled out the abbreviation to check for a match.  

Here's the code from the aspx page:
 <asp:DataList ID="dlStates" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" EnableViewState="True" Width="682px">
       <ItemTemplate>
          <asp:CheckBox ID="chkStateID" runat="server" Text='<%# Eval("State") & " (" & Eval("State_ID") & ")"%>' />
        </ItemTemplate>  
  </asp:DataList>

Here's the code from the aspx.vb page:dlStates.DataBind()
 Dim checkBox As CheckBox
 Dim x As String

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

     For Each dlitme In dlStates.Items
         checkBox = DirectCast(dlitme.FindControl("chkStateID"), CheckBox)

         x = Right(checkBox.Text, 3)
         x = Left(x, 2)

         If (x = dRow.Item("relatedState")) Then
             checkBox.Checked = True
         End If
     Next
 Next
0
 
LVL 1

Author Closing Comment

by:jshesek
ID: 36935222
I found the solution myself
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get combo value in class 5 14
System.net.Sockets Error 5 26
Name Space error VS2015 1 22
SSRS Deployment problem 5 64
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question