Inheritance still wrong please figure out for me!!!

  • Thread starter Thread starter Hai Nguyen
  • Start date Start date
H

Hai Nguyen

I would like to know I have two pages: consider page1, page2. Also, I have
code C# behind these two asp.net webforms. I code some methods in page1 and
I want page2 inherit what page1 has.
I know something has to deal with reference and inherit namespace and such.

Thanks

Page 1:

private void Page_Load(object sender, System.EventArgs e)
{

if(!Page.IsPostBack)

{

step1.Font.Bold = true; ->sth with this

StateList();

string clientIP1 = Request.UserHostAddress;


string strServername = Request.ServerVariables["SERVER_NAME"];

string strServerIP = Request.ServerVariables["LOCAL_ADDR"];

string strRemoteIP = Request.ServerVariables["REMOTE_ADDR"];

string[] str1 = Request.UserLanguages;

Response.Write(clientIP1+ "<br>");

Response.Write(strServername +"<br>");

Response.Write(strServerIP+"<br>");

Response.Write(strRemoteIP+"<br>");

for (int count = 0; count < str1.Length; count++)

{

Response.Write("User Language " + count +": " + str1[count] + "<br>");

}



}

}

IF I used this: Page 2: System.Web.UI.Page it's correct but i can not access any methods used in Page1

but if i use this Page 2: Page 1 there is an error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 272: //Update the statebox used ArrayList above
Line 273: //statebox = new DropDownList();
Line 274: statebox.DataSource = StateList;
Line 275: statebox.DataBind();
Line 276: }
 
Hi Hai,

This is what I would do, I haven;t test it but I think it will work.
I would create a class ( Project/ Add Class ) that will contain the common code, this class inherit from Page:

public class CommonClass : System.Web.UI.Page


Then you create both page as usual and in the code behind and change the parent class from the default Page to the class that you created.

Now the code that you have will need to change too. you will need to create in the CommonClass a method where you init the common controls. and you will have to call that method in the Page_Load () method of both your forms.
Also you need to define the controls in both aspx pages.


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
I would like to know I have two pages: consider page1, page2. Also, I have
code C# behind these two asp.net webforms. I code some methods in page1 and
I want page2 inherit what page1 has.
I know something has to deal with reference and inherit namespace and such.

Thanks

Page 1:

private void Page_Load(object sender, System.EventArgs e)
{

if(!Page.IsPostBack)

{

step1.Font.Bold = true; ->sth with this

StateList();

string clientIP1 = Request.UserHostAddress;


string strServername = Request.ServerVariables["SERVER_NAME"];

string strServerIP = Request.ServerVariables["LOCAL_ADDR"];

string strRemoteIP = Request.ServerVariables["REMOTE_ADDR"];

string[] str1 = Request.UserLanguages;

Response.Write(clientIP1+ "<br>");

Response.Write(strServername +"<br>");

Response.Write(strServerIP+"<br>");

Response.Write(strRemoteIP+"<br>");

for (int count = 0; count < str1.Length; count++)

{

Response.Write("User Language " + count +": " + str1[count] + "<br>");

}



}

}

IF I used this: Page 2: System.Web.UI.Page it's correct but i can not access any methods used in Page1

but if i use this Page 2: Page 1 there is an error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 272: //Update the statebox used ArrayList above
Line 273: //statebox = new DropDownList();
Line 274: statebox.DataSource = StateList;
Line 275: statebox.DataBind();
Line 276: }
 
Back
Top