asp.net store a cookie/session for checkbox - asp.net

I have a checkbox and I want to achieve that if I checked the checkbox, after reload the page, it's still checked (I don't have a value stored in the database).
The checkbox is
<asp:CheckBox ID="cb1" runat="server" />
The event about checkbox change it
Protected Sub cb1_CheckedChanged(sender As Object, e As EventArgs) Handles cb1.CheckedChanged
Dim SE_cb1 As Boolean = Nothing
SE_cb1 = If(cb1.Checked = True, 1, 0)
Session("SE_cb1") = SE_cb1
End Sub
The page load event is
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Session("SE_cb1") Is Nothing Then cb1.Checked = If(Session("SE_cb1") = 1, True, False)
end sub
I wonder why is that after I reload/redirect the page, the checked checkbox still dropped the check?
Thanks for any advice!

OK, when i change the load event to prerender, it seems OK now. Thanks for the help!
Handles Me.Load--> Handles Me.PreRender

Related

ASP.NET Add Command Argument to Button in Code Behind

I am trying to grab an argument added to an asp:button in code behind, but getting error:
Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.CommandEventArgs'
This button is part of a normal form, not a gridview, etc..
What might I be doing wrong? Regards.
<form><asp:Button ID="btnPrint" runat="server" Text="Print" CommandArgument="" /></form>
Imports System.Web
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim String AS String = "MyValue"
btnPrint.CommandArgument = String
End Sub
Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnPrint.Click
Dim ID as String = e.CommandArgument.toString
'Have also tried
'Dim btn As Button = sender
'Dim ID As String = btn.CommandArgument
Response.Redirect("Print.aspx?tid=" & ID)
End Sub
You just need to update btnPrint_Click to handle the Command event instead of "Click":
Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnPrint.Command
My ASP button click events look like this:
Private Sub SearchASPxButton_Click(sender As Object, e As EventArgs) Handles SearchASPxButton.Click
Notice the second parameter is of type System.EventArgs. Did you change the type of the automatically generated click event? Or did you create this manually yourself?

View state after postback VB?

I have a label's value that is posted back from a previous page and I want to use this label to do a simple calculation in the current page, but when I do the calculation the page (refreshed) the value of this label and automatically deleted the value (Since there would be no value in postback when it refreshed).
Here is the code behind file:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label2.Text = Request.Form("Hidden1")
End Sub
and here where I want to use the label
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Stotal As String
Stotal = Val(Label2.Text) * 10
Label3.Text = Stotal
End Sub
How can I save the value in the page via view state or any other method? Thanks in advance
Unless you've disabled ViewState on your page, the problem isn't that your label's ViewState isn't being saved, it's that you're overwriting it in your Page_Load method on the postback since the form variable Hidden1 is no longer being posted to your page from the previous page. Try:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
Label2.Text = Request.Form("Hidden1")
End If
End Sub

problem with asp textbox control

i have a textbox control in asp.net. textbox have a search button beside it. On clicking the search button i redirect to new page with the value in textbox. the new page also hase textbox and button beside it. I set the value sent from previous page to the textbox on new page. If i change the value on new page and click the search button it should take the new value. But it takes the previous value.
on page load method wrote the following code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End Sub
On button click following code
Protected Sub bsrcnew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsrcnew.Click
Dim s As String
s = txtsrch.Text
If (flag.Equals(0)) Then
Response.Redirect("newSearch.aspx?str1=" + s)
ElseIf (flag.Equals(1)) Then
Response.Redirect("termsnew.aspx?str1=" + s)
End If
end sub
can any1 tell me how do i get changed value in textbox?
Try assigning it in a
If(!IsPostBack)
{
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
}
On buttonclick its again assigning the value from query string.
Use IsPostBack to set your text box only on the first run in the second page. Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End If
End Sub

ASP User Control Issue

I am attempting to construct my own date picker using code from several sources.
Why won't the calendar hide when visible?
myDate.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="myDate.ascx.vb"
Inherits="Website.myDate" %>
<asp:TextBox ID="dateText" runat="server" > </asp:TextBox>
<asp:Button ID="dateBtn" runat="server" UseSubmitBehavior="false" Text="x" />
<asp:Calendar ID="dateCal" runat="server" ></asp:Calendar>
myDate.ascx.vb
Partial Public Class myDate
Inherits System.Web.UI.UserControl
Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
dateText.Text = dateCal.SelectedDate ' Update text box'
dateCal.Visible = False ' Hide calendar'
End Sub
Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
End Sub
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dateCal.Visible = False ' Hide calendar on load'
End Sub
Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
End Sub
End Class
It won't hide when visible because Page_Load runs every time the page is loaded including postbacks (button clicks etc).
So you need to use the IsPostBack property to set the visiblity in Page_load:
if Not Page.IsPostBack then
dateCal.Visible = False ' Hide calendar on load'
end if
Sometimes getting the initialization right in page_load with IsPostBack can lead to a mass of strange conditions so use it wisely.
I think I'd be inclined to set the property in the ascx file rather than in Page_load.
First of all, if you want to toggle controls between page postbacks, you need to use ViewState. Check, is your page uses ViewState.
EnableViewState for the page must be set to true in your case.
Also, check your Page_load function.
On every loading of the page you are hiding your calendar
Page_load is calling every time before any button or calendar even rises.
So, you are changing visibility to true and then using changed visibility value in events:
dateCal.Visible = Not dateCal.Visible
That's making calendar always be visible when you are clicking on dateBtn
To make it clear to you i will order events in your code in the order they are calling:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dateCal.Visible = False ' Hide calendar on load'
End Sub
Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
dateText.Text = dateCal.SelectedDate ' Update text box'
dateCal.Visible = False ' Hide calendar'
End Sub
Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
End Sub
Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
End Sub
Now you may see that every time when the page is loading, the page_load event is calling and hides the calendar.
You should either set Visible="false" in the ascx file for the calendar. Or call
dateCal.Visible = False
only when its first loading of the page (!IsPostback property)
so, in C# it will be
protected void Page_Load(object sender, EventArgs args) {
if (!IsPostback)
dateCal.Visible = false;
}

My Session Value is Not Changing

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = True Then
Session("x") = "ABC"
End If
End Sub
Protected Sub btnABC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnABC.Click
Session("x") = "ABC"
End Sub
Protected Sub btnCBA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCBA.Click
Session("x") = "CBA"
End Sub
Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
TextBox1.Text = Session("x")
End Sub
End Class
Three buttons-ABC,CBA and Show.
if you click on ABC and then Click on Show button The textbox shows "ABC"
but when I Clicking on CBA button And then Click on Show button The textbox shows again "ABC". IsPostback property will true on each time the page is posted to the server.
So the session reset the value.
how to overcome this issue ????
If you set the value in page_load(), this assignment occurs every time you load the page. Maybe you want to set this value only at the first call of the page:
If IsPostback = False Then
Session("x") = "Something"
End If
The second load of the page will not overwrite the value you set in button1_click.
When you press the show button it causes a postback to the server. The Page_Load method fires first, and you assign "ABC" into Session("x"). Then you're putting Session("x") into into the textbox.
What you'd probably want is this instead:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("x") = "ABC"
End If
End Sub
Besides what other people wrote above, I also recommend you to assign Session values during Page_InitComplete event. Because mostly developers work in Page_Load stage and some times assigning Session values as well may throw errors because of it. It is like which one is happening before or after. You can make mistakes and so on.

Resources