locked
Hyper Link for the first column - dynamically RRS feed

  • Question

  • User1786833724 posted

    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.

    Monday, November 19, 2012 12:52 AM

Answers

  • User-126879547 posted
    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2012 1:42 AM
  • User1786833724 posted

    Thank you so much Ramesh Rajan ! It works like charm Smile 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});



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2012 1:10 PM

All replies

  • User3866881 posted

    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!

    Monday, November 19, 2012 8:27 PM
  • User1786833724 posted

    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 Frown

    Thanks for your help in advance :-)

    Wednesday, November 21, 2012 1:38 AM
  • User-126879547 posted
    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2012 1:42 AM
  • User3866881 posted

    I did tried that but with Eval I think I had to hardcode the column-name

    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.

    Wednesday, November 21, 2012 1:42 AM
  • User1786833724 posted

    Thanks Ramesh this would be the first thing I would try tomorrow in office.

    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)

    Wednesday, November 21, 2012 1:46 AM
  • User1786833724 posted

    Thank you so much Ramesh Rajan ! It works like charm Smile 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});



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2012 1:10 PM