简体   繁体   中英

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.

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

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 Eg

$(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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM