display user input after pressing button - asp.net

I have a total of 4 views. in view 1 the user inputs their first name then clicks a button to go into the next view
<lable for="fname">First Name</lable>
<asp:TextBox ID="fname" runat="server" required></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" style="color:red" ErrorMessage="* Letters Only" ControlToValidate="fname" ValidationExpression="^[aA-zZ]*$" ></asp:RegularExpressionValidator>
i want to re-display the user's input on the 3rd view. this is my code for it
<asp:Label ID="cardName" runat="server" Text="Name On Card"></asp:Label>
<asp:Label ID="Label1" runat="server" ></asp:Label>
vb for it
Private Sub View3_Activate(sender As Object, e As EventArgs) Handles View3.Activate
Label1.Text = fname.Text
End Sub
this does not work, so i tried this
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Label1.Text = fname.Text
End Sub
(when button three is clicked, the page goes to view 3 from view 2. button type is not submit)
None of this has worked for me

You can use ASP.NET Session to store the value the user entered and then pull that value out of Session when you want to display it on view 3.
In button click event handler on view 1, do this:
Protected Sub View1_ButtonClick(sender As Object, e As EventArgs) Handles Button1.Click
Session("FirstName") = fname.Text
End Sub
Now in the Page_Load of view 3, do this:
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
' Get first name value out of Session by name here and set label
Label1.Text = Session("FirstName").ToString()
End Sub
Note - Things you put into Session are of type Object so you must cast the value to the correct type. In this case, the .ToString() makes it a String that can be put as the text of the label.

Related

vb.net button click event not firing first time

I have a very simple Textbox and Button, but Button click event not firing at first time ( only firing in second click)
<asp:TextBox ID="TxtSurvey" runat="server"></asp:TextBox> <br />
<asp:Button ID="BtnGoSurvey" runat="server" Text="LetsGo"/>
The code is very simple too
Protected Sub BtnGoSurvey_Click(sender As Object, e As EventArgs) Handles BtnGoSurvey.Click
Session("IDSurvey") = TxtSurvey.Text
BtnGoSurvey.PostBackUrl = "~/Survey.aspx"
End Sub
What i'm doing wrong ?
Thanks for help
If you can use Response.Redirect, then following is supposed to work:
Protected Sub BtnGoSurvey_Click(sender As Object, e As EventArgs) Handles BtnGoSurvey.Click
Session("IDSurvey") = TxtSurvey.Text
Response.Redirect("~/Survey.aspx",True)
End Sub

ASP.NET: Unable to retrieve values for controls located in a panel after postback

I've placed some controls in a panels. When the page postback, I'm trying to retrieve the posted values, but only the older value seems to be there.
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim _txtFName As TextBox = FindControl("editNamePanel").FindControl("txtFName")
Dim _txtMName As TextBox = FindControl("editNamePanel").FindControl("txtMName")
Dim _txtLName As TextBox = FindControl("editNamePanel").FindControl("txtLName")
End Sub
Even when I hover over the e EventArgs is null. Am I missing something?
EDIT
I'm getting new values when I put the above code in the page load event handler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Page.IsPostBack Then
'The above code here ...
End If
End Sub
Thank for helping
In your aspx
<asp:Panel ID="editNamePanel" runat="server">
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
In your code behind
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim _txtFName As TextBox = txtFName
Dim _txtMName As TextBox = txtMName
Dim _txtLName As TextBox = txtLName
End Sub

Hide control in Page_Load

I am using label control in form designing.But i want to hide that particular label control on page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label10.Visible = False
End Sub
What should i do? I'm new to vb.net.
Try setting the visibility property within the aspx code rather than within the load event, this may solve your issue.
<asp:Label ID="lblValidation" runat="server" BackColor="Red"
Text="Please fill in all of the date fields below to proceed" Visible="False"></asp:Label>
Hope this helps!

Radio button checked changed event in asp.net

I have 2 radio buttons on a simple web page and If I click one of them the other should be false(i.e checked=false) but it's not working Can anyone point me the mistake I'm doing.I knew it's a silly one but I need to know what's going on?
Here are the radio buttons:
<asp:RadioButton ID="Rb1" runat="server" Text=""/>
<asp:RadioButton ID="Rb2" runat="server" Text=""/>
Onchecked changed event:
Protected Sub Rb1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rb1.CheckedChanged
Rb2.Checked = False
End Sub
Protected Sub Rb2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rb2.CheckedChanged
Rb1.Checked = False
End Sub
You have to set GroupName property and AutoPostBack=True.

Addhandler, button.click not firing using VB.NET

I am experiencing a problem with buttons and AddHandler. It only works if I use AddHandler Button1.click, AddressOf... in Page_load, but if I create the button dynamically in one of the sub routines, the event doesn't fire.
For example,
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
<asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<contenttemplate>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
</contenttemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="UpdatePanel2" runat="server" UpdateMode="Conditional">
<contenttemplate>
<asp:Label id="Label2" runat="server" Text="Label"></asp:Label>
</contenttemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Label1.Text = Date.Now
ScriptManager1.RegisterAsyncPostBackControl(DropDownList1)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label2.Text = "Panel refreshed at " + Date.Now.ToString()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim b As New Button
b.Text = "Click"
ScriptManager1.RegisterAsyncPostBackControl(b)
AddHandler b.Click, AddressOf Button1_Click
PlaceHolder1.Controls.Add(b)
UpdatePanel1.Update()
End Sub
The dropdownlist works, but the button doesn't. What am I doing wrong?
You have to regenerate your dynamically created controls on every postback (at last in Page_Load, better in Page_Init). You have to set the ID of the controls accordingly because ASP.Net needs it to identify which control caused a Postback and to handle the appropriate events.
You could save the number of created buttons in ViewState and use this to regenerate them on Page_Load. Increase the number when you add a new button. Use this number also to make the Button's ID unique(append it to the ID) to ensure that its the same on every postback.
For further informations, have a look the Page-Lifecycle and ViewState with dynamically added controls.
Edit: As Joel commented, if you only need one Button you can set it's ID statically, but you have to regenerate it on postback f.e. to handle its click-event.
Just to aid anyone who has this problem and isn't quite sure how to implement. Here's a quick example.
This example starts out by displaying a dropdownlist. When user selects something from the dropdown, another dropdownlist appears.
I typed this off the top of my head, so it MAY contain errors, but you get the idea =)
In the aspx file, add a placeholder:
And in your codebehind:
...
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'Store control count in viewstate
If Not IsPostBack Then ViewState("ControlCounter") = 1
'Rebuild dynamic controls on every postback so when page's life cycle hits Page_Load,
'selected values in the viewstate (asp.net default behavior) can be loaded into the dropdowns
Build_Dynamic_Controls()
End Sub
Protected Sub Build_Dynamic_Controls()
'Clear placeholder
myPlaceholder.Controls.Clear()
'This is where the control counter stored in the viewstate comes into play
For i as Integer = 0 To CInt(ViewState("ControlCounter") -1
Dim ddlDynamic as New DropDownList With {
.ID = "ddlDynamicDropdown" & i,
.AutoPostBack = True
}
'This is the event that will be executed when the user changes a value on the form
'and the postback occurs
AddHandler ddlDynamic.SelectedIndexChanged, AddressOf ddlDynamic_SelectedIndexChanged
'Add control to the placeholder
myPlaceholder.Controls.Add(ddl)
'Put some values into the dropdown
ddlDynamic.Items.Add("Value1")
ddlDynamic.Items.Add("Value2")
ddlDynamic.Items.Add("Value3")
Next i
End Sub
Protected Sub ddlDynamic_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'When a dropdown value is changed, a postback is triggered (autopostback=true)
'The event is captured here and we add another dropdown.
'First we up the control count:
ViewState("ControlCounter") = CInt(ViewState("ControlCounter")) + 1
'Now that the "total controls counter" is upped by one,
'Let's recreate the controls in the placeholder to include the new dropdown
Build_Dynamic_Controls()
End Sub
...

Resources