Solved

PostBack from GridView problem

Posted on 2008-09-30
9
1,307 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
Comment Utility
Why not using the built-in "Select" command in GridView?
0
 
LVL 1

Author Comment

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

I'm open to alternative solutions.
0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
Please note that I"m not using VS2008 wizards. All databindings are done through code.
0
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
When the HyperLink is cliced, what do you do?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pass through dll 2 33
.Net application crashing 6 19
JavaScript error 1 32
Make a border less form movable 2 10
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

6 Experts available now in Live!

Get 1:1 Help Now