.NET Forum / ASP.NET / General / October 2005
Capture hyperlink text property
|
|
Thread rating:  |
Joe - 25 Oct 2005 18:37 GMT I have an aspx page (referred to here as page_1) with a datagrid whose first column contains hyperlinks. When a user clicks one of these hyperlinks, he will navigate to another aspx page (referred to here as page_2). I need to cache the value of the link's text (hyperlink.text property) so that I can use it in the page_load event of page_2.
I've thought of using a hidden field and then calling Request.Form("hdnClickedLinkText") in the Page_Load event of page_2. What I don't know is how to populate the hdnClickedLinkText field on page_1 when the hyperlink is clicked. I've thought about using AddHandler but there is no exposed click event for the hyperlink control. I could design a custom class which inherits from the hyperlink class and implement my own Click event, but this seems a bit much when a simpler solution may exist.
Does anyone have any other ideas?
TIA,
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
Curt_C [MVP] - 25 Oct 2005 18:59 GMT is this a standard HREF or does it have a postback? If it's a standard HREF you will have to pass the call to some clientside code, passing in the text, and then processing it and redirecting. If it's a postback you can get the info more easily but more importantly you can do what you want/need with it then (session value, querystring, etc).
 Signature Curt Christianson site: http://www.darkfalz.com blog: http://blog.darkfalz.com
> I have an aspx page (referred to here as page_1) with a datagrid whose first > column contains hyperlinks. When a user clicks one of these hyperlinks, he [quoted text clipped - 13 lines] > > TIA, Joe - 25 Oct 2005 19:15 GMT Standard HREF; there is no postback. page_1 and page_2 are two different pages.
Each href looks something like: href="page_2.aspx?q1=value1&q2=value2" where value1 and value2 change from link to link.
Also, I do not have the option to pass the text property in the querystring.
page_2 merely needs the value of the hyperlink's text. I can handle the processing once I've got it. I just don't know how to get it.
So, any ideas?
For example, how would you "pass the call to some clientside code, passing in the text?"
Thanks,
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> is this a standard HREF or does it have a postback? If it's a standard HREF > you will have to pass the call to some clientside code, passing in the text, [quoted text clipped - 19 lines] > > > > TIA, Curt_C [MVP] - 25 Oct 2005 21:03 GMT without postback and without querystring you are limited. I would say try the javascript call. add an onClick to the HREF to a javascript function, passing in the text and URL. Have the javascript function save this to a session item, then redirect to the URL.
Plenty of samples of this out there...
 Signature Curt Christianson site: http://www.darkfalz.com blog: http://blog.darkfalz.com
> Standard HREF; there is no postback. page_1 and page_2 are two different > pages. [quoted text clipped - 37 lines] > > > > > > TIA, Joe - 25 Oct 2005 21:12 GMT I have been looking at samples for over an hour and haven't found one that shows how to retrieve the text. Do you know how? The URL is easy; the text is a mystery....
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> without postback and without querystring you are limited. I would say try the > javascript call. [quoted text clipped - 46 lines] > > > > > > > > TIA, Curt_C [MVP] - 25 Oct 2005 21:38 GMT you misunderstood.. you have to PASS the text to the function. In otherwords you need to write the text into the onClick function as well as to the screen.
 Signature Curt Christianson site: http://www.darkfalz.com blog: http://blog.darkfalz.com
> I have been looking at samples for over an hour and haven't found one that > shows how to retrieve the text. Do you know how? The URL is easy; the text [quoted text clipped - 50 lines] > > > > > > > > > > TIA, Joe - 26 Oct 2005 12:49 GMT Curt,
I did not misunderstand. I can't PASS the text if I can't RETRIEVE the text.
I have tried the following javascript:
function parseHyperlinkId(o) { alert("Thomas Jefferson was a hack!"); alert(o.text); alert(o.href); alert(o.target); }
Using the following Server side code to link the hyperlink's onClick event to the javascript:
hLink.Attributes.Add("onClick", "return parseHyperlinkId(this);")
The works fine except that o.text returns "undefined." Do you know why? Do you know how to retrieve the text from the link? Which property I should use? Do you know what's wrong with my code?
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> you misunderstood.. you have to PASS the text to the function. > In otherwords you need to write the text into the onClick function as well [quoted text clipped - 54 lines] > > > > > > > > > > > > TIA, Elton W - 25 Oct 2005 20:51 GMT Hi Joe,
Two solutions: 1. Still use Hyperlink column in the datagrid, but add something in datagrid_itemDatabound event:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { HyperLink link = (HyperLink)e.Item.Cells[link_column_index].Controls[0]; link.NavigateUrl += "&text=" + link.Text; }
Then in page2, you can retrieve it from querystring.
2. Change Hyperlink column to LinkButton. Then you can process in datagrid_ItemCommand event: pass data to Session, Cookie, or queryString and redirect to page2.
HTH
Elton Wang
> I have an aspx page (referred to here as page_1) with a datagrid whose first > column contains hyperlinks. When a user clicks one of these hyperlinks, he [quoted text clipped - 13 lines] > > TIA, Joe - 25 Oct 2005 20:58 GMT Thanks Elton. I can't use the QueryString due to Business Requirements. How do I cast the Hyperlink column in the datagrid to a link button?
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> Hi Joe, > [quoted text clipped - 36 lines] > > > > TIA, Elton W - 25 Oct 2005 21:26 GMT No you can't cast Hyperlink to link button. You should change HyperLinkColumn to ButtonColumn (ButtonType=LinkButton). Then you can do your logic in datagrid_ItemCommand event on server-side.
HTH
Elton
> Thanks Elton. I can't use the QueryString due to Business Requirements. How > do I cast the Hyperlink column in the datagrid to a link button? [quoted text clipped - 39 lines] > > > > > > TIA, Joe - 26 Oct 2005 12:51 GMT Thank you, thank you, thank you, thank you.
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> No you can't cast Hyperlink to link button. You should change HyperLinkColumn > to ButtonColumn (ButtonType=LinkButton). Then you can do your logic in [quoted text clipped - 47 lines] > > > > > > > > TIA, Joe - 26 Oct 2005 13:03 GMT Elton,
The ItemCommand is not firing. Here is my code:
Private Sub dgForms_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgForms.ItemCommand
hdnFormNumber.Value = CType(e.Item.Cells(0).Controls(0), LinkButton).Text End Sub
I've had this problem before with the SortCommand, but don't quite know how to resolve it. Do you have any ideas why this wouldn't fire?
Joe
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> No you can't cast Hyperlink to link button. You should change HyperLinkColumn > to ButtonColumn (ButtonType=LinkButton). Then you can do your logic in [quoted text clipped - 47 lines] > > > > > > > > TIA, Elton W - 26 Oct 2005 15:20 GMT Check the datagrid's EnableViewState, if it's false, enable it. Disabled EnableViewState causes datagrid not function properly.
HTH
Elton
> Elton, > [quoted text clipped - 63 lines] > > > > > > > > > > TIA, Joe - 26 Oct 2005 16:57 GMT Elton,
Do you know if disabling the page's viewstate diaables the viewstates of the controls on teh page?
TIA,
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> Check the datagrid's EnableViewState, if it's false, enable it. Disabled > EnableViewState causes datagrid not function properly. [quoted text clipped - 70 lines] > > > > > > > > > > > > TIA, Joe - 26 Oct 2005 17:00 GMT EnableViewState is on. Any other ideas?
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> Check the datagrid's EnableViewState, if it's false, enable it. Disabled > EnableViewState causes datagrid not function properly. [quoted text clipped - 70 lines] > > > > > > > > > > > > TIA, Elton W - 26 Oct 2005 17:25 GMT Set breakpoints in your code and trace running step by step to see what happens. It might help.
> EnableViewState is on. Any other ideas? > [quoted text clipped - 72 lines] > > > > > > > > > > > > > > TIA, Joe - 26 Oct 2005 17:49 GMT Hi Elton,
The company that I work for requires that page-level ViewState be turned off. So, just to try it, I turned page-level Viewstate on and the code works. I have the impression that if the page-level ViewState is turned off, the individual control's viewstates will not work, regardless of whether or not they are turned on. Do you know anything about this?
I will see if I can find another solution. I have thought of using AddHandler to link Sub declarations to the LinkButton's Click event. This has worked with some success in the past and might provide a solution now.
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> Set breakpoints in your code and trace running step by step to see what > happens. It might help. [quoted text clipped - 75 lines] > > > > > > > > > > > > > > > > TIA, Elton W - 26 Oct 2005 18:51 GMT You can try another way. When postback, in Page_Load event, re-bind the datagrid’s data source. The disadvantage is that if you need collect user changed data from the datagrid, the re-binding will overwriting user changed data.
HTH
Elton
> Hi Elton, > [quoted text clipped - 87 lines] > > > > > > > > > > > > > > > > > > TIA, Joe - 26 Oct 2005 19:13 GMT Elton,
The user can't change any data in the datagrid. We use it solely for display purposes.
I'm going to post this in today's posts (I am switching back and forth between too many windows and need to consolidate.). Please feel free to continue our discussion. I appreciate your suggestions.
 Signature Joe
VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
> You can try another way. When postback, in Page_Load event, re-bind the > datagrid’s data source. The disadvantage is that if you need collect user [quoted text clipped - 96 lines] > > > > > > > > > > > > > > > > > > > > TIA, Elton W - 26 Oct 2005 20:51 GMT Actually, I think once you re-bind datagrid’s data source in postback, datagrid’s events will work event disabled datagrid viewstate. Anyhow, you can try.
HTH
Elton
> Elton, > [quoted text clipped - 105 lines] > > > > > > > > > > > > > > > > > > > > > > TIA,
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|