?
Solved

PostBack from GridView problem

Posted on 2008-09-30
9
Medium Priority
?
1,362 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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