CheckBoxList capture values into Sub Page_Init
Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=26145
Printed Date: 28 March 2026 at 9:03am Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com
Topic: CheckBoxList capture values into Sub Page_Init
Posted By: webpaws
Subject: CheckBoxList capture values into Sub Page_Init
Date 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 &
------------- Anyone who has never made a mistake has never tried anything new. - Albert Einstein
|
Replies:
Posted By: jamie.townsend
Date Posted: 21 August 2008 at 5:13pm
|
Perhaps to perserve placing the code in a if (!isPostBack) {}
|
Posted By: webpaws
Date 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?
------------- Anyone who has never made a mistake has never tried anything new. - Albert Einstein
|
Posted By: jamie.townsend
Date Posted: 22 August 2008 at 8:02am
Please see here for the page life cycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx - 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.
|
Posted By: webpaws
Date 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
------------- Anyone who has never made a mistake has never tried anything new. - Albert Einstein
|
|