Given the code on the content page:
Are you sure you want to add another <asp:Literal runat="server" id="Name1" /> to the basket?
You already have a <asp:Literal runat="server" id="Name2" /> in your basket.
<asp:Literal runat="server" id="Name3" /> has not yet been added to your basket.
开发者_如何学编程And then in the codebehind:
Name1.Text = Product.Name;
Name2.Text = Product.Name;
Name3.Text = Product.Name;
I'm pretty sure this isn't the greatest way to go about things, is there an ASP.net feature or a design pattern I'm unaware of?
Why don't you use string.Format
?
String.Format("Are you sure you want to add another {0} to the basket? You already have a {0} in your basket. {0} has not yet been added to your basket.", Product.Name)
Well, in fact you don't need to use literals, if these three lines in your rendering should show the same product name, what about:
Are you sure you want to add another <%# Product.Name %> to the basket?
You already have a <%# Product.Name %> in your basket.
<%# Product.Name %> has not yet been added to your basket.
And during Load
just call DataBind() in your Page
or Control
.
Or you can use a ResX, add a string resource called "BasketText" and put this string:
Are you sure you want to add another {0} to the basket?
You already have a {0} in your basket.
{0} has not yet been added to your basket.
And in your control or page, implement a internal or public read-only property called "BasketText" and in the getter do this:
return String.Format(GetLocalResourceObject("BasketText"), ProductName)
Then, your markup will look like this (call DataBind too, as previous approach):
<%# BasketText %>
UPDATE & NOTE
Pay attention to other users suggesting inline evalutations <%=...%>
. This is an old-style approach and it has strong counterparts. If you do this way and code modifies control collection, your page will throw an exception.
For that reason, I'll be always suggesting data-binding expressions, which doesn't have such problem and these work like a charm.
If you can reach the variable Product.name or expose it you can do this:
Are you sure you want to add another <%=Product.Name%> etc..
Instead of a literal
Why dont you just use one Literal control and then use
String.format( "Are you sure you want to add another {0} to the basket? You already have a {0} in your basket. {0} has not yet been added to your basket.", Product.Name);
check it out:
http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx
Cheers
I don't see a problem with this except the variable names could use some better names. However you can dynamically add controls to your page, but that may be overkill for this situation. Here's a good article on that.
https://web.archive.org/web/20211020131055/https://www.4guysfromrolla.com/articles/081402-1.aspx
simply use only one literal with runat="server"
and in the same place where you are currently assigning the Product.Name
to those literals have a String.Format
Can't say this is necessarily better, but a shorter alternative.
<%=string.Format("Are you sure you want to add another {0} to the basket? You already have a {0} in your basket. {0} has not yet been added to your basket.",Product.Name)%>
精彩评论