[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.
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;
Re: [2005] Gridview and link button
I have figured it out using a link button as the databound data. Thanks mate anyway.
Re: [RESOLVED] [2005] Gridview and link button
That's pretty much the same thing but I generally prefer my method for more flexibility.
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. :wave:
Re: [RESOLVED] [2005] Gridview and link button
Let me add maintainability, scalability and babadoobalibility to my list. :p
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. :wave:
Can this be used for more then one field? ie MyId2 as well?
'<%# Eval("MyId", "MyId2" "showdetails.aspx?MyId={0}?????????"') %>'
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.
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
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.
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.
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.