0

I have requirement where users are redirected youtube.com by using hyperlink control using below

I want to change the URL dynamically based on drop down list selected item by using below code.

protected void ddlPType_SelectedIndexChanged(object sender, EventArgs e) { int x = ddlPType.SelectedIndex; if (x == 0) { activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ"; activateCerts.Text = "activateCerts"; activateCerts.Target = "_blank"; //activateCerts.HRef = "http://www.youtube.com/watch?v=3AYoipyqOkQ"; } else if (x == 1) { //activateCerts.Target = "_blank"; //activateCerts.HRef = "http://www.youtube.com/watch?v=hk3hxUuwg0w"; activateCerts.Text = "activateCerts"; activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ"; } 

and this is the one aspx code

<asp:Label runat="server" style="padding-left:23rem;" Text="pls watch this video on How to"></asp:Label> <asp:HyperLink ID="activateCerts" runat="server"></asp:HyperLink> 

but when I click on link I am not able to open a youtube video

2
  • 3
    Check AutoPostBack=true for dropdpwn ddlPType Commented Nov 27, 2014 at 6:09
  • checked auto postback is true and I solved the problem by putting this check on pageLoad Commented Nov 27, 2014 at 6:15

2 Answers 2

1

This is working for me by setting AutoPostBack=true for dropdpwn ddlPType :

<form id="form1" runat="server"> <div> <asp:DropDownList runat="server" ID="ddlPType" AutoPostBack="true" OnSelectedIndexChanged="ddlPType_SelectedIndexChanged"> <asp:ListItem Text="Option 1" Selected="True" /> <asp:ListItem Text="Option 2" /> </asp:DropDownList> <br /> <asp:Label ID="Label1" runat="server" style="padding-left:23rem;" Text="pls watch this video on How to"></asp:Label> <asp:HyperLink ID="activateCerts" runat="server"></asp:HyperLink> </div> </form> 

.cs Page :

 protected void ddlPType_SelectedIndexChanged(object sender, EventArgs e) { int x = ddlPType.SelectedIndex; if (x == 0) { activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ"; activateCerts.Text = "activateCerts"; activateCerts.Target = "_blank"; //activateCerts.HRef = "http://www.youtube.com/watch?v=3AYoipyqOkQ"; } else if (x == 1) { //activateCerts.Target = "_blank"; //activateCerts.HRef = "http://www.youtube.com/watch?v=hk3hxUuwg0w"; activateCerts.Text = "activateCerts"; activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ"; } } 
Sign up to request clarification or add additional context in comments.

Comments

0

For your Dropdownlist named ddlPType, you need to make sure its AutoPostBack is true. You can set it in the Attribute Panel, or using the code:

<asp:DropDownList runat="server" ID="ddlPType" AutoPostBack="true" OnSelectedIndexChanged="ddlPType_SelectedIndexChanged"> 

By this step you should achieve your goal, but sometimes this is not that simple. You may need to make sure you put your data-bind (if there is) in

if (!Page.IsPostBack)
in Page_Load.

Also, Dropdownlist will only send data when the data is changed. That is to say, if you get two options sharing the same value, Dropdownlist may not respond you. For example:

if(!IsPostBack) { for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value")); } 

Here comes the most strange situation: you have done all above but it still does not work. Sometimes it happens in IE8. If you use window.showModalDialog() to show DropDownList, submitting will leads you to a new page. You need to add between head tag:

<base target=_self></base> 

Hope my experience will do help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.