Dynamically create hyperlinks For English Characters from A-Z
This is how to Dynamicly build the a links of characters from A-Z , somthing like this
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
You may need to filter a GridView results , like displaying the list of users in which there names begins with specific character .
We can use the Repeater Control to Dynamicly generate the character links as follows:
ASPX:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:HyperLink ID="hyp1" runat="server" NavigateUrl='<%# eval("Name","~/Target.aspx?Key={0}") %>'
Text='<%# eval("Name") %>'></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>|</SeparatorTemplate>
</asp:Repeater>
And the Page Code Behind:
VB:
Private Sub BindRepeater()
Dim dt As New Data.DataTable
dt.Columns.Add("Name")
Dim c As Char = "A"
Do While Asc(c) <= Asc("Z")
dt.Rows.Add(c.ToString())
c = Chr(Asc(c) + 1)
Loop Me.Repeater1.DataSource = dt
Me.Repeater1.DataBind()
End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindRepeater()
End Sub
C#:
private void BindRepeater()
{
Data.DataTable dt = new Data.DataTable();
dt.Columns.Add("Name");
char c = "A";
while (Strings.Asc(c) <= Strings.Asc("Z")) {
dt.Rows.Add(c.ToString());
c = Strings.Chr(Strings.Asc(c) + 1);
}
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
protected void Page_Load(object sender, System.EventArgs e)
{
BindRepeater();
}
See Also :
Repeater Web Server Control
Repeater Class
Revision number 3, Monday, September 10, 2012 3:26:08 PM by tmorton
You must Login to comment.
|
Sat, Jun 7, 2008 3:50 AM
by blurearc
|
good one... but why donr you just wrap it up.. in a server control.. where datatable repeater will remain inside... and user just have to pass his params.. this will extend the usability.. then it depends on user whether he wants in uppercase, lowercase, or in numeric type.. but it was really a good aritcle... [:)]
|
|
Mon, Jun 9, 2008 2:42 AM
by rtam
|
very useful great place for a newbie to learn .. can you please go one step further and show me how to process / create pages that response to the hyperlink click? ie. when I place the mouse on A I can see the url as Target.aspx?Key=A or \on F the url shows Target.aspx?Key=F. How do I use the ?Key = information?
|
|
Mon, Jun 9, 2008 3:24 AM
by rtam
|
Apologize for being too hasty to post the above comment; I worked out a simple solution: on the Target.aspx page, use Request.Url to get the url, then extract the Key ... like I said earlier, this is a great place to learn
|
|
Tue, Jul 22, 2008 8:06 AM
by joels214
|
Hello,
Can u give me the whole code...Since i am kind of new to asp.net...Please...Tried but was getting errors Error 1 'System.Linq.Strings' is inaccessible due to its protection level
|
|
Wed, Dec 17, 2008 6:35 AM
by sirdneo
|
SeparatorTemplate>|/SeparatorTemplate> is new to me nice thing to use in data repeater
|
|
Wed, Jan 28, 2009 1:53 AM
by nilsan
|
Good and Simple Article!! :)
|