Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - CheckBoxList capture values into Sub Page_Init
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CheckBoxList capture values into Sub Page_Init

 Post Reply Post Reply
Author
 Rating: Topic Rating: 1 Votes, Average 5.00  Topic Search Topic Search  Topic Options Topic Options
webpaws View Drop Down
Newbie
Newbie
Avatar

Joined: 20 August 2008
Location: Boston
Status: Offline
Points: 7
Post Options Post Options   Thanks (0) Thanks(0)   Quote webpaws Quote  Post ReplyReply Direct Link To This Post Topic: CheckBoxList capture values into Sub Page_Init
    Posted: 21 August 2008 at 9:05am
I am trying to capture the values of my CheckBoxList items selected, capture the values into a MySQL statement and post into my Sub Page_Init on a re-page or postback to change the SQL statement (data in datalist).  I can capture the items, but not preserve them in the sub Page_Init, nor on a repage, I loose the selected items in the checkboxlist.

Here's part of my code:
                <p style="font-size:small;">
                <asp:Button ID="SubmitProdtype" OnClick="submit_prodtype" runat="server" Text="Show:" style="font-size:small; float:left;" />
                <asp:CheckBoxList
                    ID="CBL_ProductType"
                    runat="server"
                    RepeatDirection="Horizontal"
                    TextAlign="Left"
                    CellSpacing="3"
                    style="font-size:small; float:left; padding-right:10px;"
                 >
                   
                        <asp:ListItem Value="Wrapper" Text="Standard " />
                        <asp:ListItem Value="KingSize" Text="KingSize " />
                        <asp:ListItem Value="HalfPound" Text="HalfPound " />
                        <asp:ListItem Value="Mini" Text="Mini " />
                        <asp:ListItem Value="Label" Text="SplashBands " />
                        <asp:ListItem Value="MyMintos" Text="MyMintos " />
                        <asp:ListItem Value="Invitation" Text="Cards " />
                       
                    </asp:CheckBoxList>
                </p>

and the VB code behind:

    Sub submit_prodtype(ByVal sender As Object, ByVal e As EventArgs)
        Dim ckProduct As ListItem
        For Each ckProduct In CBL_ProductType.Items
            If ckProduct.Selected Then
                Dim ProdType As Object = CBL_ProductType.SelectedValue
                Dim ckProductVal As Object = ckProduct.Value
                prTySQL &= "Product.ProductType = '" & ckProductVal & "' Or "
                'Response.Write("init page sub prTySQL = " & prTySQL & "<br>")
                'Response.Write("init page sub ckProductVal = " & ckProductVal & "<br>")
            End If
        Next
        Response.Write("submit_prodtype sub prTySQL = " & prTySQL & "<br>")
    End Sub


    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim cnn As New MySqlClient.MySqlConnection(ConfigurationManager.ConnectionStrings("ConnectionStringMySQL").ConnectionString())
        Dim strSQL As String = ""


        Response.Write("Page_Init sub prTySQL = " & prTySQL & "<br>")


        'For Poduct Only Queries
        If Request.QueryString("product") <> "" And Request.QueryString("keyword") = "" Then
            strProduct = Request.QueryString("product")
            strProduct = strProduct.Replace("'", "") 'sql injection prevention
            strProduct = strProduct.ToLower
        ElseIf Request.QueryString("product") = "" And Request.QueryString("keyword") <> "" Then
            If Request.QueryString("keyword").Length = 2 Then
                strProduct = Request.QueryString("keyword")
                strProduct = strProduct.Replace("'", "") 'sql injection prevention
                strProduct = strProduct.ToLower

                strSQL &= "SELECT DISTINCT ProductId, Name, LongDescr, ProductType, Size "
                strSQL &= "FROM product "
                strSQL &= "WHERE ProductId REGEXP '" & strProduct & "' "
                strSQL &= "AND (product.Active = 'Y') "

                If Session("SiteID") = "100" Then
                    strSQL &= "AND ( Product.SiteID = '100' ) "
                Else
       &
Back to Top
jamie.townsend View Drop Down
Groupie
Groupie


Joined: 07 December 2007
Location: England
Status: Offline
Points: 114
Post Options Post Options   Thanks (0) Thanks(0)   Quote jamie.townsend Quote  Post ReplyReply Direct Link To This Post Posted: 21 August 2008 at 5:13pm
Perhaps to perserve placing the code in a if (!isPostBack) {}
Back to Top
webpaws View Drop Down
Newbie
Newbie
Avatar

Joined: 20 August 2008
Location: Boston
Status: Offline
Points: 7
Post Options Post Options   Thanks (0) Thanks(0)   Quote webpaws Quote  Post ReplyReply Direct Link To This Post Posted: 21 August 2008 at 6:20pm
I have tried putting the Sub submit_prodtype code in a

IF Not Page.IsPostBack Then
  ...perform code
End If

But this doesn't get the variable prTySQL to properly post into the Sub Page_Init.  Is there a ViewState or Session command to preserve the value of the variable?  I also think the Sub Page_Init is occurring prior to the Sub submit_prodtype code and therefor the variable would not be populated yet?
Back to Top
jamie.townsend View Drop Down
Groupie
Groupie


Joined: 07 December 2007
Location: England
Status: Offline
Points: 114
Post Options Post Options   Thanks (0) Thanks(0)   Quote jamie.townsend Quote  Post ReplyReply Direct Link To This Post Posted: 22 August 2008 at 8:02am
Please see here for the page life cycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx
 
The Page_Init will fire before anything else including page_load, see the link above for clarification.
 
Back to Top
webpaws View Drop Down
Newbie
Newbie
Avatar

Joined: 20 August 2008
Location: Boston
Status: Offline
Points: 7
Post Options Post Options   Thanks (0) Thanks(0)   Quote webpaws Quote  Post ReplyReply Direct Link To This Post Posted: 27 August 2008 at 6:50pm
I found a solution by putting my code into Page_Load and using a URL Variable if no change occurs in the CheckBoxList to preserve their state.  First (if there are state changes in the CheckBoxes, I count them and then re-iterate their condition to the page as it is loading to keep their state on PostBack (CheckBoxList_ID.Items(j).Selected = True).  Second, if no change is found, I use a URL variable to determine the last state and keep the correct boxes checked.  Another way to do this is with ViewState or Session, but I found this more consistent with the rest of my code.

The VB code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Keeps Search Box text persistent

        'Determine which Products are selected to check appropriate checkboxlist listitem
        Dim selectedCount As Integer = 0
        For i As Integer = 0 To CBL_ProductType.Items.Count - 1
            If CBL_ProductType.Items(i).Selected Then
                selectedCount += 1
            End If
        Next
        If selectedCount > 0 Then
            'Reset Selected Items to All
            'CountxFour = 1111111
            For j As Integer = CBL_ProductType.Items.Count - 1 To 0 Step -1
                If CBL_ProductType.Items(j).Selected = True Then
                    'CountxFour = CountxFour + CBL_ProductType.Items(j).Value
                    CBL_ProductType.Items(j).Selected = True
                End If
            Next j
        Else
            If Not Request.QueryString("pt") Is Nothing And Request.QueryString("pt") <> "" Then
                If Request.QueryString("pt") > 1111111 Then
                    If IsNumeric(Request.QueryString("pt")) Then
                        Dim chk As Integer = Request.QueryString("pt")
                        'CountxFour = 1111111
                        If Mid(chk, 1, 1) >= 2 Then
                            CBL_ProductType.Items(0).Selected = True
                            'CountxFour = CountxFour + CBL_ProductType.Items(0).Value
                        End If
                        If Mid(chk, 2, 1) >= 2 Then
                            CBL_ProductType.Items(1).Selected = True
                            'CountxFour = CountxFour + CBL_ProductType.Items(1).Value
                        End If
                        If Mid(chk, 3, 1) >= 2 Then
                            CBL_ProductType.Items(2).Selected = True
                            'CountxFour = CountxFour + CBL_ProductType.Items(2).Value
                        End If
                        If Mid(chk, 4, 1) >= 2 Then
                            CBL_ProductType.Items(3).Selected = True
                            'CountxFour = CountxFour + CBL_ProductType.Items(3).Value
                        End If
                        If Mid(chk, 5, 1) >= 2 Then
                            CBL_ProductType.Items(4).Selected = True
                            'CountxFour = CountxFour + CBL_ProductType.Items(4).Value
                        End If
                        If Mid(chk, 6, 1) >= 2 Then
                            CBL_ProductType.Items(5).Selected = True
                            'CountxFour = CountxFour + CBL_ProductType.Items(5).Value
                        End If
          &nbs
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.