Reputation: 13
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.
Upvotes: 1
Views: 100
Reputation: 4183
One way to do this is to place this function on the masterpage. Then access the function on your page.
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;
}
<%= ((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):
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;
}
}
<%= SessionTools.getPhoneNumber() %>
Upvotes: 1