Esraa_92
Esraa_92

Reputation: 1568

How can I add a hyperlink to a dropdownlist create in vb.net code behind?

I create a dropdownlist in code behind with this:

Public sub CreateDDL()

 Dim ddl As New DropDownList
 Dim list As ListItem = New ListItem()
            list.Text = "printTemplate1"
            list.value = "~/template1.aspx"
            ddl.Items.Add(list)
End Sub

I don't know how to put the value to be a link. Suggest me, thanks.

Upvotes: 1

Views: 1494

Answers (2)

Esraa_92
Esraa_92

Reputation: 1568

I solve my problem at this way :

Public sub CreateDDL()
 Dim ddl As New DropDownList
 ' ############# THE MODIFICATION ########################
 ddl.Attributes.Add("onchange", "template1.aspx")
' ##################################################
 Dim list As ListItem = New ListItem()
            list.Text = "printTemplate1"         
            ddl.Items.Add(list)
End Sub

I hope it will be helpful to someone

Upvotes: 3

Suprabhat Biswal
Suprabhat Biswal

Reputation: 3216

What you are looking for is quite not possible because DropDownList render themselves into native HTML select. These controls aren't really designed to do such kind of activity.

In order to make them navigate to other page you need to combine them with client side script and make them behave as per your requirement. For E.g.

$(function() {
    $("#<%=ddl.ClientID%>").change(function(e) {
        var selectedUrl = $(this).val();
        window.location.href = selectedUrl;
    });
});

Also you can make use of ResolveURL() or ResolveClientUrl() to create a relative path to the root or relative to the current page respectively and then assign them to the ddl value.

list.value = ResolveUrl("~/template1.aspx"); 

/*or*/

list.value = ResolveClientUrl("~/template1.aspx");

Upvotes: 2

Related Questions