Results 1 to 12 of 12

Thread: [RESOLVED] [2005] Gridview and link button

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Singapore
    Posts
    34

    Resolved [RESOLVED] [2005] Gridview and link button

    Hi all

    I have a gridview control bind to sql server. I have 6 column names on my grid and 1 additional column which is a link button named 'view'. When data is generated, i want to click on the 'view' link button and show the particular details for that row. (Eg.First column is ID, means show details of the ID).

    I want to know how can i do that and how to write the code for the link button.

    Many thanks.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Gridview and link button

    Create a new page which will show the details of the data row clicked, say showdetails.aspx

    Have showdetails.aspx accept a querystring parameter which will be passed to it as a result of clicking one of the hyperlinks.

    You will of course also need to modify the NavigateURL properties of the hyperlinks in your hyperlink column. To do this, handle the GridView's RowDataBound event. It is hit once for each row in the GridView, so you can find the hyperlink control using something like

    e.Row.Cells[6].FindControl("hylDetails")

    But make sure to cast it to the type HyperLink first, before you modify its NavigateUrl property to be something like

    "showdetails.aspx?id=" + strID;

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2007
    Location
    Singapore
    Posts
    34

    Re: [2005] Gridview and link button

    I have figured it out using a link button as the databound data. Thanks mate anyway.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2005] Gridview and link button

    That's pretty much the same thing but I generally prefer my method for more flexibility.

  5. #5
    Hyperactive Member kayos's Avatar
    Join Date
    Apr 2004
    Location
    Largo, Florida
    Posts
    306

    Re: [RESOLVED] [2005] Gridview and link button

    Quote Originally Posted by mendhak
    That's pretty much the same thing but I generally prefer my method for more flexibility.
    Well for flexibility, yes, but in your itemtemplate you can simply set the NavigateUrl property like this:

    NavigateUrl='<%# Eval("MyId", "showdetails.aspx?MyId={0}"') %>'

    for simplicity sake.


    If this post helps, please RATE MY POST!

    Using Visual Studio 2005 SE

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2005] Gridview and link button

    Let me add maintainability, scalability and babadoobalibility to my list.

  7. #7
    Lively Member
    Join Date
    Aug 2006
    Location
    Sweden
    Posts
    114

    Re: [RESOLVED] [2005] Gridview and link button

    Quote Originally Posted by kayos
    Well for flexibility, yes, but in your itemtemplate you can simply set the NavigateUrl property like this:

    NavigateUrl='<%# Eval("MyId", "showdetails.aspx?MyId={0}"') %>'

    for simplicity sake.

    Can this be used for more then one field? ie MyId2 as well?

    '<%# Eval("MyId", "MyId2" "showdetails.aspx?MyId={0}?????????"') %>'
    Bert
    ___________________________________________

  8. #8
    Hyperactive Member kayos's Avatar
    Join Date
    Apr 2004
    Location
    Largo, Florida
    Posts
    306

    Re: [RESOLVED] [2005] Gridview and link button

    Quote Originally Posted by AlphaOne
    Can this be used for more then one field? ie MyId2 as well?

    '<%# Eval("MyId", "MyId2" "showdetails.aspx?MyId={0}?????????"') %>'
    no, unfortunately this is a single field solution.

    if you want multifield then you will have to use something like what mendhak was talking about.


    If this post helps, please RATE MY POST!

    Using Visual Studio 2005 SE

  9. #9
    Lively Member
    Join Date
    Aug 2006
    Location
    Sweden
    Posts
    114

    Re: [RESOLVED] [2005] Gridview and link button

    Hmmm....thats not realy true I did this:
    <%# String.Format("reserve.aspx?Id={0}&PID={1}", DataBinder.Eval(Container.DataItem,"ID"), DataBinder.Eval(Container.DataItem,"PropertyId")) %>

    This can be extended as long as you will need:-)

    Bert
    Bert
    ___________________________________________

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2005] Gridview and link button

    That code actually made me shudder.

    I don't think people actually understand the negatives of inserting values that way.

  11. #11
    Lively Member
    Join Date
    Aug 2006
    Location
    Sweden
    Posts
    114

    Re: [RESOLVED] [2005] Gridview and link button

    Maybe you explane why this is negative. I got this solution out od Microsofts website and I guess if they think its ok Its good for me.
    Bert
    ___________________________________________

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2005] Gridview and link button

    The reasons I'm against code like:

    <%# Eval("MyId", "MyId2" "showdetails.aspx?MyId={0}?????????"') %>

    inserted into the page's source are:

    1) Mixing of UI logic with UI elements. Always keep them separate.
    2) Not scalable. Becomes more difficult to debug later. If you handle this in the DataBound events, you have more control over it and it's easier to debug later, especially when a new programmer comes in and sees this wonderful throwback to ASP Classic style of coding.
    3) This code is executed after the object for the page is created and executed. That means there's two runs over for the same object and its methods. Inefficient.
    4) This may be on Microsoft's website, but Microsoft also created ActiveX and we thought it was a good idea. Just because a lot of people use it doesn't make it right. It makes a lot of people wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width