USING EXTENSION METHODS FOR GENERAL FUNCTIONALITIES


1. 1st one, a replacement for RegisterStartUpScript. We can use this method in place and can be called from all pages.
2. 2nd one is additional method for "String" class.

Just try these and put your investments too.

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
    function pageLoad()
    {
        var submitButton =  $get(g_SubmitButton);
        submitButton.click();
       
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Button ID="submitButton" runat="server" Text="Button" OnClientClick="javascript:alert('You clicked me');return false;" />
    </form>
</body>
</html>

Coding
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterScriptVariable("g_SubmitButton",submitButton.ClientID,true);
            string name = "C-SHARP CORNER";
            string helloName =  name.HelloString();
               
              
        }
    }
    //Class contains Extension Methods
    public static class ExtesionMethodClass
    {
        //Extender method for ClientScriptManager
        public static void RegisterScriptVariable(this ClientScriptManager sm, string variableName, string variableValue, bool declareVariable)
        {
            string declaration = declareVariable ? "var " : string.Empty;
            sm.RegisterClientScriptBlock(typeof(Page), variableName, String.Format("{0}{1} = '{2}';\r\n", declaration, variableName, variableValue.ToString().ToLower()), true);
        }
        //Extender method for String
        public static string HelloString(this String str)
        {
            return string.Concat("Hello ", str);
        }
    }
}
Ebook Download
View all
Learn
View all