pass code behind variable value to link in aspx - asp.net

I have value in variable in code behind file. I want to pass that value to link present in aspx page. this is my code behind file code.
string SchoolCode = Request.QueryString["School_Code"].ToString();
This is my aspx page.
See Progress Report
I want to show progressreportschool.aspx with the url in the link by passing schoolcode.

You can do this in server-side
See Progress Report
Code Behind(VB.Net)
Private Sub aprogress_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles aprogress.ServerClick
aprogress.HRef = "ProgressReportSchool.aspx?" & SchoolCode
End Sub

Related

How to link DataSet to .aspx page in asp.net

I created a DataSet named CardInfo.xsd in Visual Studio 2015 that works perfectly
When I try to show data from it using a TableAdapter (which also works fine and displays the specific query I set up), I get an error in the code behind file.
It says CardInfo is not defined and also that the TableAdapter (CardInfoTableAdapters.TableTableAdapter) is not defined.
However it lets me select any other .xsd or .mdf files in the project, so I feel like the DataSet is not broadcasting itself to the rest of the project.
In the end it is supposed to get the value from the text box and show the row whose primary id matches the inputted number.
Here is the code to create the DataSet on the page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
GetCardInfo(cardentry.Text)
End If
End Sub
Protected Sub GetCardInfo(ByVal Entry As String)
Dim myDataSet As New CardInfo
Dim cardDA As New CardInfoTableAdapters.TableTableAdapter
cardDA.Fill(myDataSet.Entry, Entry)
GridViewRetrieval.DataSource = myDataSet.Tables("Table")
GridViewRetrieval.DataBind()
End Sub

How To Pass Control As A Variable

This is one of those problems which seems like it should have a simple solution but I can't work out what it is!
How can I pass a control from one sub to another if the first sub doesn't actually call the second? For example, where btnChangeText is in a panel that has a ModalPopupExtender called mpExample, and therefore isn't usually visible:
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
<SpecifiedTextBox>.Text = "Hello"
End Sub
And then on the main page, visible at all times, is a button associated with each textbox. In this example, it's textbox15:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
<Set TextBox15 as variable>
mpExample.Show()
End Sub
I know it's a silly example - believe me when I say that the real application I want to make of this actually makes sense! But the point is that I want to somehow store the name of the control to be updated by the first sub when the second sub is run.
If I was calling the first sub from the second it'd be easy, I'd just pass it as an argument, but I'm not. The first sub is called from a button click and is an independent action from the running of the second sub.
I don't seem to be able to use a session variable (my first thought) because I can't find any way to store the control name as a string and then convert it back to an actual control when the first sub runs. That'd be the easiest answer if somebody could tell me how to do it.
One approach would be to store the control's ID as a string in a Session variable, and then use the FindControl method to grab the control in your 2nd Click event.
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15.ID
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Page.FindControl(Session("currentTextBox")),TextBox)
currentTextBox.Text = "Hello"
End Sub
Note that if your TextBox15 control is inside some kind of container (a Panel or something), you'll need to use that container's FindControl method, rather than Page.FindControl.
Another approach is to store the TextBox itself in a Session variable, and then pull that out to set the text in your other method. Note that this only works if the methods are both called in the same request (which doesn't sound like it would work for your use-case). Here's what that would look like:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Session("currentTextBox"), TextBox)
currentTextBox.Text = "Hello"
End Sub

Redirect in Master Page Before Content Page Load

I have code in my ASP.NET master Page_Init event page that checks if a user is authorized to be on the content page, and if not redirects them to the login page. This code works fine as far as the check itself.
However, I have discovered that the content Page_Load event still fires after the above redirect.
This is causing a problem on pages that assume the user is logged in and certain variables are set.
This is the master page code (simplified)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
...
If Access_Level > User_Level_ID Then
Response.Redirect("~/login.aspx", False)
End If
End Sub
The above test works fine, and the redirect line is executed, but doesn't take effect before the code below is fired and executes.
This is the content page code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Rec_IDs As New List(Of String)
Rec_IDs = Session("Rec_IDs")
lblCount.Text = String.Format("You have {0} records in your cart", CType(Rec_IDs.Count, String)) 'this gives an error if Session("Rec_IDs") is null
End Sub
I realize I can put code in each of my content pages to check if a user is logged in / authorized, but I wanted to control it all from one location if possible.
Am I doing something wrong? I've read so many pages that say the master page is the place to do the check.
Thanks. :-)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
...
If Access_Level > User_Level_ID Then
Response.Redirect("~/login.aspx", True)
End If
End Sub
Using Response.Redirect("~/login.aspx", True) will terminate the current page processing & redirect to desired page.
Though it is recommended to use "Response.Redirect("~/login.aspx", False)" but this will not terminate page execution. It will redirect after current page processing end.
It does so because the 2nd argument in your Response.Redirect is set to false - which means you're not ending the execution of the rest of the page.
If you set it to true, page execution ends (prevents the Page_Load of the content page/s from firing. EDIT: as well as any other subsequent Master page events for that matter)
Response.Redirect("~/login.aspx", True)
Check what it does to all your pages though....e.g. your login.aspx page shouldn't have the same master page the way the code is written above...

How can I grab the current url and assign it to a variable using vb and asp.net?

I have a form that the user fills out and submits. I have vb code that converts that form into an email and sends it to me. I need to know what page it is coming from so I want to assign the current url to a variable that will be included in the email.
Simply put: How do I assign the URL to a variable?
You can find this in the request object. e.g. Request.Url.AbsoluteUri
Dim url As String = Request.Url.AbsoluteUri
VB.NET
'static variable
Private Shared prevPage As String = String.Empty
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Not IsPostBack) Then
prevPage = Request.UrlReferrer.ToString()
End If
End Sub

How to call Value from web form?

I creat one WEB project, this project contain tow WEB FORM, In the first Web Form Design i have tow TextBox for Entring the date(All dataTable between this tow dates) and one Button, I want that when i press to to this Button it will load the second WEB FORM and show all the Data Table in DataGrid In this WEB FORM, So i need To call this tow TextBox value from the first WEB FORM to the second WEB FORM In Load_Page i will use this tow value in select statment. So i want to know how to call this to value from the first WEB FORM. I'am using VB.NET WEB APPLICATION.i have allrady DB in SQL .
you have a lot of ways to pass values from page to another like query string, sessions, etc....
Another way to achieve this is by using the PostBackUrl property of the button.
Refer Button..::.PostBackUrl Property for more information
You can use querystring to get last page values to load page. Use this code
Response.Redirect("Default.aspx?value1 =" & value & "&value2" & value)
write page name as like default.aspx.
In the button click on the first page, you can call the second page by using Server.Transfer like this:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = TextBox1.Text & " Add something before continue..."
Server.Transfer("SecondPage.aspx")
End Sub
Then you can get the textbox from the first page in the second page Page_load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lastPage As Page = Context.Handler
Dim textBox As TextBox = lastPage.FindControl("TextBox1")
End Sub
If you don't need to do anything on the button event other than posting to the next page, you can do it simpler...
Definition of the button in the ASPX markup for the first page:
<asp:Button id="Button1" PostBackUrl="SecondPage.aspx" Text="Continue" runat="Server"/>
Then you can get the textbox from the first page in the second page Page_load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim textBox As TextBox = PreviousPage.FindControl("TextBox1")
End Sub

Resources