|
Is it possible to inherit a form layout from another page?
At the moment I can only work out how to inherit the code behind, not the visual layout.
Thanks
Stephen
|
|
|
|
|
I am not sure I understand exactly what you are trying to do, but it seems that user controls is what you might be looking for:
User controls in ASP .NET by Chris Maunder.
|
|
|
|
|
In Windows Forms, you can inheret from another form, and you get all the controls that where defined on the base form showing up in your current form.
Just wondering if I could do the same in ASP.NET.
For Example I define a ASPX page that has my footer and header on it. I then inherit from that page in all the other pages I write, and they all get the same header and footer.
Just a simple example and I do not belive that you can do it, just seemed a nice idea after doing the same sort of thing with windows forms.
Stephen
|
|
|
|
|
You are probably right - I did not see anything that will allow you to inherit a form. I guess, we are back to good ol' include files...
|
|
|
|
|
I have a code-behind file which creates a table dynamically based on an xml file. The format of the xml is like
<sample>
<uname>some1</uname>
<uid>1234</uid>
</sample>
This sample node repeats under a <samples> parent node.
The no. of rows in the table is equal to the no. of <sample> nodes. I am successful upto creating the table. But along with each row of data, I need to add a CheckBox control in the first column of the table. I am able to do this too. But I am not able to control the checkboxes. For example, I need a top header checkbox, which when checked, will check all the other check boxes. How do I handle the event in the code behind page. How do I get control to the individual check boxes? All I do to create the checkboxes is...
TableCell tc = new TableCell();
tc.Controls.Add(new CheckBox());
Help me!
omkamal
|
|
|
|
|
To handle events of dynamically added controls you need to create a function that will handle event like this:
private void myCtrl_Click(Object Source, EventArgs e)
{
...
}
then you need to add your control as follow:
TableCell tc = new TableCell();
CheckBox chkBox = new CheckBox();
chkBox.ID = "chkBox";
chkBox.CheckedChanged += new System.EventHandler(this.myCtrl_Click);
tc.Controls.Add(chkBox);
Events will one fire if your control is added inside of your form. If it will happen to be outside of the form object event will not fire. That means that your table cell should be added to your form not just to the page. In order to add controls to the form you need to give your form an ID
<form id="frmMain" ... >
then you need to add a field to your class that corresponds to the form:
protected HtmlForm frmMain;
Now when you are adding your table to the page you need to add it to the form instead:
frmMain.Controls.Add(myTable);
To refer to your chkBox you can do this anywhere in your code:
CheckBox chkBox = (CheckBox)FindControl("chkBox");
if( chkBox != null )
{
chkBox.Checked = true;
}
I hope this helps.
|
|
|
|
|
This is what I exactly did. I even made it simpler. My form tag and table tag are hardcoded in my .aspx file. Meaning there is no dynamic change that I wanted to do to my form. All the form has is a table and the rows of the table are modified by the xml file.
The problem lies only in the checkbox. When I render the aspx page, it displays all checkboxes correctly and there is a control checkbox which when checked should check all the others.
As you said, I wrote a SelectAll function and added it as a CheckedChanged event to the control checkbox.
But the function is not getting called. Why? When I run the page, and click the control checkbox, it refreshs the page (meaning it posts back) and unchecks the box automatically and no SelectAll function is executed.
What am I missing? I even tried creating the HtmlForm object and adding the controls. But didnt work.
omkamal
|
|
|
|
|
Well, it is hard to say what is wrong without seeing any of your code, but one thing that I had to do is set AutoPostBack property of a CheckBox to true (default is false). I know you said that it posts back, but I cannot think of anything else at the moment...
|
|
|
|
|
my .aspx snippet
<form runat="server">
<asp:Button CssClass="button" id="KillProc" Text="Kill Selected" onclick="writeKillProcDetail" runat="server"/>
<asp:Table id="ProcessDataTable" CssClass="asptable" CellSpacing="0" GridLines="Both" HorizontalAlign="center" runat="server"/>
<asp:Button CssClass="button" id="KillProc1" Text="Kill Selected" onclick="writeKillProcDetail" runat="server"/>
</form>
my .cs snippet...
Dynamic Controls declared for use in the Form
public Label UserStatus;
public Table ProcessDataTable;
public CheckBox SelectAllCheckBox;
public CheckBox[] ProcessChecks;
public int TotalProcess;
public string UName;
public string Pwd;
public string MachineName;
The following code resides in Page_Load function
protected void Page_Load(Object src, EventArgs e)
{
XmlDocument procXmlDoc = new XmlDocument();
procXmlDoc.Load(dataUrl);
long memValue = 0;
long memValueK = 0;
XmlNodeList procList = procXmlDoc.GetElementsByTagName("process");
XmlNodeList pNameList = procXmlDoc.GetElementsByTagName("pname");
XmlNodeList pIdList = procXmlDoc.GetElementsByTagName("pid");
XmlNodeList pWSetList = procXmlDoc.GetElementsByTagName("pwset");
TotalProcess = 10; //procList.Count;
//Write Header Row
SelectAllCheckBox = new CheckBox();
SelectAllCheckBox.ID = "checkAll";
SelectAllCheckBox.AutoPostBack = true;
SelectAllCheckBox.CheckedChanged += new EventHandler(SelectAll);
ProcessChecks = new CheckBox[TotalProcess];
TableRow trHead = new TableRow();
TableCell tcCheck = new TableCell();
tcCheck.Controls.Add(SelectAllCheckBox);
trHead.Cells.Add(tcCheck);
TableCell tcName = new TableCell();
tcName.Controls.Add(new LiteralControl("Process Name"));
tcName.HorizontalAlign = HorizontalAlign.Center;
trHead.Cells.Add(tcName);
TableCell tcId = new TableCell();
tcId.Controls.Add(new LiteralControl("Process Id"));
tcId.HorizontalAlign = HorizontalAlign.Center;
trHead.Cells.Add(tcId);
TableCell tcMemory = new TableCell();
tcMemory.Controls.Add(new LiteralControl("Usage"));
tcMemory.HorizontalAlign = HorizontalAlign.Center;
trHead.Cells.Add(tcMemory);
ProcessDataTable.Rows.Add(trHead);
for(int i=0; i<TotalProcess; i++)
{
TableRow tr = new TableRow();
ProcessChecks[i] = new CheckBox();
ProcessChecks[i].ID = "c" + i.ToString();
for(int j=0; j<4; j++)
{
TableCell tc = new TableCell();
switch(j)
{
case 0:
tc.Controls.Add(ProcessChecks[i]); //Checkbox for each row
break;
case 1:
tc.Controls.Add(new LiteralControl(pNameList[i].InnerText));
break;
case 2:
tc.Controls.Add(new LiteralControl(pIdList[i].InnerText));
tc.HorizontalAlign = HorizontalAlign.Right;
break;
case 3:
memValue = XmlConvert.ToInt32(pWSetList[i].InnerText);
memValueK = memValue / 1024;
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NumberDecimalDigits = 0;
tc.Controls.Add(new LiteralControl(memValueK.ToString("n", nfi) + "K"));
tc.HorizontalAlign = HorizontalAlign.Right;
break;
default:
break;
}
tr.Cells.Add(tc);
}
if( i % 2 == 0 )
{
tr.CssClass = "rowN";
}
else
{
tr.CssClass = "rowN1";
}
ProcessDataTable.Rows.Add(tr);
}
}
protected void SelectAll(Object src, EventArgs e)
{
for(int i=0; i<TotalProcess; i++)
{
ProcessChecks[i].Checked = SelectAllCheckBox.Checked;
}
//UserStatus.Text = "my test";
}
omkamal
|
|
|
|
|
I've looked and looked and I cannot see anything wrong with your code. I've tried to recreate something here that is very similar to what you have and it works every time...
I guess, you need to start commenting code out in your Page_Load leaving only lines that are related to SelectAllCheckBox until you find what it is that is causing the problem...
|
|
|
|
|
Guess what... it worked when I created the table dynamically in my .cs file. I removed the <asp:Table> tag and created a table and attached it to the form. And Voila!!!!
Does your recreation of the problem has the table added to the form dynamically? Or u use the <asp:Table> tag?? Please let me know.
I am trying to conclude the following out of this...
"When you create child controls dynamically and if you want to handle their events, then the parent control should also be created dynamically". Sounds kinda odd though. But it works for me!!!
If anyone finds the above statement is wrong, or should this be of a different interpretation, please reply to this message.
Thanks guys!
<!--I started loving codeproject.com-->
omkamal
|
|
|
|
|
omkamal wrote:
Does your recreation of the problem has the table added to the form dynamically? Or u use the tag??
I've tried it both ways and in ether case it worked... Go figure...
It had to be something else that was wrong, but we've missed it...
omkamal wrote:
"When you create child controls dynamically and if you want to handle their events, then the parent control should also be created dynamically". Sounds kinda odd though. But it works for me!!!
I cannot prove your case here since it works for me in ether way...
Do you have AutoEventWireup set to false on your page? Mine was always false and maybe this is where the difference is?
omkamal wrote:
<!--I started loving codeproject.com-->
Did you hate it before?
|
|
|
|
|
Konstantin Vasserman wrote:
omkamal wrote:
Did you hate it before?
No I did not. But I am novice to codeproject. Can I say I was doing a trial version... but no longer. I am not much involved in discussion groups previously. I used to buy books and go thru 'em. For .NET books arent helping me much. But codeproject does a lot....
omkamal
|
|
|
|
|
I see.
I am not much of a discussion groups person myself, but there is a very interesting group of people got together here on CP (lounge especially), so I am hanging around lately. I wish there was more people helping here in ASP forum, but most of the visitors of the site are C++ people, so there is not much interest in ASP yet.
|
|
|
|
|
Well, I have immense hope in .NET and Web services. I think it will start a great revolution in web community. I would want to be a pioneer when people start thinking like that. And if they start thinking like that, CP would be one of the best places for developers to get help from.
omkamal
|
|
|
|
|
omkamal wrote:
Well, I have immense hope in .NET and Web services. I think it will start a great revolution in web community.
I doubt it will be anything that can be classified as revolution. But I am sure that more and more of .NET will be around and people who knows anything about it will be needed. Beside that it is a cool thing to learn. So we shall see what will come out of it...
|
|
|
|
|
Hi,
I've just getting into ASP.NET and am wondering what is the best practice on passing variables between pages. I have done a lot of programming in classic ASP and used querystrings a lot to pass information between 2 pages.
Given that one of ASP.NET's advantages is strongly typed variables it seems a bit nasty to have to resort to using querystrings and then converting values into which ever type is expected e.g.:
string strMode = Request.QueryString["mode"];
if( strMode == "add")
{
if(Request.QueryString["myid"] != null)
{
short nMyID = Convert.ToInt16(Request.QueryString["myid"]);
DetailsFromAddPage(nMyID);
}
}
None of the books I have seen really mention this so i'm thinking that i've missed something!
If someone could give me a pointer in the right direction I would be grateful
Thanks.
|
|
|
|
|
I don't think there is another way short of storing values in Session variables, database or text file.
Now if you can use Server.Transfer() instead of Response.Redirect() you won't even need to pass anything because the page you call will be executed in the same process and I believe that all the variables from the first page will be accessible in the second page.
|
|
|
|
|
I need to attach different (self-created) user controls to my web form at run-time, and I want to specify their position when/after they are loaded.
The only way I have managed to do this so far, is by drawing a control container (like the Placeholder control) at designtime, but I need to do it all at runtime.
Does anyone know how to do this?
Thanks!
Eivind
|
|
|
|
|
Try something like this:
Label lblNew = new Label();
lblNew.Text = "This is a new label";
lblNew.Style["position"] = "absolute";
lblNew.Style["top"] = Convert.ToString(150);
lblNew.Style["left"] = Convert.ToString(200);
Controls.Add(lblNew);
|
|
|
|
|
Thanks, but this metod has a minor drawback:
It doesn't work for controls that needs to be inside a Form tag (i.e. the TextBox Control).
The reason for this, I guess, is that .NET output the control after the tag in the web-page, which I believe is not ideal.
I guess that I somehow should try to get a reference to the Page's Form, but I don't know how... (yes, I'm a newbie!)
Eivind
|
|
|
|
|
Well, if your form has an id you should be able to refer to it using that id. For example:
...
<form id="frmMain" ....>
...
frmMain.Controls.Add(myLabel);
...
Another solution might be is to just place all the controls in the form from the very beginning, but hide the ones you don't need and show them when necessary.
|
|
|
|
|
Thank you Konstantin, this was what I needed to get things right!
By using the FindControl() method, I got a reference to the Form. Here's the code for anyone interested.
Control myUserControl;
myUserControl = Page.LoadControl("Login.ascx"); //my custom user control
Control activeForm;
activeForm = Page.FindControl("myFormId"); //the id of my - element
activeForm.Controls.Add(myUserControl); //add the user control to the form
|
|
|
|
|
Hi how can i validate some form fields without using asp:RequiredFieldValidator or asp:RegularExpressionValidator?
I have this:
asp label tag id="lNome" text="Name:" runat="server" /> | asp textbox tag id="tNome" size="30" runat="SERVER" /> | | |
and the function:
Sub Validate()
if tNome.value = "" then '??? doesnt work!!!
msgbox ("Please Insert a string")
else
msgbox ("OK") ' How Can I submit the form here??
end if
End Sub
Can someonelse help me please???
Thanx in advance
bRunO
|
|
|
|
|