Reuse a Variable Multiple Times on an ASP.NET Page
Asked Answered
L

5

8

I feel somewhat foolish asking such a simple question, but I can't seem to find an answer. I'm new to ASP.NET (C#), but I'm learning by building a simple set of web pages that display a report. I have a variable that represents a company name. I need to output this variable in multiple places on the web page. The only way I have found to output a variable this is with:

company_name.Text = "Acme Windows";

then

<asp:literal id="company_name" runat="server" />

My problem is that I want to use company_name in multiple places on the page. Do I really have to create a separate variable holding the the same value for each time it is placed on the page? If I just copy the above XML code to all the places I want to show the variable it obviously creates a compile error since that ID is already defined.

I feel like I'm missing something very obvious.

Lashelllasher answered 19/4, 2010 at 17:18 Comment(1)
Its actually quite a good question.Usia
S
11

The easiest way to do this is to create a string variable or property in your code-behind class and use the <%= %> notation (short for Response.Write) to render it on your page inline:

// You can do this anywhere on your .aspx, as many times as you like.
<%= this.CompanyName %>

// Better yet, html encode the value to protect against various threats,
// such as cross-site script injection (XSS)
<%= HttpUtility.HtmlEncode(this.CompanyName) %>

.NET 4.0 introduces a new shortcut notation (Html Encoding Blocks) to html-encode your output:

<%: this.CompanyName %>

Regarding your original approach, ASP.NET web controls like Literal represent individual parts of a web page - you can't use them multiple times on a page because the object instance company_name refers to the specific part of the HTML generated by the <asp:literal> in your .aspx page.

Systematize answered 19/4, 2010 at 17:18 Comment(1)
Warning: this leaves you open to xss bugs. A label control will sanitize the output. ASP.Net 4 (came out last week) has a new <%: %> bee sting you should almost always use instead of <%= %>Faux
T
3

In this case, you create a property on the page and output that in every place you need it.

public string CompanyName { get { return "Acme Windows"; } }

And in the aspx:

.NET 4.0:

<%:this.CompanyName%>

Before 4.0:

<%=this.companyName%>
Tallula answered 19/4, 2010 at 17:18 Comment(0)
D
1

Why is this a community wiki?

Anyway, you have several possibilities to achieve what you want. Placing multiple variables holding the same name is for sure not best practice. If you have it filled with, let's call it, "semi-dynamic" value, I'd not put it hardcoded within your code. What I would do is to use a global resource file.

You create a new resource file in the App_GlobalResources folder and add a key "COMPANY_NAME" with value "Acme Windows". Then within your ASPX code you can do something like

<asp:literal id="company_name" runat="server" Text="<%$ Resources:GlobalResources, Button_Save %>"/>

I've written a blog post some time ago which details this approach. The advantage of the resource file is that you don't have to touch the code.

If you want to further "refactor" then - assuming you have some general company info you have to display on different positions on the page - you could create a separate UserControl which contains the information like company name, phone number, contact info etc. Within that control you have your literal, label, whatever you use to display that information exactly 1 time. This UserControl is then placed on the places on the actual page where you need it, even multiple times.

Diacid answered 19/4, 2010 at 17:18 Comment(0)
G
1

You could add the control dynamically:

Literal myLiteral = new Literal();
myLiteral.text = "Acme Windows";
this.Page.Controls.Add(myLiteral);

You can also add the control within a specific control on the page, by changing the this.Page.Controls reference to the particular control you want to add the literal to.

Georgeta answered 19/4, 2010 at 17:18 Comment(0)
G
0

The simplest answer is you need to define multiple controls.

But a better solution would be to do this:

Create a property on the code behind side of things:

protected CompanyName{get;set;}

Then, in the aspx side of things, reference that with the <%= %> commands

<span><%=CompanyName %></span>
Garfish answered 19/4, 2010 at 17:18 Comment(1)
<%= leaves you open to xss bugs. If you can use asp.net 4, <%: is better.Faux

© 2022 - 2024 — McMap. All rights reserved.