1

I want to ask How to call javascript from C# page load. My Javascript is

function Hide(lst) {
        if (document.getElementById) {
            var tabList = document.getElementById(lst).style;
            tabList.display = "none";

            return false;
        } else {
            return true;
        }
    }

and want to call from pageload

if (dtSuperUser(sLogonID).Rows.Count < 1)
        {

            //Call Javascript with parameter name tablist
        }

thanks

4 Answers 4

4

Actually, you can use pageOnload event to do so. Like this.

 protected void Page_Load(object sender, EventArgs e)
        {           
            if (IsPostBack)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>document.getElementById('Your element').style.display = 'block'</script>");
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>document.getElementById('Your element').style.display = 'hidden'</script>");
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

thanks... but I used Ajax Toolkit, so need to use ScriptManager.RegisterClientScriptBlock?
when I use the code which you provided, that is not hide, it show code on UI page when I call the page. thanks
When page first open the IsPostBack==false; So you should use 'Hidden' in the else. I just show an example. Sorry for that.
this.ClientScript.RegisterStartupScript(this.GetType(), "urScriptName", "alert(1);");
1
String csName = "myScript";
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))
{
  cs.RegisterClientScriptBlock(csType, csName, 
      string.Format("Hide({0})", lst.ClientID));
}

1 Comment

I got this message, "Cannot implicity convert type 'System.Type'" to '_______.Type'
1
RegisterStartupScript("Hide", string.Format(@"if (document.getElementById) {
        var tabList = document.getElementById('{0}').style;
        tabList.display = 'none';
        return false;
    } else {
        return true;
    }",lst));

Or if you already have the Javascript function rendered in the Markup

RegisterStartupScript("Hide",string.Format("Hide('{0}');",lst));

Comments

0

Are you using webforms or MVC? If using webforms check:

http://msdn.microsoft.com/en-us/library/Aa479011

Page.RegisterStartupScript("MyScript",
   "<script language=javascript>" +
   "function AlertHello() { alert('Hello ASP.NET'); }</script>");

Button1.Attributes["onclick"] = "AlertHello()";
Button2.Attributes["onclick"] = "AlertHello()";

1 Comment

I dun want to use button, bro. I check permission and want to disable html tab

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.