Execute Hyperlink Programmatically

R

Rob Rogers

My code creates a dynamic hyperlink from drop down list, and a
textbox, but has to wait for autopostback to complete for the
hyperlink to be valid. So is there a way to execute my hyperlink
programmatically in my "SetState" proc that sets up the hyperlink url.
I have to have the target frame, so I can't use Response.Redirect.

Here is my code.

<%@Page Explicit="True" Language="VB" Debug="True" %>
<HTML>
<script runat="server">
Sub SetState(Sender As Object, E As EventArgs)

HyperLink1.NavigateUrl = "http://www.natltc.com/search.aspx?St=" &
JumpTo.SelectedItem.Value & "?City=" & CityBox.text

End Sub



</script>
<body>
<h1>City &amp; State<BR>
</h1>
<form runat="server">
<P>
<asp:TextBox id="CityBox" runat="server" autopostback="true"
OnTextChanged= "SetState"></asp:TextBox><BR>
Enter City</P>
<P>&nbsp;</P>
<P>
<asp:dropdownlist id="JumpTo" runat="server" autopostback="true"
OnSelectedIndexChanged="SetState">
<asp:listitem></asp:listitem>
<asp:listitem>AL</asp:listitem>
<asp:listitem>AK</asp:listitem>
<asp:listitem>AZ</asp:listitem>
<asp:listitem>TX</asp:listitem>
<asp:listitem>OK</asp:listitem>

</asp:dropdownlist><BR>
Select State</P>
<P><br>
<br>
</ASP:IMAGEBUTTON>
<asp:HyperLink id="HyperLink1" runat="server" Target="mainFrame"
NavigateUrl="http://www.natltc.com/search.aspx">HyperLink</asp:HyperLink></P>
</form>
</body>
</HTML>
 
G

Girish Bharadwaj

I dont think you can do that from SetState. But, what you can do is to set a
hidden field in your SetState().
And in a client side javascript, create a function which basically uses that
hidden field to find out if it needs to do a window.open() or a
window.navigate() to the hyperlink.
Or, better still, write client side scripts to hook into to those drop down
selection and text input to construct the hyperlink within client without a
postback.
HTH?
 
S

Shiva

Other option would be to use client-side JavaScript to dynamically set the
anchor tag's href.

Eg:
<script>
function changeUrl()
{
var dx = window.document.forms[0].ddl;
ac.href = "http://www.google.com?q=" + dx.options[dx.selectedIndex].text;
}
</script>
<form>
select id="ddl" onChange="changeUrl();">
<option>C#</option>
<option>J#</option>
</select>
<a href="#" id="ac" target="mainFrame">Google</a>
</form>

Hope this helps.

My code creates a dynamic hyperlink from drop down list, and a
textbox, but has to wait for autopostback to complete for the
hyperlink to be valid. So is there a way to execute my hyperlink
programmatically in my "SetState" proc that sets up the hyperlink url.
I have to have the target frame, so I can't use Response.Redirect.

Here is my code.

<%@Page Explicit="True" Language="VB" Debug="True" %>
<HTML>
<script runat="server">
Sub SetState(Sender As Object, E As EventArgs)

HyperLink1.NavigateUrl = "http://www.natltc.com/search.aspx?St=" &
JumpTo.SelectedItem.Value & "?City=" & CityBox.text

End Sub



</script>
<body>
<h1>City &amp; State<BR>
</h1>
<form runat="server">
<P>
<asp:TextBox id="CityBox" runat="server" autopostback="true"
OnTextChanged= "SetState"></asp:TextBox><BR>
Enter City</P>
<P>&nbsp;</P>
<P>
<asp:dropdownlist id="JumpTo" runat="server" autopostback="true"
OnSelectedIndexChanged="SetState">
<asp:listitem></asp:listitem>
<asp:listitem>AL</asp:listitem>
<asp:listitem>AK</asp:listitem>
<asp:listitem>AZ</asp:listitem>
<asp:listitem>TX</asp:listitem>
<asp:listitem>OK</asp:listitem>

</asp:dropdownlist><BR>
Select State</P>
<P><br>
<br>
</ASP:IMAGEBUTTON>
<asp:HyperLink id="HyperLink1" runat="server" Target="mainFrame"
</form>
</body>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top