I have a button that sets a session variable when clicked. But for some reason, I have to click it twice in order for the save to actually happen. Is there anyway around this?
Thanks
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If CInt(Session("save")) <> 1 Then
'save something ...
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
Session("save") = 1
End Sub
Page_Load runs before btnSave_Click. You can see more information about the ordering of events in MSDN.
In other words, when btnSave is clicked, the postback runs the Page_Load then the btnSave_Click method. To fix this problem, move the code 'save something ... into the btnSave_Click method.
Related
Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.ID = "but"
AddHandler but.Click, AddressOf Button_Click
Me.form1.Controls.Add(but)
End Sub
The event control for this is as below.
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Im getting the error
Multiple controls with the same ID '1' were found
The subroutine createbutton works on page load as follows.
Public Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
createbutton()
End Sub
Help is appreciated , Thanks :)
i think you are a guy who worked a lot with VB 6.0
Control array feature is available in VB 6.0
in VB.net everything is depend upon control ID.
if you don't have specific requirement like ID should be same then i suggest pls append prefix and incremental ID would resolve your issue.
Let me know if you have some specific requirements
Thanks
Asif
Corrected Code, Instead of ID it's name which allow uniqueness
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createbutton()
End Sub
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.Name = "but"
AddHandler but.Click, AddressOf Button_Click
Me.Controls.Add(but)
End Sub
End Class
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.
Is there any reason why an if then statement would fire regardless:
Its inside my Page_load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim a As New Clicks
If Request.QueryString("u") IsNot Nothing Then
a.Click(Request.QueryString("u"), Request.ServerVariables("REMOTE_ADDR"), Request.ServerVariables("HTTP_USER_AGENT"))
Response.Redirect(urls.GetURL(Request.QueryString("u")))
End If
End Sub
As a result, I am get 126 instances of a.Click per page load when "u" isnt present?
I have tried to move to other Page Events but same result
Created a workaround by encasing if statement in the original if statement to capture the unwanted items.
Not the perfect solution but is working
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.