Solved

PostBack from GridView problem

Posted on 2008-09-30
9
1,320 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
  • 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
 
LVL 22

Expert Comment

by:prairiedog
ID: 22607916
When the HyperLink is cliced, what do you do?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 400 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now