Solved

PostBack from GridView problem

Posted on 2008-09-30
9
1,336 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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