1

I got this script from a developer to put it on my website for changing the phone numbers dynamically.

    <script runat="server">
        public string getPhoneNumber()
        {
            string strPhone = "";
            if (!(Session["Phone"] == null))
            {
                strPhone = Session["Phone"].ToString();
            }
            else
            {
                searchphone.GetPhone obj = new searchphone.GetPhone();
                string strURL = HttpContext.Current.Request.Url.AbsolutePath;

                if (Request.QueryString["s"] != null && Request.QueryString["P"] != null)
                    strPhone = obj.GetPhoneNumber(Request.QueryString["s"], Request.QueryString["p"]);

                if (!string.IsNullOrEmpty(strPhone))
                    Session["Phone"] = strPhone;
                else
                    strPhone = obj.GetPhoneNumber(strURL);
            }
            return strPhone;
        }
    </script>

Logic is in a dll file and there is a namespace also which needs to be included on every page. Code is working fine.

But my problem is he has asked me to put this script on every page manually. Also its not working when I put the script and import the namespace on the master page and want to use the getphonenumber method(<% =getPhoneNumber() %>) on the content pages to track and change the phone number based on user sessions.

The objective for this code is to track the users and display the phone number based on that. And i want to include this script once globally so every page can access it and use its getphonenumber method. Thanks.

4
  • 2
    if you want it in all the pages place it master page. Commented Jan 15, 2014 at 9:04
  • @AntoJSubash read his question, he has tried it Commented Jan 15, 2014 at 9:04
  • this is C# code, why dont you move it to an assembly and reference that assembly? Commented Jan 15, 2014 at 9:05
  • 1
    Why don't you use a public class and make this method public static? Then you can access it from everywhere. You don't need an instance of the page anyway. You can get all objects like Session or Request via HttpContext.Current. Commented Jan 15, 2014 at 9:07

1 Answer 1

1

One way to do this is to place this function on the masterpage. Then access the function on your page.

Masterpage:

public string getPhoneNumber()
{
    string strPhone = "";
    if (!(Session["Phone"] == null))
    {
        strPhone = Session["Phone"].ToString();
    }
    else
    {
        searchphone.GetPhone obj = new searchphone.GetPhone();
        string strURL = HttpContext.Current.Request.Url.AbsolutePath;

        if (Request.QueryString["s"] != null && Request.QueryString["P"] != null)
            strPhone = obj.GetPhoneNumber(Request.QueryString["s"], Request.QueryString["p"]);

        if (!string.IsNullOrEmpty(strPhone))
            Session["Phone"] = strPhone;
        else
            strPhone = obj.GetPhoneNumber(strURL);
    }
    return strPhone;
}

Page

<%= ((MasterPageType)this.Master).getPhoneNumber() %>

Where MasterPageType is the type name of your master page.


Another way to achieve this is by creating a static class (new file):

Static class

public static class SessionTools
{
    public static string getPhoneNumber()
    {
        string strPhone = "";
        if (!(HttpContext.Current.Session["Phone"] == null))
        {
            strPhone = HttpContext.Current.Session["Phone"].ToString();
        }
        else
        {
            searchphone.GetPhone obj = new searchphone.GetPhone();
            string strURL = HttpContext.Current.Request.Url.AbsolutePath;

            if (HttpContext.Current.Request.QueryString["s"] != null && HttpContext.Current.Request.QueryString["P"] != null)
                strPhone = obj.GetPhoneNumber(HttpContext.Current.Request.QueryString["s"], HttpContext.Current.Request.QueryString["p"]);

            if (!string.IsNullOrEmpty(strPhone))
                HttpContext.Current.Session["Phone"] = strPhone;
            else
                strPhone = obj.GetPhoneNumber(strURL);
        }
        return strPhone;
    }
}

Page

<%= SessionTools.getPhoneNumber() %>
Sign up to request clarification or add additional context in comments.

13 Comments

hi, thanks for your reply. But i am not an expert in asp.net. Could you please elaborate the masterpagetype part?
What's the name of your master page? Do you have a code behind (.cs) file for you masterpage?
Its LandingPage.master. Yeah i have a code behind .cs file also.
Then you should use <%= ((LandingPage)this.Master).getPhoneNumber() %>
Its showing a red squiggly line under LandingPage. On hovering its shows this error "The type or namespace name 'LandingPage' could not be found(are you missing a using directive or an assembly reference?)"
|

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.