Need Suggestion

J

Jonathan Wood

Greetings,

My app displays users in a GridView control. I'd like to have the name field
shown in each row of this control a link that displays another page with
information about that user.

But I'm not sure how to approach this. How would I tell the target page
which user to display? I'd prefer not to pass the user's ID on the URL, and
I don't know when I'd get a chance to set a Session variable.

I know I can add a template with a link but, if I set an event handler for
that link, how does it know which row was selected?

Thanks for any tips.
 
E

Eliyahu Goldin

One way is to do Server.Transfer to the user info page on server side.

The link on the grid will fire just a postback, perhaps a RowCommand event,
the server will find what row fired the event, get the user id from the row,
put it in a session variable and transfer to the info page.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
J

Jonathan Wood

I've got a couple of ways to bring up another page. But, again, my issue is
providing the ID of the row the link was on.

So I couldn't quite follow your suggestion. Are you talking about built-in
row buttons, or something added via a template, or something else entirely?

That'd be a big help if you could clarify.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Eliyahu Goldin said:
One way is to do Server.Transfer to the user info page on server side.

The link on the grid will fire just a postback, perhaps a RowCommand
event, the server will find what row fired the event, get the user id from
the row, put it in a session variable and transfer to the info page.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jonathan Wood said:
Greetings,

My app displays users in a GridView control. I'd like to have the name
field shown in each row of this control a link that displays another page
with information about that user.

But I'm not sure how to approach this. How would I tell the target page
which user to display? I'd prefer not to pass the user's ID on the URL,
and I don't know when I'd get a chance to set a Session variable.

I know I can add a template with a link but, if I set an event handler
for that link, how does it know which row was selected?

Thanks for any tips.
 
J

Jonathan Wood

Am I completely up in the night here, or is that "selected row" business
restricted to gridviews that have a select button?

I want a link for each row (preferrably a hyperlink in actual data) to
direct the user to another page, and to somehow tell that page which row the
link was clicked on. There would be no separate step of selecting a row.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
 
M

Mark Rae [MVP]

is that "selected row" business restricted to gridviews that have a select
button?
No.

I want a link for each row (preferrably a hyperlink in actual data) to
direct the user to another page, and to somehow tell that page which row
the link was clicked on. There would be no separate step of selecting a
row.

There are several ways you could do this...

Since you don't fancy the SelectedRow method, why not create a
TemplateColumn and add a LinkButton to it...? An asp:LinkButton is a button
control which looks like a hyperlink. This means that you can set its
CommandArgument value during the RowBinding of the GridView which you can
use to set a Session variable when the hyperlink is clicked, and then do a
Response.Redirect to the other page:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx
 
J

Jonathan Wood

Mark,

I don't understand how can there be a selected row if a grid has no means to
select one?
Since you don't fancy the SelectedRow method, why not create a

Given my lack of knowledge on it, I have nothing against the SelectedRow
method. It's just that I'm not seeing how that would produce the effect that
I want.
TemplateColumn and add a LinkButton to it...? An asp:LinkButton is a
button control which looks like a hyperlink. This means that you can set
its CommandArgument value during the RowBinding of the GridView which you
can use to set a Session variable when the hyperlink is clicked, and then
do a Response.Redirect to the other page:

I followed this right up to the point where I set the CommandArgument value
during the RowBinding of the GridView. I see the GridView has no RowBinding
event. There is a RowDataBound event. I can modify the handler like this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Text = "What?";
}

I'm not clear on A) how I would access the LinkButton in this cell, B) how I
would respond to the link being clicked, and C) how I would know which row
the link was clicked on in my click handler.

Any chance you could fill this out a bit more for me?

Thanks.
 
M

Mark Rae [MVP]

I don't understand how can there be a selected row if a grid has no means
to select one?
http://www.google.co.uk/search?sour...idView+ClientScript.GetPostBackEventReference

I'm not clear on A) how I would access the LinkButton in this cell,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
((LinkButton)e.Row.Cells[0].FindControl("MyLinkButton")).CommandArgument
B) how I would respond to the link being clicked,

By specifying a Command event for the LinkButton in the TemplateColumn, same
way that you would respond to an said:
and C) how I would know which row the link was clicked on in my click
handler.

You wouldn't need to now, because you've included the data you need in the
LinkButton's CommandArgument property...

http://www.thescripts.com/forum/thread554224.html
 
J

Jonathan Wood

Mark,

I seem to be having trouble with this.

These articles still seem to be talking about making a row selectable.
That's not what I need.
I'm not clear on A) how I would access the LinkButton in this cell,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{

((LinkButton)e.Row.Cells[0].FindControl("MyLinkButton")).CommandArgument =
B) how I would respond to the link being clicked,

By specifying a Command event for the LinkButton in the TemplateColumn,
same way that you would respond to an said:
and C) how I would know which row the link was clicked on in my click
handler.

You wouldn't need to now, because you've included the data you need in the
LinkButton's CommandArgument property...

http://www.thescripts.com/forum/thread554224.html

Ack, okay, I think I finally wrapped my mind around this. I think I
incorporated some of what you were saying but kind of got the core from some
of the articles.

My GridView column looks like this:

<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkName" runat="server" Text='<%# Eval("Name") %>'
CommandName="ViewTrainer" CommandArgument='<%# Eval("UserId") %>'
onclick="lnkName_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

And then I can respond in either of two ways:

protected void lnkName_Click(object sender, EventArgs e)
{
Session["ID"] = ((LinkButton)sender).CommandArgument;
Response.Redirect("Trainer.aspx");
}

Or

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs
e)
{
if (e.CommandName == "ViewTrainer")
{
Session["ID"] = (string)e.CommandArgument;
Response.Redirect("Trainer.aspx");
}
}

The first version looks a little cleaner--I may go with that.

Thanks for your help.
 
R

Registered User

Greetings,

My app displays users in a GridView control. I'd like to have the name field
shown in each row of this control a link that displays another page with
information about that user.

But I'm not sure how to approach this. How would I tell the target page
which user to display? I'd prefer not to pass the user's ID on the URL, and
I don't know when I'd get a chance to set a Session variable.

I know I can add a template with a link but, if I set an event handler for
that link, how does it know which row was selected?

Thanks for any tips.

Look at cross-page posting.

regards
A.G.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top