I am using an ASP.NET framework. Within my wele page (after a user logs in) I want to customize the paragraph text to include their login information. For example:
<p>
Wele to the website!
</p>
Is what the code would have currently, but what I want is:
<p>
Wele <var=Username> to the website!
</p>
Now obviously that is not correct, i didn't expect it to be. I just want to demonstrate what i am trying to do. I thought about using JavaScript to acplish this somehow, but that seemed a bit overly plicated. My variable "Username" es from another class in a .cs file elsewhere. How can I embed that variable into my <p></p>
?
I am using an ASP.NET framework. Within my wele page (after a user logs in) I want to customize the paragraph text to include their login information. For example:
<p>
Wele to the website!
</p>
Is what the code would have currently, but what I want is:
<p>
Wele <var=Username> to the website!
</p>
Now obviously that is not correct, i didn't expect it to be. I just want to demonstrate what i am trying to do. I thought about using JavaScript to acplish this somehow, but that seemed a bit overly plicated. My variable "Username" es from another class in a .cs file elsewhere. How can I embed that variable into my <p></p>
?
- Can you share the .cs file? – Darren Commented Dec 28, 2012 at 18:01
- I don't see what that matters, its just a simple string variable located in a class named 'User' – Nibirue Commented Dec 28, 2012 at 18:05
- it matters so we can try and provide with a more concise answer. If you check my answer below it should help. – Darren Commented Dec 28, 2012 at 18:06
- @Nibirue - because all of of answers we put = User and assume you know how to get the User's name , if you were unable to get the Users name more code could be provided to help with that as well – Scott Selby Commented Dec 28, 2012 at 18:08
- In this case, yes. but i have already retrieved the correct username and saved it in the codebhind for the wele page, so that is a non-issue. – Nibirue Commented Dec 28, 2012 at 18:22
7 Answers
Reset to default 4Add a label in your markup:
<p>
Wele <asp:Label ID="lblUsername" runat="server" /> to the website!
</p>
And then in the code behind:
protected void Page_Load(object sender, EventArgs e)
{
lblUsername.Text = "username"; // set to the username variable here
}
You could use an <asp:Literal
in place of your marker then in the Page_Load set the value as required ltUsername.Text = UserName;
<p>Wele <asp:Literal ID="ltUsername" runat="server" /> to the website</p>
and in your code behind
protected void Page_Load(object sender, EventArgs e)
{
ltUsername.Text = Username; // whatever that may be
}
The literal outputs only what you supply - whereas an asp:Label will wrap your text in a <span>
tag and adds extra mark-up which can be confusing. For that reason I only use asp:Label
for actual <label>
elements (those associated with an input
).
if that variable es from another class you will need a way to store it somehow. One way to do that is by putting it in a session.
so in your "other" class
Session["myVariable"] = variable;
Then in the page you want to set the "Wele "Variable" to the website" you do the following:
first, create a property:
public string name {get;set;}
Then, in your page_load:
protected void Page_Load(object sender, EventArgs e)
{
name = Session["myVariable"]
}
then simply put the following code in your .aspx page:
Hello <%= name %> , wele to the website!
Alternatively you could also do:
Page_Load:
Label1.Text = Session["myVariable"].ToString();
.aspx:
Hello, <asp:Label ID="Label1" runat="server" /> wele to the website.
in Html
<p>
Wele <asp:Label ID="lblUserName" runat="server"></asp:Label> to the website!
</p>
then in .cs file in Page_Load
lblUserName.Text = {The User's First Name}
if you post more code on how you did the authentication , then I can replace {The Users First Name}
with actual code. For now just use that on your own to put in FirstName Code
You will have to use a server side control, i.e. a Label, Textbox and do:
<asp:Label id="username" runat="server" />
The above party goes in your markup section.
Then on your Page_Load
event or where the username property is located in the .cs file populated you can do:
username.Text = User;
I would suggest you take a tour through a tutorial site and get familiar with the syntax and structure. A good place to start is here, http://www.w3schools./aspnet/default.asp (Gosh I can imagine the feedback on this already ;) )
What you are trying to acplish is very simple, you can also just install some of the Templates that e along with Visual Studio (if you have it) ... some do exactly what you are trying to achieve.
Good luck!
I would say that best way to go is using:
var page=HttpContext.Current.Handler as Page;
Than:
page.Header.Controls.Add(literalcontrol);
So can you use that from a class, and dont need to write it on each code behind.