LinkButton : How to navigate to a child in another page

B

Bazza Formez

Hi,

The intended functionality is as follows :

I want the user to be able to click on the LinkButton for an individual row
in a datagrid, server side sub grabs id for item selected, stores in session
& reirects user to new page which retrieves id from session & loads its own
grid accordingly.

Note that I don't want pass info as a parameter tacked on to the url... and
this is why I am not using a hyperlink.

My question(s) are :
(1) what is the syntax in the html to build the template column properly for
the LinkButon to acheive this.
(2) How do I tie this in the server side code and retrieve the item
selected.
(3) Also, Am I doing this the hard way. Is there a better way to pass data &
still avoid user seeing parameters ?

Thanks !

Bazza
 
J

John Saunders

Bazza Formez said:
Hi,

The intended functionality is as follows :

I want the user to be able to click on the LinkButton for an individual
row in a datagrid, server side sub grabs id for item selected, stores in
session & reirects user to new page which retrieves id from session &
loads its own grid accordingly.

Note that I don't want pass info as a parameter tacked on to the url...
and this is why I am not using a hyperlink.

My question(s) are :
(1) what is the syntax in the html to build the template column properly
for the LinkButon to acheive this.
(2) How do I tie this in the server side code and retrieve the item
selected.
(3) Also, Am I doing this the hard way. Is there a better way to pass data
& still avoid user seeing parameters ?

1) On one of my forms, I have the following:

<asp:TemplateColumn HeaderText="Buy?"><ItemTemplate>
<asp:LinkButton ID="lnkToShopFor" Runat="server"
CommandName="Toggle">No</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

This renders as:

<td>
<a id="grdShoppingList_ctl02_lnkToShopFor"
href="javascript:__doPostBack('grdShoppingList$ctl02$lnkToShopFor','')">No</a>
</td>

2) On the server side, you pick this up in the ItemCommand event of the data
grid. The EventArgs will pass you the DataGridItem which represents this row
in the grid, in the Item property. It will also pass you the CommandName and
CommandArgument properties from the LinkButton. If the ID you want to pass
in Session is the primary key of the DataTable you bound the grid to, and if
you specified the DataKey property of the DataGrid, then the grids DataKeys
property can be indexed by e.Item.ItemIndex to find the primary key for that
row. Otherwise, you can pass the id in the CommandArgument property through
data binding.

3) This it the better way. :) At base, it stores the values in hidden input
fields and then posts back. The receiving control (in this case, the
LinkButton) raises a PostBack event based on the values it finds in the
hidden fields. The event handler then deals with the event, or, in this
case, bubbles it up to its enclosing control (the DataGrid), for you to
handle in the ItemCommand event.

BTW, the other way to not pass parameters is for the client-side code to set
a cookie, but that's not pretty. The hidden form field method is better, and
letting the LinkButton and DataGrid do it for you is better still!

John Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top