Filling multiple literals with the same value - asp.net

I have multiple literals on a page. For example,
<asp:Literal id="Label1" runat="server" />
I have around 10 of these on one page and want to fill them all with the same value. Is there a good way to do that without referencing the id for each control?

Create a public property that holds the value you want to display and in the aspx use
<%= propertyName %>
everywhere you want to display the value.

If you're using resource files, you can set the meta:resourcekey attribute to the same value for all 10 of them.

The only way I've seen it done is make the id's a loopable name (e.g. Label1 - Label10) and loop through referencing the id. But we didn't try too hard for a different solution!

Are the Literals located in a single Container (for instance, a Panel) or are they littered about the page? If they are in a single container which does not contain too many other non-relevant controls, it would be as simple as looping through the Container's Controls collection.
Yes, I'm well aware that a Page is a Container as well, but it is almost always very inefficient to loop through all the controls in a Page. I wouldn't recommend it at all.

How are your labels named. If they have a common naming convention like Label1 - Label10 then you could do
for(int i = 1; i <= 10; i++)
{
Literal l = Page.FindControl(string.Format("Label{0}", i)) as Literal
if(l != null)
l.Text = "Whatever";
}
If they're not named similarly, you could just stick the label names in an array and loop through that instead of having 10 explicit .Text = statements. e.g.
string [] litNames = new string [] { "litFirst", "otherLit", "someOtherVar" }
foreach(string s in litNames)
{
Literal l = Page.FindControl(s) as Literal
if(l != null)
l.Text = "Whatever";
}

Related

Is it a good practice to compare combobox.selectedvalue to a string?

I have a combo box and couple of text boxes in my web page, depending on combobox's selected value, I will set focus to specific text box. Following is my code:
if (cbo1.SelectedValue == "01")
txt1.Focus();
else
txt2.Focus();
This would work even when the combo box being just loaded and there is no selected item. My question is "is this a good practice?" since SelectedValue actually is an object. Normally I use cob1.SelectedValue.ToString(), but I got an exception when there is no selected item.
Good practice would be to declare a string constant:
private const string FIRST_FIELD_VALUE = "01";
(...)
if (cbo1.SelectedValue.Equals(FIRST_FIELD_VALUE))
txt1.Focus();
else
txt2.Focus();
Otherwise, yes. I think comparing strings with strings is good practice.
Add this condition
if( cbo1.SelectedIndex > 0)
{
if (cbo1.SelectedValue == "01")
txt1.Focus();
else
txt2.Focus();
}

Displaying a value as an image in vb

In my aspx page I have a gridview which displays the value from my database as "*". So if a value in my database table is 5, it will be displayed as "*****" in the gridview.
code in aspx:
<asp:TemplateField HeaderText="Rating" SortExpression="Rating">
<ItemTemplate><h1><%# getrating(Eval("Rating"))%></h1></ItemTemplate>
</asp:TemplateField>
code in aspx.vb
Protected Function getrating(ByVal rate As Integer)
Dim retval As String
retval = ""
For i = 1 To rate
retval = retval + "*"
Next
Return retval
End Function
What I want to do is change that "*" to a picture, ie star.jpg, so in the gridview it will display the image star.jpg instead of "*".
Any idea on how to do this please? Using MS visual studio 2010
You should be able to just do something like the following:
retval = retval + "<img src=""star.jpg"" alt=""*"">"
You may need some styling to put them all on the same line (float:left should do) but I forget offhand whether that is necessary.
The principle of this is obviously pretty basic. In the outputted HTML you will just have an image tag where before you had a *. A neater way to do it might be to have five images for one to five and then have something like:
Protected Function getrating(ByVal rate As Integer)
Dim retval As String
retval = String.Format("<img src=""Star{0}.jpg"" alt=""{0} stars"">", rate)
Return retval
End Function
This will output an image tag pointing to any of Star1 to Star5. This is nicer in some ways because it allows better control over how they look as well as allowing you to make five identical sized images and know that the space used will always be the same.
Here's an implementation using CSS - http://www.thebroth.com/blog/119/css-rating-stars - does require additional divs and two images to create effect but seems to be a neat solution.

How to get multiple value from one textbox?

I have created web application and textbox as a textarea. I am using javascript for validation. When I enter value in text box so it should be number not alphabet I have use textmode is multiple line.
My problem is that how I get multiple value from textbox and store in array in javascript and check each value is number or not. I am using the web form. Please help me.
You can get the value from a textarea like
var txtvalue = document.getElementById("txtareaid").value
and if are using a separator then something like
var txtvaluearray = document.getElementById("txtareaid").value.split(';')
will get you all the values in an array if the seperator is ;
Edit
As per your update you can use \n as the separator and as pointed by #Sohnee you can do the validation.
As addition to rahul:
If you want the values in the textarea seperated by line, you can use \r\n as the splitter.
This is a starter for ten.
var textValues = document.getElementById("mytextarea").value.split("\n");
for (var i = 0; i < textValues.length; i++) {
if (isNaN(textValues[i])) {
alert(textValues[i] + " is not a number.";
}
}

Filtering names on the basis of first character of the name

i have a page in which i am displaying the name of all the users i want to filter their names on the basis of first character for that i want to show A B C D ....X Y Z filters on the top on clicking of which it will filter the names accordingly my problem is not the query part but how to add these letters do i have to add 26 link buttons separately or there is some work around for example you might have seen such type of behavior in some music sites for filtering the songs with starting character.
These are few useful links how to do alphabetical paging
1. http://www.highoncoding.com/Articles/209_GridView_Alphabet_Paging.aspx
2. http://aspdotnetcodebook.blogspot.com/2008/03/how-to-add-alphabet-paging-in-gridview.html
Use ASCII characters codes to do this, for example:
var letters = new List<string>()
for(int i = 65; i < 91; i++)
letters.Add(Convert.ToChar(i).ToString());
Display it by adding links to page:
foreach(letter in letters)
{
var hyperlink = new Hyperlink()
{
NavigateUrl = string.Format("Filter.aspx?letter={0}", letter),
Text = letter
}
Page.Controls.Add(hyperlink);
}
Of course instead of Page you can use any other container you want, you just need to add those hyperlinks to controls collection.
Also take care to run this code in proper method, for example by overriding CreateChildControls method.
Regards

How to use ASP.NET Resources in this way?

I am doing some labeling. I would like to add a resource key (s, sec) to one of my combo boxes with item[3sec, 5sec, 10sec, 30sec ...] and use it like:
Text="3<%$ Resources: myResource, s%>" to get comboBoxItem 3sec,
Text="5<%$ Resources: myResource, s%>" to get comboBoxItem 5sec ...
But I find that the server will treat this as plain text.
Do I need to define each item in a separate resource key pair?
Consider defining your resource file entry value as follows:
{0}sec
And then
String.Format(Resources.myResource.s, "3")
You can try creating your own custom expression builder. They are really powerful if you want to achieve what you have asked in your question.
Or, you can always do it using server-side code.
You could do it like this (with a DataBind() call somewhere):
Text='<%# "3" + Resources.myResource.s %>'
Edit: You can also do this from code behind. Something like this:
int[] times = new int[]{ 3, 5, 10, 30 };
foreach (int time in times)
{
string text = time.ToString() + Resources.myResource.s;
cbo.Items.Add(new ListItem(text, time.ToString()));
}
Edit 2: As per Muhammad's observation the first example doesn't work for this. I've used it for other controls, and I didn't see that for the current case it's not correct. Given this I would fill the control from code behind.

Resources