c# - ASP.NET how does markup access to variables in code-behind? -


i have webform button:

<asp:button id="btn1" runat="server" text="click me"             onclientclick="printvideos(new object(),new eventargs(),url,linkname)"/> 

and onclientclick event is

<script language= "c#" type="text/c#" runat= "server">     private void printvideos(object sender, eventargs e, string url, string linkname) {          (int = 0; < 4; i++) {             response.write("<a href='"+url+"'target_blank>'"+linkname+"</a>");         }     } </script> 

where url , linkname defined in c# code-behind private strings within class.

the button not work, , there warnings showing url , linkname not used. should let markup access code-behind variables?

your first problem trying run server side code on client - change onclientclick onclick.

next, need set protection level of linkname , url properties aspx markup can access them. can set either public or protected (source). finally, remove arguments printvideos method - onclick handler expects specific method signature. so, button markup should like:

<asp:button id="btn1" runat="server" text="click me" onclick="printvideos"/> 

and script...

<script language= "c#" type="text/c#" runat= "server">     private void printvideos(object sender, eventargs e) {         (int = 0; < 4; i++) {             response.write("<a href='"+url+"'target_blank>'"+linkname+"</a>");         }     } </script> 

and in codebehind page, declaring variables:

protected string url = "..."; protected string linkname = "..."; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -