Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PostBack from GridView problem

Posted on 2008-09-30
9
Medium Priority
?
1,370 Views
Last Modified: 2013-12-17
Hi Experts.

I have a bit of a problem and I'm hoping to find some help here.

I have a databound GridView with an extra colum that contains a hyperlink.
The plan is that clicking on the hyperlink should select the Row.

So here is the code on the GridView RowCreated event:

    protected void grdAddressTypes_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType==DataControlRowType.DataRow)
        {
            HyperLink hl = (HyperLink)e.Row.FindControl("hlDetails");
            hl.Text = "Details";
            hl.ID = e.Row.RowIndex.ToString();
            hl.NavigateUrl = string.Format("?Selected={0}", e.Row.RowIndex.ToString());
           
        }
    }

This way when you click on the Hyperlink it returns the Row Index ( identifying the row selection )

The problem is that after clicking on the HyperLink and the page is refreshed it returns as a new page causing problem with my Page Load event that has some  !Page.IsPostback code.

How can I avoid it and make sure that the GridView hyperlink click is recognized as a postback.
Is there a better solution maybe  ?

Thank you.
0
Comment
Question by:techsuppoprt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:prairiedog
ID: 22607572
Why not using the built-in "Select" command in GridView?
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22607711
Couldn't figure out for to "select the row" with it.

I'm open to alternative solutions.
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22607717
Please note that I"m not using VS2008 wizards. All databindings are done through code.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:prairiedog
ID: 22607916
When the HyperLink is cliced, what do you do?
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22607941
See the code in the initial post.

( hl.NavigateUrl = string.Format("?Selected={0}", e.Row.RowIndex.ToString()); )

When the link is clicked right now it just refreshes the page with the row index in the Query string.
That's how I know which row was selected, from the query string...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22608014
Let me ask you this way: what do you want to do? Or what are you trying to accomplish?
HyperLink will not trigger postback. Every click on it is a new request.
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22608085
When "Select" hyperlink is clicked I want the row, that the "Select" link is part of,  to be selected and the SelectedRowIndex to be recorded somewhere in the INT.

Later on I'm going to pull the data from the selected row into the DetailsView and various Text Boxes.
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 1600 total points
ID: 22608224
You should be able to do this with the built-in Select CommandField.
Add the Select CommandField in your GridView, and create GridView_SelectedIndexChanged event handler. The selected row index will be this.grdAddressTypes.SelectedIndex.
If you need to get the corresponding primary key of the selected row:
1. Specify GridView's DataKeyNames = "PrimaryKeyFieldName"
2. In SelectedIndexChanged event handler, do this:

int index = this.grdAddressTypes.SelectedIndex;
int i = int.Parse(this.grdAddressTypes.DataKeys[index].Values[0].ToString());

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22608310
Wow, seriously I even started laughing here after seeing how much easier it is than what I was trying to do.

Thank you.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question