Can we binding a variable to parameter of a Javascript function

  • Thread starter Thread starter ad
  • Start date Start date
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?
 
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');";
 
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 :)
 
Back
Top