Hi friends, I am working on a user control which is having dynamic data binding behaviour with sorting, paging and caching. I am using just the asp:GridView and asp:SqlDataSource in that .ascx file. Now I want to have a hyper link field for the first column
of my SqlDataSource - I've made arrangements for the field I want as hyper link to be always the first in my queries / stored procedures.
As it is going to be a user control there seems to possibilities to hardcode the column name, we' ought to be dynamic. Please share code snippets / scenarios / use ful links / work-arounds.
Since you only wanna Hyper Link for you first column, why need userControl?In fact I think you can need:
1)<asp:HyperLink……/>
2)<a href=……>XXX</a>
In fact, you can turn your first column to a customized one and then put the hyperlink in the template and then bind by using <%#Eval("……")%> or <%#Bind(……)%> dynamically.
Can you please post a small example ? I did tried that but with Eval I think I had to hardcode the column-name , Or possibly I am missing something. I tried using Datasource.columns[0].headertext ... and if I remeber correctly, I got an error with that.
I also gave a shot to "control type" proprty in the code behind (C#) ... but could not get it done
Thanks Ramesh this would be the first thing I would try tomorrow in office.
RameshRajendran
Be warned that if you bind data to grid only first time page loaded then your changes will disappear.
^ I think I can do paging and sorting stuff quite easily at the moment being (I am handling both events in the code behind) not sure if I am gonna have that issue (just guessing)
aarsh
Participant
1543 Points
428 Posts
Hyper Link for the first column - dynamically
Nov 19, 2012 04:52 AM|LINK
Hi friends, I am working on a user control which is having dynamic data binding behaviour with sorting, paging and caching. I am using just the asp:GridView and asp:SqlDataSource in that .ascx file. Now I want to have a hyper link field for the first column of my SqlDataSource - I've made arrangements for the field I want as hyper link to be always the first in my queries / stored procedures.
As it is going to be a user control there seems to possibilities to hardcode the column name, we' ought to be dynamic. Please share code snippets / scenarios / use ful links / work-arounds.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Hyper Link for the first column - dynamically
Nov 20, 2012 12:27 AM|LINK
Hello,
Since you only wanna Hyper Link for you first column, why need userControl?In fact I think you can need:
1)<asp:HyperLink……/>
2)<a href=……>XXX</a>
In fact, you can turn your first column to a customized one and then put the hyperlink in the template and then bind by using <%#Eval("……")%> or <%#Bind(……)%> dynamically.
Reguards!
aarsh
Participant
1543 Points
428 Posts
Re: Hyper Link for the first column - dynamically
Nov 21, 2012 05:38 AM|LINK
Can you please post a small example ? I did tried that but with Eval I think I had to hardcode the column-name , Or possibly I am missing something. I tried using Datasource.columns[0].headertext ... and if I remeber correctly, I got an error with that.
I also gave a shot to "control type" proprty in the code behind (C#) ... but could not get it done
Thanks for your help in advance :-)
RameshRajend...
Star
7983 Points
2099 Posts
Re: Hyper Link for the first column - dynamically
Nov 21, 2012 05:42 AM|LINK
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var firstCell = e.Row.Cells[0]; firstCell.Controls.Clear(); firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text }); } }Be warned that if you bind data to grid only first time page loaded then your changes will disappear.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Hyper Link for the first column - dynamically
Nov 21, 2012 05:42 AM|LINK
Hi again,
As far as I see,a fixed datatable binded to the data presentation control must be unique and fixed.
If you want to dynamically change the DataSource,you should make your GridView AutoGenerated Column=True and then do code behind to bind datasource.
aarsh
Participant
1543 Points
428 Posts
Re: Hyper Link for the first column - dynamically
Nov 21, 2012 05:46 AM|LINK
Thanks Ramesh this would be the first thing I would try tomorrow in office.
^ I think I can do paging and sorting stuff quite easily at the moment being (I am handling both events in the code behind) not sure if I am gonna have that issue (just guessing)
aarsh
Participant
1543 Points
428 Posts
Re: Hyper Link for the first column - dynamically
Dec 04, 2012 05:10 PM|LINK
Thank you so much Ramesh Rajan ! It works like charm
I just wanted to open it up in a new window so I
did this :
firstCell.Controls.Add(new HyperLink { NavigateUrl = "http://google.com/" + firstCell.Text, Target="_blank", Text = firstCell.Text});