Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Hyperlink Button List

Author  Topic 

jhermiz

3564 Posts

Posted - 2007-04-12 : 17:00:01
Im using vs.net 2k5 and wondering how does one create a linkbutton list. For instance if I wanted links for every alphabet letter:

A B C D E F G ... Z

As hyperlinks...is there some sort of control I could use ?

ASP.net by the way.

Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url]

dfiala
Posting Yak Master

116 Posts

Posted - 2007-04-12 : 21:00:40
Sure. You could use a repeater. Here's a user control that fills a repeater with links using the letters of the alphabet

Ascx...
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AlphaLinkRepeater.ascx.cs" Inherits="AlphaLinkRepeater" %>
<asp:Repeater ID="AlphaRepeater" runat="server" OnItemDataBound="AlphaRepeater_ItemDataBound">
<HeaderTemplate>
<table><tr>
</HeaderTemplate>
<ItemTemplate >
<td style="padding:2px"><asp:HyperLink ID="AlphaLink" runat="server" /></td>
</ItemTemplate>
<FooterTemplate></tr></table></FooterTemplate>
</asp:Repeater>

ascx.cs...

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AlphaLinkRepeater : System.Web.UI.UserControl
{
private const string ALPHAHBET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
char[] AlphaArray = ALPHAHBET.ToCharArray();
AlphaRepeater.DataSource = AlphaArray;
AlphaRepeater.DataBind();

}
}
protected void AlphaRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink AlphaLink = (HyperLink)e.Item.FindControl("AlphaLink");
char LinkChar = (char)e.Item.DataItem;

AlphaLink.Text = LinkChar.ToString();
AlphaLink.NavigateUrl = String.Format("{0}?SelectedLetter={1}", LinkUrl, AlphaLink.Text);
}

}

public string LinkUrl
{
get
{
//if no baselink has been set
if (ViewState["BaseLink"] == null)
{
//just return the current page
return (Path.GetFileName(Request.Path));

}
else
return (string)ViewState["BaseLink"];
}
set
{
ViewState["BaseLink"] = value;
}

}
}


Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2007-04-13 : 00:30:47
Well, my original version returned to the calling page the selected letter, which you could have pulled off the Request.QueryString. But you could:

- add an event to handle LinkButton or Button clicks
in the code above
- replace the hyper link with a LinkButton or Button
- set the button text = the letter
- set handler to the button's click event to the handler you created
AddHandler AlphaButton.Click, AddressOf AlphaButton_Click

- then you can just read the text of the sender object (which will be the button) in the click event and do what you will

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-04-13 : 10:58:11
quote:
Originally posted by dfiala

Well, my original version returned to the calling page the selected letter, which you could have pulled off the Request.QueryString. But you could:

- add an event to handle LinkButton or Button clicks
in the code above
- replace the hyper link with a LinkButton or Button
- set the button text = the letter
- set handler to the button's click event to the handler you created
AddHandler AlphaButton.Click, AddressOf AlphaButton_Click

- then you can just read the text of the sender object (which will be the button) in the click event and do what you will

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP



Hi Dean,

The original issue is I get a post back which makes some resets on some things that I dont want it to reset. Basically I need to be able to select a "Letter" and know what has been selected without that post back. I tried to add an AddHandler..but it wasnt working the way I wanted it to. Any possibility you can post some code to handle this ?

Thanks again, so far the code fits my needs.

Thanks,
Jon


Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url]
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2007-04-13 : 13:23:57
Buttons post back. If you want to know or do anything on the server side -- you have to post back. That's how the server knows what the use has done.

If you don't want your other code to reset then you have 2 choices:
1) put the other code inside an If...Then statement
If Not Page.IsPostback Then
'other code
End if
2) use my original code which just calls the page again with the selected letter

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-04-13 : 13:45:42
I have done that but the radio button list still resets even after a not page is postback.
The issue is I want to know the letter right on the click of the hyperlink, I dont want to pass it in the URL address bar :(


Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url]
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2007-04-13 : 14:07:16
You want the clicked value. You don't want it passed by a URL. That leaves you one choice.

To have it passed to the server by postback -- which is how web forms work.

If your radio button list is resetting on pastback, there is some code in the postback causing it to reset.



Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-04-13 : 14:08:34
Err my mistake got it.

Thanks again


Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url]
Go to Top of Page
   

- Advertisement -