最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

calling javascript from c# - Stack Overflow

programmeradmin1浏览0评论

I need to use the javascript functions to show and hide an element on my page, but calling it from within a C# method. Is this possible?

EDIT : I tried RegisterStartupScript (see below) but this did not hide the elements as I had hoped :

HidePopup("CompanyHQSetup", "$('#<%=DivDataProvider.ClientID %>').hide();$('#<%=modalOverlay.ClientID %>').hide();");

private void HidePopup(string Key, string jscript)
    {
        string str = "";
        str += "<script language='javascript'>";
        str += jscript;
        str += "</script>";
        RegisterStartupScript(Key, jscript);
    }

EDIT : Got around this by using a hidden field boolean to determine whether or not to hide or show the elements

I need to use the javascript functions to show and hide an element on my page, but calling it from within a C# method. Is this possible?

EDIT : I tried RegisterStartupScript (see below) but this did not hide the elements as I had hoped :

HidePopup("CompanyHQSetup", "$('#<%=DivDataProvider.ClientID %>').hide();$('#<%=modalOverlay.ClientID %>').hide();");

private void HidePopup(string Key, string jscript)
    {
        string str = "";
        str += "<script language='javascript'>";
        str += jscript;
        str += "</script>";
        RegisterStartupScript(Key, jscript);
    }

EDIT : Got around this by using a hidden field boolean to determine whether or not to hide or show the elements

Share Improve this question edited May 9, 2011 at 11:10 user517406 asked May 9, 2011 at 10:27 user517406user517406 13.8k30 gold badges84 silver badges122 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Yes, check out RegisterClientScriptBlock.

Here's a snippet taken from that link:

  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client script on the page.
    String csName = "ButtonClickScript";
    Type csType = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(csType, csName))
    {
      StringBuilder csText = new StringBuilder();
      csText.Append("<script type=\"text/javascript\"> function DoClick() {");
      csText.Append("Form1.Message.value='Text from client script.'} </");
      csText.Append("script>");
      cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
    }
  }

One is server side, the other is client side. They can pass variables to each other (Javascript to ASP would be via forms/querystring/cookies and ASP to JS done via response.writing variables), but they can't directly interact.

you can use page.RegisterClientScript method to do that go on the following url http://msdn.microsoft./en-us/library/system.web.ui.page.registerclientscriptblock.aspx

and give it a try

Javascript is client side, c# is server side. You can't call javascript directly from C#. Take a look at Comet though, it will show you how you can push data from the HTTP server to the webpage.

发布评论

评论列表(0)

  1. 暂无评论