Shaggy
Shaggy

Reputation: 5790

Pass function parameter from c# code behind

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

Upvotes: 0

Views: 524

Answers (2)

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

this look very strange shouldnt it be something like?

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

Upvotes: 1

Roland Mai
Roland Mai

Reputation: 31077

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);

Upvotes: 2

Related Questions