0

I have ASP.NET Web Application Project.In which i want to Pass Value to a function in javascript from c# code-behind page.

My C# Code Snippet:-

//Here td_Output is TableCell

td_Output.Text = "<input type='button' style='font-size:11px;' onclick='LoadLoayaltyProgram(1,'"+Convert.ToString(Request.Form["Program"])+"');' class='ui-button ui-widget ui-state-default ui-corner-all' role='button' value='Add' />";

JavaScript Code :-

function LoadLoayaltyProgram(PID,ID) {
    alert(PID +" "+ ID);
}

When i do Inspect Element on .aspx Page in browser i see code Like :

<input type="button" style="font-size:11px;" onclick="LoadLoayaltyProgram(1," pid112002');'="" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" value="Add">

I am not getting any alert on click of "Add" button

2 Answers 2

2

In LoadLoayaltyProgram you are printing consequtive ' . So, escape the " with \" so you can reuse it with something like this:

string program = Server.UrlEncode(Request.Form["Program"] ?? string.Empty);
td_Output.Text = String.Format("<input type='button' style='font-size:11px;' onclick='LoadLoayaltyProgram(1, \"{0}\");' class='ui-button ui-widget ui-state-default ui-corner-all' role='button' value='Add' />", program);
Sign up to request clarification or add additional context in comments.

Comments

1
  onclick="LoadLoayaltyProgram(1," pid112002');'="" 

this look very strange shouldnt it be something like?

  onclick="LoadLoayaltyProgram(1, 'pid112002')" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.