views:

695

answers:

4

i m trying to create a div container in c# codebehind.i want the div container to raise a javascript event onclick once its clicked.its working fine.below is the code where a javascript function "ds()" is called with no parameters.the problem is that i want to pass a parameter to javascript function.I dont know how to accomplish it.Kindly facilitate me.

//string userIcon = "gbn";

sb.Append(userIcon + @"<div onclick=""ds()"" style='background-color: #EFEFEF; padding: 10px;'>" + "<a href=#>" + "MubbshirAbbs" + "</a></div><br>");

Literal1.Text = sb.ToString();

The Javascript function is this:

function ds()
{
  var a="";alert("123");
  document.getElementById("TextBox1").style.backgroundColor="red";
}

Kindly let me know how can i add a parameter in the div onclick function.

+1  A: 

Alter the C# code:

sb.Append(userIcon);
sb.Append(@"<div onclick=""ds(");
sb.Append("'clicked'");
sb.Append(@")"" style='background-color: #EFEFEF; padding: 10px;'>");
sb.Append("<a href=#>");
sb.Append("MubbshirAbbs");
sb.Append("</a></div><br>");

Alter the javascript:

function ds(param)
{
  var a="";alert("123");
  document.getElementById("TextBox1").style.backgroundColor="red";
}
John Fisher
A: 

I don't know if I get your problem, can't you just do this?:

sb.Append(userIcon + @"<div onclick=""ds(" + parameter + ")"" style='background-color: #EFEFEF; padding: 10px;'>" + "<a href=#>" + "MubbshirAbbs" + "</a></div><br>");
Literal1.Text = sb.ToString();
function ds(parameter)
{
var a="";alert(parameter);
document.getElementById("TextBox1").style.backgroundColor="red";
}
Sebastian
A: 

@sebastian, i tried it, but in vain.

Mubbashir
A: 

@ john fisher

it didnt work

i replaced this:

sb.Append( yourParameter );

with this

sb.Append("clicked");

where clicked for instance is the value i need to pass to javascript function.

and the javascript code with

function ds(s) { alert(s);

document.getElementById("TextBox1").style.backgroundColor = "red";

}

Kindly help me bro

Mubbashir
Check the edits to my answer. You need to pass quotation marks that the javascript will use to identify the word 'clicked' as a string.
John Fisher