Click here to Skip to main content
15,918,967 members
Home / Discussions / C#
   

C#

 
GeneralRe: access to controls of a project in another project Pin
Stefan Troschuetz27-Jun-04 2:07
Stefan Troschuetz27-Jun-04 2:07 
QuestionMail Checker 1.0 extension? Pin
razzeler26-Jun-04 18:08
razzeler26-Jun-04 18:08 
AnswerRe: Mail Checker 1.0 extension? Pin
Heath Stewart27-Jun-04 5:51
protectorHeath Stewart27-Jun-04 5:51 
Generalproxy pass https question Pin
steven shingler26-Jun-04 8:54
steven shingler26-Jun-04 8:54 
GeneralRe: proxy pass https question Pin
Heath Stewart26-Jun-04 14:36
protectorHeath Stewart26-Jun-04 14:36 
GeneralRe: proxy pass https question Pin
steven shingler29-Jun-04 6:58
steven shingler29-Jun-04 6:58 
GeneralRe: proxy pass https question Pin
Heath Stewart29-Jun-04 8:45
protectorHeath Stewart29-Jun-04 8:45 
GeneralEvent Order Processing Problem Pin
Gazy26-Jun-04 5:48
Gazy26-Jun-04 5:48 
Hi
I have a problem which I think is basically due to my lack of understanding as to the order that events are processed.

I have an .aspx page to which I want to programtically add all its controls (so that I can change the look of the application by simply modifying a couple of classes). I've based this on Peter Provost's article "ASP.Net Templates - Using Inheritance" (http://www.codeproject.com/aspnet/page_templates.asp).

The code almost works except that not all of my controls get added to my form as intended - the earlier-added ones do, the later-added ones do not. This is the kind of rendered HTML I get:

<div><label for="txt4">4</label><input name="txt4" id="txt4" type="text" /></div>
<div><label for="txt5">5</label><input name="txt5" id="txt5" type="text" /></div>

<form name="frmApp" method="post" action="company.aspx" id="frmApp">
<input type="hidden" name="__VIEWSTATE" value="dDw1MzgxOzs+kxSQNMEmk8WT5ck725Xa7SNOEg4=" />
<div><label for="txt0">0</label><input name="txt0" id="txt0" type="text" /></div>
<div><label for="txt1">1</label><input name="txt1" id="txt1" type="text" /></div>
<div><label for="txt2">2</label><input name="txt2" id="txt2" type="text" /></div>
<div><label for="txt3">3</label><input name="txt3" id="txt3" type="text" /></div>
</form>

My guess is that this is because the later-added controls have not been correctly initialised before the page is rendered.

Can anyone enlighten me?

Many thanks in advance.

Gary


==============
The .aspx file
==============

<%@ Page Inherits="CompanyPage" Src="classes/company_page.cs" %>

====================
The Code-behind file
====================

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using MyNamespace;

public class CompanyPage : MyNamespace.ExtendedPage{


//--------------------------------------------------------------------------------------------------

protected override void OnInit(System.EventArgs e){
for(int i = 0; i < 10; i++){
//add some text boxes and labels to the page
this.AddTextBox("Text" + i, "txt" + i);
}
base.OnInit(e);
}

//--------------------------------------------------------------------------------------------------

}

===========================
The Base Classes (compiled)
===========================

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace MyNamespace{

public class BasicPage : System.Web.UI.Page{

//--------------------------------------------------------------------------------------------------

protected String Footer{

get{

return("...</body></html>");
}
}

//--------------------------------------------------------------------------------------------------

protected String Header{

get{

return("<html><head>...");
}

}

//--------------------------------------------------------------------------------------------------

}

public class ExtendedPage : BasicPage{

//--------------------------------------------------------------------------------------------------

protected void AddTextBox(String strLabel, String strName){

this.Controls.Add(new LiteralControl("<div>"));
this.Controls.Add(new LiteralControl("<label for=\"" + strName + "\">" + strLabel + "</label>"));

HtmlInputText ctrl = new HtmlInputText();
ctrl.ID = strName;
this.Controls.Add(ctrl);

this.Controls.Add(new LiteralControl("</div>\n"));

}

//--------------------------------------------------------------------------------------------------

private void AddExistingControls(HtmlForm form){

//****************************************************
// Add existing controls to form and remove from class
//****************************************************

for(int i = 0; i < this.Controls.Count; i++){

//get first control in collection
System.Web.UI.Control ctrl = this.Controls[0];
//add control to form
form.Controls.Add(ctrl);
//remove control (gets added back along with form in BuildPage)
this.Controls.Remove(ctrl);

}

}

//--------------------------------------------------------------------------------------------------

protected void BuildPage(HtmlForm form){

//***************
// Build the page
//***************

//this.Controls.Clear();
//add page header
this.Controls.AddAt(0, new LiteralControl(Header));
//add form
this.Controls.Add(form);
//add page footer
this.Controls.Add(new LiteralControl(Footer));

}

//--------------------------------------------------------------------------------------------------

private HtmlForm GetForm(){

//********************************************************
// Returns HtmlForm containing controls added to sub-class
//********************************************************

HtmlForm form = new HtmlForm();
form.ID = "frmApp";
//objects passed by reference!
AddExistingControls(form);
return form;

}

//--------------------------------------------------------------------------------------------------

protected override void OnInit(System.EventArgs e){


BuildPage(GetForm());
base.OnInit(e);

}

//--------------------------------------------------------------------------------------------------

}

}
GeneralRe: Event Order Processing Problem Pin
Heath Stewart26-Jun-04 14:37
protectorHeath Stewart26-Jun-04 14:37 
Generalproblem with threads Pin
Ravikumar_mv26-Jun-04 3:47
Ravikumar_mv26-Jun-04 3:47 
GeneralRe: problem with threads Pin
Stefan Troschuetz26-Jun-04 12:27
Stefan Troschuetz26-Jun-04 12:27 
GeneralRe: problem with threads Pin
Ravikumar_mv27-Jun-04 18:19
Ravikumar_mv27-Jun-04 18:19 
GeneralASerial Communication Pin
ahmed mohamed abdelhameed26-Jun-04 0:14
ahmed mohamed abdelhameed26-Jun-04 0:14 
Generalaxwebbrowser Pin
fatidarya25-Jun-04 19:27
fatidarya25-Jun-04 19:27 
GeneralRe: axwebbrowser Pin
Heath Stewart26-Jun-04 14:25
protectorHeath Stewart26-Jun-04 14:25 
GeneralRe: axwebbrowser Pin
fatidarya26-Jun-04 20:59
fatidarya26-Jun-04 20:59 
GeneralRe: axwebbrowser Pin
Heath Stewart27-Jun-04 5:48
protectorHeath Stewart27-Jun-04 5:48 
Generalopendialog Pin
fatidarya25-Jun-04 18:56
fatidarya25-Jun-04 18:56 
GeneralRe: opendialog Pin
Stefan Troschuetz26-Jun-04 12:32
Stefan Troschuetz26-Jun-04 12:32 
GeneralRe: font of selected text of textbox Pin
leppie25-Jun-04 21:52
leppie25-Jun-04 21:52 
GeneralRe: font of selected text of textbox Pin
Anonymous25-Jun-04 22:20
Anonymous25-Jun-04 22:20 
GeneralRe: font of selected text of textbox Pin
fatidarya25-Jun-04 22:32
fatidarya25-Jun-04 22:32 
GeneralRe: font of selected text of textbox Pin
Dave Kreskowiak26-Jun-04 17:56
mveDave Kreskowiak26-Jun-04 17:56 
Generalosql.exe Pin
devvvy25-Jun-04 18:41
devvvy25-Jun-04 18:41 
GeneralRe: osql.exe Pin
Heath Stewart26-Jun-04 14:19
protectorHeath Stewart26-Jun-04 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.