0

Trying to add a Javascript on a hyperlink click event. Here, I get the Name of the hyper link, but nothing happens when i try to click on the hyperlink.

tblCell = new HtmlTableCell();
List<string> liEmailIdsForCC = new List<string>();
if(some condition)
{
liEmailIdsForCC .add("someitem");
}

HyperLink hpOwnerName = new HyperLink();
hpOwnerName.Text += string.Format("({0})  ", "FullName");
string args = string.Format("{0},{1}"
                           , this
                           , liEmailIdsForCC                           
                           );

var onClick = String.Format("javascript:OnNameClick({0});return false;", args);
hpOwnerName.Attributes.Add("onclick", onClick);
tblCell.Controls.Add(hpOwnerName);

Now, in this javascript, I am just trying to alert the first item in the list

function OnNameClick(sender, list) {
    alert(li[0]);
}
5
  • But li[0] where is it? It does not sending like a parameters. Commented Jan 13, 2015 at 15:56
  • Why are you sending a comma delimited string as an argument? You are also converting this to a string, which will not be what you want. And as @WilfredoP states, in your javascript function li doesnt exist. I think you mean list Commented Jan 13, 2015 at 16:05
  • Yes, list[0]. But nothing happens on clicking the hyperlink Commented Jan 13, 2015 at 16:08
  • Do you see your JavaScript code in page source in your browser? Commented Jan 13, 2015 at 16:13
  • @zmechanic Yes, I can Commented Jan 13, 2015 at 16:39

1 Answer 1

0

The client-side script won't have access to the server-side List. I don't know offhand what liEmailIdsForCC.ToString will return, but I don't think it's not going to be something JavaScript can use. If you want to have access to the string list, I'd convert it to a JavaScript array. Something like the following. Though you should probably escape the strings.

tblCell = new HtmlTableCell();
List<string> liEmailIdsForCC = new List<string>();
if(some condition)
{
    liEmailIdsForCC.add("someitem");
}

HyperLink hpOwnerName = new HyperLink();
hpOwnerName.Text += string.Format("(FullName)  ", "FullName");

string listJSArray = string.Format("['{0}']", string.Join("', '", liEmailIdsForCC.ToArray());

var onClick = string.Format("OnNameClick(this, {0}); return false;", listJSArray);
hpOwnerName.Attributes.Add("onclick", onClick);
tblCell.Controls.Add(hpOwnerName);

// javascript code...
function OnNameClick(source, list) {
    alert(list[0]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I need to pass'this;, as I have few more parameters contained
this, as you're using it, refers to the class, not the object that's causing it to be called. I've modified it to pass the html element that called it, which is what I think "source" usually is in ASP.NET.

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.