Variable is not being replaced in ASPX - asp.net

I have 2 ASPX files:
In A.aspx:
<%
string user = "in A.aspx.cs";
Response.Write(user);
%>
In B.aspx.cs
string id = Request.QueryString["id"].ToString();
​Response.Write(id);
When I run A.aspx and click on the link, the value of id from B.aspx is "<%=dz_now%>".
Why?

The problem is that <%=variable %> does not work within server side code. You need to add the strings together like this:
string user = "in A.aspx.cs";

Related

Display query string in asp.net controls

I'm writing an ASP web application with VB back-end. What I'd like to do is generate a url and display this in control on the page. For example if I have a label and a button on the form. The label is blank. When the button is clicked the following code fires:
Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerate.Click
label1.Text = "Hello"
End Sub
What I'd like to have is a url that would point to my ASP Page with the words "Hello" in the label. Is this possible?
You could do the following:
{siteaddress}/aspxpage.aspx?label=hello
then in you aspx page do something like:
<asp:label runat="server" id="yourLabelId" text='<%=Request.QueryString("label")%>' />
Or in the Page_Load:
yourLabelId.Text = Request.QueryString("label")
I would recommend validating the data before just writing it into the page.
Pass the text in query string e.g. suppose relative path of the page is /pagename.aspx , you can pass the query string as per given example below:
/pagename.aspx?text=hello
in c# write following code in Page_Load event
//You don't have to check the url all the time , so just check it if page is not posting back (first time after user visits the page and ignore all other same page post backs. Label can maintain its control state (text value) after every postback, so assign it only once to increase performance
if (!IsPostBack)
{
//Check if query string is provided or not , if it is not provided take some default text, I am taking empty string as default text.
string givenText = (Request.QueryString["text"] == null)?"":Request.QueryString["text"];
label1.Text = givenText;
}
You can also create a property for text given through query string and default text.

Loading usercontrol to string and submitting the form within

What i'm doing is creating a website where the design is done i html files that are then read into the masterpage using System.IO.StreamReader.
and inside the html templates there are keywords like #USER.LOGIN#
that I replace with functions etc.
The issue is that i'm replacing #USER.LOGIN# With a usercontrol where there is a login form.
I have a function that reads the usercontrol into a string and it works.
but since the usercontrol is loaded to string alle the events are not following.
so when I submit the login form nothing nothing happends (the page posts) but cannot get any of the fields from the form...
NOTE:
i'm using url-rewriting so urls are http://www.domain.com/account/login
where account is account.aspx and login is the mode the account is in.
Code for replacing the keyword in the streamreader loop (pr line)
If InStr(line, "#USER.LOGIN#") Then
line = line.Replace("#USER.LOGIN#", vbCrLf & userfunc.GetMyUserControlHtml("uc", "account_login.ascx", "/account/login/") & vbCrLf)
End If
And the functions to read usercontrol
Public Shared Function GetMyUserControlHtml(contextKey As String, controllerfile As String, Optional ByVal formaction As String = "")
Dim myId As Guid = New Guid()
Return userfunc.RenderUserControl("~\Controllers\" & controllerfile, "", myId, formaction)
End Function
Public Shared Function RenderUserControl2(path As String, Optional ByVal formaction As String = "") As String
Using pageHolder As New Page(), _
viewControl As UserControl = DirectCast(pageHolder.LoadControl(path), UserControl), _
output As New StringWriter(), _
tempForm As New HtmlForm()
If formaction <> "" Then
tempForm.Action = formaction
Else
tempForm.Action = HttpContext.Current.Request.RawUrl
End If
tempForm.Controls.Add(viewControl)
pageHolder.Controls.Add(tempForm)
HttpContext.Current.Server.Execute(pageHolder, output, False)
Dim outputToReturn As String = output.ToString()
Return outputToReturn
End Using
End Function
How would you guyz do this?
I need the userlogin to be hardcoded in the usercontrol but still be able to place it anywhere using the template keyword.
This will also be used with other functions (newsletter signup, shoutbox etc.)
what i would suggest is register you control on the web config..
<add tagPrefix="CustomControl" tagName="LogIn" src="~/UserControls/Login.ascx"/>
you can still use "#USER.LOGIN#" but instead of replacing it with a control...
replace it with a something like this
<CustomControl:LogIn id="LogIn" runat="server"/>
this is just a quick write up.. but you could always try if it works
you can save your HTML like this istead of placing an actual "#USER.LOGIN#"
<% =GetLoginControl() %>
and then create a public function in your code behind named GetLoginControl() and return a response.write of the HTML Mark up you need

ASP Link with parameter not passing

Sometimes a link with a parameter will bring up the parameter and sometimes it won't. If I have IE open and doing things in other tabs and try to click the link with the parameter in it, it will come up to the main screen. If I click the link without IE open, it goes to the site with the parameter. Please help!
Sample Link: http://ServerName/time_and_attendance/?timesheet_id=7489
Code below:
<!--#INCLUDE virtual="/time_and_attendance/i_time_attendance_header.asp" -->
<%
'---------------------------------------------------------------------------------------------------------------------
'JFV 6-10-2010: Will need these lines uncommented and inserted above the '<!--#INCLUDE' line
' to be used in the alternate e-mail configuration
'<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows 2000 Type Library" -->
'<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->
'---------------------------------------------------------------------------------------------------------------------
%>
<%
'If there is not a timesheet id send user back to their employee page
If Request("timesheet_id")<>"" Then
my_employees_timesheet_id=Request("timesheet_id")
RedirectUrl="my_employees_timesheet.asp?timesheet_id="&Request("timesheet_id")
'Response.Write my_employees_timesheet_id
Else
Response.Redirect("default.asp")
End If
%>
You should be using Request.Querystring instead of simply Request, and Trim the value before using it.
Also, dim a variable and retrieve the parameter into the variable first.
dim ts_id
ts_id = trim(request.querystring("timesheet_id"))
If ts_id <>"" Then
my_employees_timesheet_id=ts_id
RedirectUrl="my_employees_timesheet.asp?timesheet_id="&ts_id
'Response.Write my_employees_timesheet_id
Else
Response.Redirect("default.asp")
End If

Another way to pass a value across WebForms

If I use a Server.Transfer command with a true value passed for the "PreserveForm" 2nd param's value:
Default.aspx:
Server.Transfer("WebForm1.aspx", True)
...I can access the value of the textbox "TextBox1" control on the first page, Default.aspx, from "WebForm1.aspx" as follows:
lblPassedValue.Text = Request.Form("ctl00$MainContent$TextBox1")
I know that I can control the way that IDs are generated in Visual Studio 2010, but how about controlling the NAME property?
I would like it to read:
lblPassedValue.Text = Request.Form("TextBox1")
I'd also rather use ASP.NET Textbox controls rather than a native HTML textbox.
Server.Transfer("Page.aspx", True)
On Page.aspx:
<%# PreviousPageType VirtualPath="~/SenderPage.aspx" %>
Dim Test = CType(Me.PreviousPage.FindControl("TextBox1"), TextBox).Text

previouspage.findcontrol getting variable from previous page

i am trying to retain the value of a variable from a previous page. i believe this is the C# way to do this, but i would like the vb.net way can someone please help:
TextBox myTxt = (TextBox)Page.PreviousPage.FindControl("previousPageTextBox");
currentPageTextBox.text = myTxt.Text;
i would like to know how to code this in vb.net
i tried this:
Dim myTxt As TextBox = CType(Page.PreviousPage.FindControl("Textbox1"), TextBox)
TextBox1.Text = myTxt.Text
but i got this error msg:
use the new keyword to create an object instance
This should do it, also make sure you are using Server.Transfer to go between pages.. but I am sure you already know this:
Dim myTxt as TextBox = CType(Page.PreviousPage.FindControl("previousPageTextBox"), TextBox)
currentPageTextBox.text = myTxt.Text
dim txt as TextBox =CType( Page.PreviousPage.FindControl("previousPageTextBox"),
TextBox)
currentPageTextBox.Text = txt.Text
PS:- You need to set the previous page in the page where you want to find the control.
<%# PreviousPageType VirtualPath="~/Test.aspx" %>
Also better way will be to create an function on previous page which returns the text in textbox.
internal function TextBoxText() as string
return myTextPage.Text
end function
and use this on next page like this:
currentPageTextbox.Text = Page.PrevousPage.TextBoxText
let me know if this works because I've not used vb for long long time.
PPS:- It is only available if you user Server.Transfer or go to currentPage using some asp.net contorl like LinkButton from simple href links PreviousPage is null

Resources