Can we binding a variable to parameter of a Javascript function

A

ad

I write a javascript function, it require a string parameter:

<asp:TextBox ID="TextBox1" runat="server"
onclick="MyJavascript('Tiger');"></asp:TextBox>

I want bind a variable to the parameter, like:
<asp:TextBox ID="TextBox1"
runat="server"onclick="MyJavascript('<%#sName%>');"></asp:TextBox>

and write some code in the code behind file:

public string sName= "Tiger";
protected void Page_Load(object sender, EventArgs e)
{ DataBind(); }


But after run , it assign a null value the parameter!

How can I do?
 
C

Craig Deelsnyder

ad said:
I write a javascript function, it require a string parameter:

<asp:TextBox ID="TextBox1" runat="server"
onclick="MyJavascript('Tiger');"></asp:TextBox>

I want bind a variable to the parameter, like:
<asp:TextBox ID="TextBox1"
runat="server"onclick="MyJavascript('<%#sName%>');"></asp:TextBox>

and write some code in the code behind file:

public string sName= "Tiger";
protected void Page_Load(object sender, EventArgs e)
{ DataBind(); }


But after run , it assign a null value the parameter!

How can I do?

It's recommended that when you want to set an attribute corresponding to
an event on the clientside (in the rendered HTML), to do so using the
Attributes collection of the control. So take out the onclick="..." in
the aspx and in the code-behind do

TextBox1.Attributes["onclick"] = "MyJavaScript('hello');";
 
C

Craig Deelsnyder

Craig said:
ad said:
I write a javascript function, it require a string parameter:

<asp:TextBox ID="TextBox1" runat="server"
onclick="MyJavascript('Tiger');"></asp:TextBox>

I want bind a variable to the parameter, like:
<asp:TextBox ID="TextBox1"
runat="server"onclick="MyJavascript('<%#sName%>');"></asp:TextBox>

and write some code in the code behind file:

public string sName= "Tiger";
protected void Page_Load(object sender, EventArgs e)
{ DataBind(); }


But after run , it assign a null value the parameter!

How can I do?

It's recommended that when you want to set an attribute corresponding to
an event on the clientside (in the rendered HTML), to do so using the
Attributes collection of the control. So take out the onclick="..." in
the aspx and in the code-behind do

TextBox1.Attributes["onclick"] = "MyJavaScript('hello');";
Sorry, for your example:

TextBox1.Attributes["onclick"] = "MyJavaScript('" + sName + "');";

just in case :)
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top