开发者

Reuse a Variable Multiple Times on an ASP.NET Page

开发者 https://www.devze.com 2022-12-27 04:53 出处:网络
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 h

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 开发者_JAVA百科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.


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.


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%>


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.


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消