Scenario: in some environments, we need to determine the parameters of a (some) hyperlink based on the values of JavaScript variables on the page, such as"Http://www.bla.com/test.aspx? Var1 ="To determine how much var1 is equal to, it must be determined based on JavaScript variables.
Method 1: simple, client script
<AHref= "#"Onclick= "Redirecturl ();"Class= "Parent">Mark</A>
Function Redirecturl (){
VaR Jsvar1;
VaR Jsvar2;
Window. Location="Http://www.bla.com/test.aspx? Var1 ="+Jsvar1;//Jsvar1 is a javascript variable.
}
Method 2: Assign the easiest way wocould be to add the runat = "server" property to the anchor and set the href in the codebehind when loading the server
<AID= "Youranchor"Href= "#"Class= "Parent"Runat= "Server">Mark</A>
Private VoidPage_load (Object Sender, system. eventargs E)
{
Youranchor. href= Redirecturl ();
}
private string redirecturl ()
{< br> string jsvar1 = " " ;
string jsvar2 = " " ;
Return "Http://www.bla.com/test.aspx? Var1 =" +Jsvar1;
}
Method 3: the customer complained that there are many such hyperlinks on the page, so it is not suitable to write an onclick script for each hyperlink. After thinking about it, there are also ways to facilitate the management of a large number of dynamic hyperlinks:
<!Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HtmlXmlns= "Http://www.w3.org/1999/xhtml">
<HeadRunat= "Server">
<Title>Untitled page</Title>
<ScriptLanguage= "JavaScript"Type= "Text/JavaScript">
//<! CDATA [
VaRJsvar1="222"
Function redir (href)
{< br> document. getelementbyid ( " hidden1 " ). value = (href + Jsvar1 );
Document. getelementbyid ("Button1" ). Click ();
}
// ]>
</Script>
</Head>
<Body>
<FormID= "Form1"Runat= "Server">
<Div>
<AHref= "Http://www.bla.com/test.aspx? Var1 ="Onclick= "Redir (this. href); Return false ;">Mark</A>
</Div>
<ASP: buttonStyle= "Display: none"ID= "Button1"Runat= "Server"Text= "Button"Onclick= "Button#click" />
<InputID= "Hidden1"Type= "Hidden"Runat= "Server" />
</Form>
</Body>
</Html>
Protected VoidButton#click (Object sender, eventargs e)
{< br> scriptmanager. registerstartupscript (page, This . getType (), " key " , " window. location = ' " + This . hidden1.value + " ' " , true );
}