Page_Load behind code not working

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 年 前
I am trying to implement some javascript onto the default.aspx by putting the code into the master.cs page_load but nothing seems to work. I even replace with a simple response.write but it is not working.

Tried on default.aspx.cs but nothing seems to work.

What am i doing wrong?
14 年 前
Please post your code.
14 年 前
Hi Skiltz, here is my code in Root.Master.cs. No response. So i tried just putting in a response.write("Hello World!") into the page_load section and nothing happens. Same goes if i try it in my default.aspx.cs

Root.Master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NopSolutions.NopCommerce.Web.MasterPages.Root
{

   protected void Page_Load(object sender, EventArgs e)
  {
        //Get IP Address
        string ipaddress;
        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (ipaddress == "" || ipaddress == null)
            ipaddress = Request.ServerVariables["REMOTE_ADDR"];

        DataTable dt = GetLocation(ipaddress);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
        if (dt.Rows[0]["CountryCode"].ToString() == "SG")
        {
          Response.Redirect("http://www.xxx.com/default.aspx");
        }
        else
        {
        Response.Write("Test Else");
        }
            }
            else
            {

            }
        }

    }

  private DataTable GetLocation(string ipaddress)
    {
        //Create a WebRequest
        WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress);

        //Create a Proxy
        WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true);

        //Assign the proxy to the WebRequest
        rssReq.Proxy = px;

        //Set the timeout in Seconds for the WebRequest
        rssReq.Timeout = 2000;
        try
        {
            //Get the WebResponse
            WebResponse rep = rssReq.GetResponse();

            //Read the Response in a XMLTextReader
            XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

            //Create a new DataSet
            DataSet ds = new DataSet();

            //Read the Response into the DataSet
            ds.ReadXml(xtr);
            return ds.Tables[0];
        }
        catch
        {
            return null;
        }
    }
  
    public partial class root : BaseNopMasterPage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }
}



Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AjaxControlToolkit;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Content.NewsManagement;
using NopSolutions.NopCommerce.BusinessLogic.Content.Polls;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web
{
    public partial class Default : BaseNopPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        Response.Write("Hello World!");
        }

        protected void BindData()
        {
            bool showCategoriesOnMainPage = SettingManager.GetSettingValueBoolean("Display.ShowCategoriesOnMainPage");
            if (showCategoriesOnMainPage)
                ctrlHomePageCategories.ParentCategoryID = 0;
            else
                ctrlHomePageCategories.Visible = false;
            
            bool showBestSellersOnMainPage = SettingManager.GetSettingValueBoolean("Display.ShowBestsellersOnMainPage");
            ctrlBestSellers.Visible = showBestSellersOnMainPage;

            if (NewsManager.NewsEnabled && NewsManager.ShowNewsOnMainPage)
            {
                ctrlNewsList.NewsCount = NewsManager.MainPageNewsCount;
            }
            else
            {
                ctrlNewsList.Visible = false;
            }

            bool showPollsOnMainPage = SettingManager.GetSettingValueBoolean("Display.ShowPollsOnMainPage");
            ctrlTodaysPoll.Visible = showPollsOnMainPage;

            bool showWelcomeMessageOnMainPage = SettingManager.GetSettingValueBoolean("Display.ShowWelcomeMessageOnMainPage");
            if (!showWelcomeMessageOnMainPage)
            {
                topicHomePageText.Visible = false;
            }
        }
    }
}
14 年 前
This is my Root.Master.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NopSolutions.NopCommerce.Web.MasterPages
{
    public partial class root : BaseNopMasterPage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Response.Write("Hello");
            }
        }
    }
}


The response.write("hello") defintetly gets rendered to the screen.  Try putting a breakpoint in code and see if it gets hit.
14 年 前
I tried putting a breakpoint at the page_load and i got this error:

"The Breakpoint will not currently be hit. No executable code is currently loaded at this location."

Same error when i used your master.cs code in a newly installed nopcommerce.
14 年 前
Any help from the developer? Thanks.
14 年 前
Managed to resolve it.

Thanks Skiltz for your initial help!
14 年 前
Hi,

First thing When using Masterpages the life cycle of event is different which you can see here.

http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx

Now you are right the you will never get the breakpoint in Page_Load of blog. Just do a little trick, write this and try if it does not work cut your master page_load code before

base.OnLoad(e);


Here is the function:


protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }


declare this before master page_load function and sorry for my bad english.

Thanks
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.