简体   繁体   中英

Call Javascript method from code behind c#

Please help me solve this challenge:( I have below code:

Label lblVideoAssessment = new Label();
                        lblVideoAssessment.Text = "<a href='../SitePages/Assessment.aspx?cat=" + cat + "' height='300px' width='300px' Target='_blank' cssClass='IconDisplayCss'><img src='~/_layouts/images/Assessment.png' border='none'/></a><br/>" + cat;

I want to replace above code by a dialogue box instead of opening a new page "Ässessment.aspx". I have tried like this:

lblVideoAssessment.Text = "<a cssclass='IconDisplayCss' width='300px' height='300px' onclick='javascript:OpenDialog('../SitePages/Assessment.aspx?cat='+cat+);return false;'><img border='none' src='~/_layouts/images/Assessment.png'></a>";

However it doesnot work. I think the issue is with the '' (Quotation which i have placed ) or do i need to use Page.ClientScript.RegisterStartupScript

if yes...how and where do i place it?

Please help as i have been struggling for fort night.

I had also got suggestion to use Literal Control.I have tried that as well and it does not work.

var strScript = "<script language=\"JavaScript\" type=\"text/javascript\">";
strScript += "alert(\"hello\")";
strScript += "</script>";
Page.ClientScript.RegisterClientScriptBlock(GetType(), "onload", strScript, false);

Good Luck!

Edit:

var script = "<script language=\"JavaScript\" type=\"text/javascript\">$('#tag').click(function () {})</script>"; Page.ClientScript.RegisterClientScriptBlock(GetType(), "ready", script , false);

Finally:

var script = "<script language=\"JavaScript\" type=\"text/javascript\">$(\".IconDisplayCss\").click(function () {OpenDialog('../SitePages/Assessment.aspx?cat='" + cat + "); });</script>" Page.ClientScript.RegisterClientScriptBlock(GetType(), "ready", script , false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM