You must Sign In to post a response.
  • Category: ASP.NET

    Hyperlink on header in asp.net

    Hi All,

    How can I make the below query as hyperlink:

    m += "<h1>" + reader["Title"].ToString() + "</h1>";

    Please advice.
  • #766812
    Hi,

    You should pass the same with in anchor tags, but as I seen your increment / concatenate something to your code with in header tags, my suggestion is after complete the increment / concatenate you just append the header tags then that will give better result, and append anchor tags for the same, but you should provide the appropriate URL for the same for redirecting into that page.

    Ex:

    <a href="Your URL">Your Text</a>


    Hope this will helpful to you....

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766826
    Hi naveensanagasetti,

    Here, headers are displaying from database dynamically and I want to make those header as hyperlink.

    Thanks,
    Ram Prasad

  • #766827
    Hi Ram,

    You have to concatenate the text, once you get the data from database you have to append the text to the one object and pass that object in anchor tag,

    Ex:

    string result=string.Empty;
    DataSet ds=//data from database;

    if(ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)
    {
    result=ds.Tables[0].Rows[0]["Column"].ToString();
    }


    Now, in result object you have the content you just append the content to anchor tag like below.

    <a href="your URL">"+result+"</a>

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766829
    Hi

    try this code working good check them

    Client Side

    <div>
    <asp:Button ID="Btn1" runat="server" Text="submit" OnClick="Btn1_Click" />
    </div>


    Server Side

    protected void Btn1_Click(object sender, EventArgs e)
    {
    SqlCommand command = new SqlCommand("SELECT EmpGender, EmpName FROM tblEmployee;", connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    if (reader.HasRows)
    {
    while (reader.Read())
    {
    HyperLink hyp = new HyperLink();
    hyp.Text = "Click Me";
    hyp.NavigateUrl = "<h 1>" + reader["EmpName"].ToString() + "</h1>";
    Page.Controls.Add(hyp);
    }
    }
    else
    {
    Console.WriteLine("No rows found.");
    }
    reader.Close();
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766830
    Hi

    Can you share the purpose of Hyperlink. Because you did not mention navigation url so.

    In my previous post i have mention Which is Database Records read and set hyperlink field.

    But can you share navigate url then i will alter and repost them.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766836
    Hi,

    Below is my code, here h1 tag comes from the database. I want to make this h1 tag as byperlink to redirect new url

    while (reader.Read())
    {
    m += " <div class='col-md-3 col-xs-12 marginBottom10'> ";
    m += " <div class='box box4'>";
    m += " < h1 >" + reader["Title"].ToString() + "</h1 >"; //this h1 will be hyperlink and will redirect new url
    m += " <div class='col-md-4 col-sm-4 col-xs-12 paddingLeft0'>";
    m += " <img src = 'ui/media/images/box4.jpg' alt=''>";
    m += " </div>";
    m += " <div class='col-md-8 col-sm-8 col-xs-12 paddingLeft0'>";
    m += GetDetails(Convert.ToInt32(reader["MasteId"].ToString()));
    m += " </div>";
    m += " </div>";
    m += " </div>";
    }

    Thanks,
    Ram Prasad

  • #766839
    Hi Ram,

    I am not sure whether you are using any of Data bound control or not.
    Here is a piece of code, that I used to make similar functionality. I have created a dynamic hyperlink based on tag and id from database and then redirected the dynamic page using Web.Routing.

    <h2>
    <a href='<%# String.Format("Song/{0}/{1}.aspx",Eval("SongID"),((string)Eval("SongURL")).Replace(" ", "-"))%>' runat="server">
    <asp:Label ID="lblSongHeading" runat="server" Text='<%#Eval("SongTitle")%>'></asp:Label></h2>

    View live demo at media.johnbhatt.com. On the frontpage, the song title is the URL generated on the fly. Only difference is I am using DataList control to repeat the similar structure and bind data from database.

    Here is the step by step article on how to implement URL re-writing in ASP.NET projects.
    https://www.pyarb.com/asp-net/url-re-write-using-global-asax-in-asp-net.html

    Hope this helps.

    -------------
    Glad to be,
    John Bhatt
    Editor - DNS Forums
    https://www.pyarb.com

  • #766840
    Try avoid using font tag because it's deprecated. Use CSS like above instead. You can give your anchors specific class and apply any style for them.
    see below snippet

    a {font-size: 100px}

    To separate the content from the styling, you should of course work towards putting the CSS in a style sheet rather than as inline style attributes. That way you can apply one style to several elements without having to put the same style attribute in all of them.
    see below snippet with style tag

    < a href="selectTopic?html" style="font-size: 100px; text-decoration: none">HTML 5< /a>

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766848
    Hi

    try this code


    Create Table UrlList
    (
    id int primary key identity(1,1),
    Title varchar(100)
    )

    Insert into UrlList values('Testp.aspx')
    Insert into UrlList values('Test1.aspx')



    C# Code


    //Response.Redirect("https://www.google.co.in");

    SqlCommand command = new SqlCommand("SELECT Id, Title FROM UrlList;", connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    if (reader.HasRows)
    {
    while (reader.Read())
    {
    HyperLink hyp = new HyperLink();
    hyp.Text = "Click Me";
    hyp.NavigateUrl = reader["Title"].ToString() ;
    Page.Controls.Add(hyp);
    }
    }
    else
    {
    Console.WriteLine("No rows found.");
    }
    reader.Close();

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766854
    Hi,
    Try with below mentioned code,

    <asp:GridView runat="server" ID="grd">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:HyperLink runat="server" NavigateUrl="YourURL"> </asp:HyperLink>
    </HeaderTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

  • #766899
    You do it in two ways. Following is the basic syntax for creating the link.
    < a href="URL">  Test < /a >


    In your case you are trying to create link dynamically. You can try anyone of the following way.
     m += "< h1>< a href=URL>" + reader["Title"].ToString() + "< /a >< /h1>";
    m += "< a href=URL>"< h1> + reader["Title"].ToString() + "< /h1 >< /a >";

    By Nathan
    Direction is important than speed


Sign In to post your comments