0

I need to pass a value to javascript from code behind.

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "<script type='text/javascript'>OpenPopUp();</script>", false);

JS:

function OpenPopUp(parameterValue) {           
        paramframe = parameterValue;            
        openmodal(paramframe);
    }

So parameterValue will have ID.Text value which is passed from code behind.

I need to pass a value from code behind in this function OpenPopUp to javascript.

I tried below code and the javascript doesn't fire. Am I passing the value correct to JS.

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "<script type='text/javascript'>OpenPopUp(" + ID.Text + ");</script>", false);

ID.Text is a ItemTemplate value of a LinkButton in a gridview.

9
  • Do you have any console error in your browser? Commented Dec 17, 2018 at 11:11
  • Does it work without passing the parameter? Commented Dec 17, 2018 at 11:16
  • @erikscandola - I'm not getting any error. The JS function doesn't fire when I send the value. Commented Dec 17, 2018 at 11:16
  • @Haldo - It works fine without passing the parameter. Commented Dec 17, 2018 at 11:17
  • 1
    @Learner I saw only a problem. If OpenPopUp(" + ID.text + "); will contains a text like Hello Word. So OpenPopUp(Hello Word) thrown an error. Try in this way: OpenPopUp('" + ID.text + "'); Commented Dec 17, 2018 at 11:20

2 Answers 2

2

try this, this should work

ScriptManager.RegisterStartupScript(this, typeof(string), "script1", "SampleJSFunction('" + vls_variable.Text+ "');", true);
Sign up to request clarification or add additional context in comments.

Comments

0

generally I would use a hidden parameter and access it from javascript and C# like this

ASP.NET

<asp:HiddenField runat="server" ID="HiddenFieldID" />

javascript

var FieldName= document.getElementById('<%=HiddenFieldID.ClientID%>').value;

and C#

var x = HiddenFieldID.Value

note that hidden fields have string as their value,. I prefer this way so that code is easier to maintain in the future.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.