Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a hyperlink in a datagrid to go to a new page

Status
Not open for further replies.

HomerJS

Programmer
Jun 25, 2001
86
US
I have a datagrid on a web page with 9 columns. The 9th column is a hyperlink I want to go to Page2.aspx. Both the first and second columns of the datagrid are databound and unique in each row. The first column is bound to DocType and the second is bound to DocNo. I need to use DocType and DocNo on Page2.aspx to display more information about the record that was selected on the main page.

My question is how can I do this? (What event is triggered when you click on the hyperlink in the grid? How can I pass the correct values to the second page?)
 
Rather than using an event after the fact you could build the hyperlink with the values, DocNo and DocType into the URL as a querystring. You know ie.
You can do this using the item created event or check out this FAQ faq855-2613 or do some custom databinding on the link That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom,
Thanks for your help. I'm sorry but I'm still a newbie to a lot of this. Can you explain the following statement more:

'<%# DataBinder.Eval(Container.DataItem,&quot;price&quot;,&quot;Page2.aspx?id={0}&quot;) %>'

(i.e., What is the databinder? What is the container?)


The following is code I use to populate the datagrid on my webpage:

Dim adaptGrid As SqlClient.SqlDataAdapter
Dim tableGrid As New DataSet(&quot;Unsettled&quot;)

strSQL = &quot;select DocType, DocNo From DocTable&quot;

adaptGrid = New SqlClient.SqlDataAdapter(strSQL, con)
adaptGrid.Fill(tableGrid, &quot;Unsettled&quot;)

OtherGrid.DataSource = tableGrid
OtherGrid.DataBind()



 
Ok I've a question for you then. Do you know that your datagrid will always be filled with that table? If so you can make a strongly typed dataset and add it to your designer view. From there you can set the datagrid's DataSource and DataMember to the typed dataset and its Table. When you do this the designer creates the columns that will be used when the table is actually filled. The way that you are useing this is done at run time. If done at design time it is a bit easier to modify certain things. Such as making a custom link.

Now back to your question. The columns that the designer makes includes the line <%# DataBinder.Eval(Container.DataItem,&quot;price&quot;,&quot;Page2.aspx?id={0}&quot;) %>'
. What this does is bind the column to some data. Databinder is a .Net framework function that does this using the Enumerable interface. (loops through an array of data) The container bit is where the data comes from. In this case the container of the control this line is for. Ummm ... ya here we go, this is a complete line of html that binds a column to the data
<asp:TemplateColumn HeaderText=&quot;Value&quot;>
<ItemTemplate>
<asp:TextBox id=txtValue runat=&quot;server&quot; Text='<%# FormatStats(DataBinder.Eval(Container, &quot;DataItem.USER_MULTIPLIER&quot;)) %>' Width=&quot;29px&quot;>
</asp:TextBox>
</ItemTemplate>


Oh yes, You'll need to make the link column a template column. You can then edit that template to do what you wish. Jus sec I'll go find a good tutorial instead of trying to write everything down. Here we are. It's like nine parts so you can skip to the part that you need to look at. If I haven't already answered the question. Sorry to be so long winded. My fingers are fresh and feel like typing haha.

PS. The italicized words, if you are unsure of them just type them into the VS help or at MSDN if you don't have VS. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks for all of your help. The link to 4guysfromrolla.com is very, very helpful.

Ok, I almost have it right. When I try to append the '?DocNo=' before the actual DocNo the address I get back is:
databinder.eval(container.dataitem, &quot;DocNo&quot;) %>

However when I set the NavigateURL to just <%# databinder.eval(container.dataitem, &quot;DocNo&quot;) %> the address I get is:
(where 123456789 is the DocNo I need)

What am I doing wrong? I've tried using the '&' and the '+' to combine the two strings but it does not work. The HTML code is below.

DOES NOT WORK . . .

<asp:TemplateColumn HeaderText=&quot;Detail&quot;>
<HeaderTemplate>
<b>Other Detail</b>
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink Runat=server NavigateUrl='DetailOther.aspx?DocNo=<%# databinder.eval(container.dataitem, &quot;DocNo&quot;) %>' >Detl</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>


DOES WORK . . .

<asp:TemplateColumn>
<HeaderTemplate>
<b>Label</b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label text = '<%# databinder.eval(container.dataitem, &quot;DocNo&quot;)%>' Runat=server/>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
 
Glad that I could help. As for your Hyperlink you just need a few changes. What I have written below should work perfectly for ya. I am not sure why what you have written isn't working except that for some reason it see's the server tag's <%%> as text rather than tags. You might have to change it so the tags are outside the quotes. Like I said the below code will work though

<asp:TemplateColumn HeaderText=&quot;Detail&quot;>
<HeaderTemplate>
<b>Other Detail</b>
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink Runat=server NavigateUrl='<%# &quot;DetailOther.aspx?DocNo= &amp; databinder.eval(container.dataitem, &quot;DocNo&quot;) %>' Text='Detl></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>


Hope I have helped you. *

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Woo Hoo!! If you couldn't tell it worked. For whatever reason a quote was missing

. . . .aspx?DocNo=&quot; <--

but no biggy.

Thanks a lot again for all of your help. :)
 
oops typo glad to help That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top