After a PostBack caused by ddlPlant_SelectedIndexChanged, I need to set set HttpContext.Current.Session("PlantNumber"). This needs to happen after ddlPlant loads in Site.Master, but before the code in Default.aspx needs the value.
Public Class SiteMaster Inherits MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
If (Not Page.IsPostBack) Then
ddlPlant.DataSource = myDataSource
ddlPlant.DataBind()
ddlPlant.SelectedValue = "1"
End If
HttpContext.Current.Session("PlantNumber") = ddlPlant.SelectedValue
End Sub
Protected Sub ddlPlant_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPlant.SelectedIndexChanged
End Sub
End Class
Public Class _Default Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim units As New List(Of EquipmentModel)
For Each unit As EquipmentModel In getUnits.Out.Results
If (CStr(unit.Plant_ID) = HttpContext.Current.Session("PlantNumber")) Then
units.Add(unit)
End If
Next
gvEquipmentUnit.DataSource = units.OrderBy(Function(n) n.Equipment_ID)
gvEquipmentUnit.DataBind()
End Sub
With the code above, when Session("PlantNumber") is set after PostBack, ddlPlant.SelectedIndex = Nothing, and ddlPlant.SelectedValue is an empty string.
I've tried moving the Session("PlantNumber") = ddlPlant.SelectedValue line to Site.Master's Page_Load instead, but that runs after it is needed in Default.aspx.vb
I looked up PreLoad, but apparently it doesn't work for the Master page.
Ultimately, I decided not to use a Session variable, and instead call the control directly from any page that needs that value on load (almost all of them):
DirectCast(Master.FindControl("ddlPlant"), DropDownList).SelectedValue
Related
i want to display text box value to another text box from one webpage to another in a button click.
i know the windows code But don't know the web application.
public qus as form = new question()
qus.txtname=txtname.text
On first page add button click event
Sub Btn_Click(sender As Object, _
e As EventArgs)
Response.Redirect("SecondPage.aspx?id=" + txtname.text, false)
End Sub
on second page set your text on page load.
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
Use the query string to pass the text value to other page in the button click handler, like this:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("OtherPage.aspx?textValue='Value from other page'")
End Sub
Now in the Page_Load of the other page, pull the query string value out and assign it to the text box, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.QueryString("textValue") IsNot Nothing Then
YourTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
You have to write code on first page
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("webpage2.aspx?textValue='value'")
End Sub
On second page
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
I know this is a very basic question, but I cannot find the answer. There are lots of web pages that say page.init does not fire on a post back e.g. here: http://www.dotnetfunda.com/interview/exclusive/x3224-what-is-the-difference-between-the-pageinit-and-pageload-events.aspx. Please see the code below:
Public Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
MsgBox("Test Init") 'Line 5
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = True Then
MsgBox("PostBack") 'line 9
End If
MsgBox("Test Load")
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello"
End Sub
End Class
The message box on line 5 and line 9 fire every time I click the button. This means that the Init event is fired on a postback. I have obviously forgotten something very basic.
From your reference page:
When you postback to any page, the Page_Init event doesn't fire.
This is totally wrong.
Page_Init is always fired - actually the page cycle is not change at all.
I'm trying to make a form that when it is return to, it will have the same state for the controls
I have 3 .net pages, testa,testb,testc
testa, sets up the session and has a button to go to testb. testb has a check box and a button to go to testc. testc has a button to return to page testb.
I want it so when we leave testc, and then come back the check mark will be in the same state, but it is not. it stays as uncheck when I go from page testc back to page testb
This is what i did,
On the page load event, I set the check box equal to the "button" Session variable. On the button event I save the button checked state in the Session variable and call page testc
Code
Page A
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Server.Transfer("testb.aspx")
End Sub
Page b
Partial Class testb
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim t As Boolean
t = Session.Item("button")
CheckBox1.Checked = t
End Sub
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim t As Boolean
t = CheckBox1.Checked
Session.Item("button") = t
Server.Transfer("testc.aspx")
End Sub
End Class
Page c
Partial Class testc
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Server.Transfer("testb.aspx")
End Sub
End Class
Data in the Session is stored as an Object. What you'll need to do is get the value from the Session and then convert it to a Boolean if it's not null (Nothing in VB). We've used this technique before:
Dim rawValue As Object = Session.Item("button")
Dim realValue as Boolean
If rawValue IsNot Nothing Then
realValue = Boolean.Parse(rawValue.ToString())
End If
It sounds like you need to check the IsPostBack property in the Page_Load method to decide if the values should be loaded or not. When it's a postback, don't load from the Session.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim t As Boolean = Session.Item("button")
CheckBox1.Checked = t
End If
End Sub
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
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.