 |
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

October 9th, 2003, 09:53 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
HtmlForm submission?! Event?
Hi there...
I do not know how to handle button on HtmlForms the best way, hope you know! I have made a class which inherits the HtmlForm class. In this class I have put some funcionality which adds some input controls and two buttons dynamically (run-time, not user controls, actually from database specification of some UI).
Each of the buttons will submit the form, however only one should. I have found that I am able to manually figure out which button is clicked in my Submit method, by looking at the values in the Request.Form collection, and then handle the different buttons.
I do not think this is the best solution. Isn't there a way to append an eventhandler to one of the buttons, namely the cancel button, so that the Submit method is not executed? I call the Submit method when I have checked for postback.
So, is it possible to make some button disregard the submission, and implement a method for this button individually?! And how!? Using Button.Command, Button.CommandName etc. ?
Thanks in advance!
Jacob.
|

October 9th, 2003, 10:22 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You should set up a handler for each button separately. Should look something like this:
Public Sub cmdSubmitButton_onClick(object as Sender, e as System.EventArgs)
and
Public Sub cmdCancelButton_onClick(object as Sender, e as System.EventArgs)
Then, when you add your buttons, you can do this:
AddHandler cmdSubmitButton.Click, AddressOf cmdSubmitButton_onClick
AddHandler cmdCancelButton.Click, AddressOf cmdCancelButton_onClick
The syntax for C# if a bit different for the handlers. I think it's something like this:
cmdSubmitButton.Click += new EventHandler(cmdSubmitButton_onClick);
(and of course you'll need to change the function code to C#)
Peter
|

October 9th, 2003, 10:29 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the correction on the other thread
When I set it up for each button individually, then I can disregaurd the submission where I test for postback, right!? I did something like this...
Code:
if(!this.Page.IsPostBack)
{
userInterface.CreateUI(this.uiid);
...
}
else userInterface.Submit();
The button actions are only handled by the handlers which is added?
Jacob.
|

October 9th, 2003, 10:55 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
(You didn't need correction, your answer was correct, i was just trying to add to it.)
Yes.. You create a specific handler for each button. Seeing as all you were doing in the form's submit event was checking to see what button was pressed, you can get rid of it. The handler for the right button will fire off when after .Net determines which you clicked. Makes it pretty simple.
How are you dealing with this HtmlForm? How is it interacting with the page and the HtmlForm that's already on the ASPX? Just curious.
Peter
|

October 9th, 2003, 11:18 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Perhaps I have made it a bit to complicated!
I have made a class which inherits a UserControl (OCMSPage), and this general class is inherited by a series of child classes where one of these is called PageUI. This PageUI class is able to generate all the different UIs I have got on my site, and these are defined in a database so the only thing I have to pass is some ID in order to get the UI build.
Then I have implemented a UI class (OCMSui) which is inherited for each UI, and this holds an abstract method, namely Submit, which should then be overriden each time I make a new UI. All this enabled me to have an initialization method in PageUI like this...
Code:
internal override void Initialize()
{
if((userInterface = LoadUI()) != null)
{
userInterface.Page = this.Page;
if(!this.Page.IsPostBack)
{
userInterface.CreateUI(this.uiid);
database = new OCMSdatabase(OCMSDatabaseSource.UI);
database.GetValuesSP(ref reader, "UserInterfaceInfo " + Convert.ToString(this.uiid), uiData);
this.PageDescription = (uiData[1] != DBNull.Value)? uiData[1].ToString(): "[error]";
}
else userInterface.Submit();
}
else
{
Page.Session.Abandon();
Page.Response.Redirect("main.aspx");
}
}
... where LoadUI is some switch that instantiate the right UI, and userInterface is the OCMSui object.
So when I try to implement the EventHandlers as you have described I get confused as to where to put them!? I instantiate the two buttons in the OCMSui object, so this should be where they (methods) are declared, right?! But it does not seem to work.
I have tried this in OCMSui...
Code:
void cancel_Click(Object sender, EventArgs e)
{
throw new Exception("in cancel");
}
... and then defined the button like this...
Code:
Button cancel = new Button();
cancel.Click += new EventHandler(this.cancel_Click);
cancel.CommandName = "Cancel";
It compiles, but it seems like the event method (cancel_Click) is never called!?
Thanks :)
Jacob.
|

October 9th, 2003, 11:20 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So, what I am doing now is all back to basics. Trying to make a single-file-example in order to get a better understanding of the concepts of the events using the info I got from you.
I got this to work...
Code:
<script runat="server">
void submit_Command(Object sender, CommandEventArgs e)
{
Response.Write("in submit");
}
void cancel_Command(Object sender, CommandEventArgs e)
{
Response.Write("in cancel");
}
public void Page_Load(Object oSender, EventArgs e)
{
HtmlForm form = new HtmlForm();
Button but01 = new Button();
but01.Text = "cancel";
but01.Command += new CommandEventHandler(this.cancel_Command);
but01.CommandName = "Cancel";
Button but02 = new Button();
but02.Text = "submit";
but02.Command += new CommandEventHandler(this.submit_Command);
but02.CommandName = "Submit";
form.Controls.Add(but01);
form.Controls.Add(but02);
Page.Controls.Add(form);
}
</script>
It is hard if you start out with a big scale project  .
Jacob.
|

October 9th, 2003, 11:37 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Yes indeed it is. There is certain merit to the "Hello World" examples! ;)
Where are you adding the buttons and setting up their event handlers? Also, I'm not sure but I don't think you need the cancel.CommandName = "Cancel"; part.
You've added quite a layer of complexity by having all this inheritance, but it shouldn't mess things up (i don't think).
How is this HtmlForm class you speak of at the beginning dealt with? I guess I'm still not seeing how that is hooked up to the page. Can you make a little tree of what you have? This is what I understand thus far...
OCMSPage : UserControl
-> PageUI
-> OCMSui
Peter
|

October 9th, 2003, 11:52 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have tried to make an informative tree and expanded it with some method definitions where interesting (dots just mean 'more')...
Code:
public abstract class OCMSPage : UserControl
internal abstract void Initialize();
protected abstract void MakeBody();
public class PageMain : OCMSPage
...
public class PageError : OCMSPage
...
public class PageUI : OCMSPage
private OCMSui LoadUI()
{
OCMSui ui;
switch(this.uiid)
{
case 1:
ui = new OCMSloginUI();
break;
...
}
return ui;
}
internal override void Initialize()
{
if((userInterface = LoadUI()) != null)
{
userInterface.Page = this.Page;
if(!this.Page.IsPostBack)
{
userInterface.CreateUI(this.uiid);
database = new OCMSdatabase(OCMSDatabaseSource.UI);
database.GetValuesSP(ref reader, "UserInterfaceInfo " + Convert.ToString(this.uiid), uiData);
this.PageDescription = (uiData[1] != DBNull.Value)? uiData[1].ToString(): "[error]";
}
else userInterface.Submit();
}
else
{
Page.Session.Abandon();
Page.Response.Redirect("main.aspx");
}
}
protected override void MakeBody()
internal abstract class OCMSui : HtmlForm
internal virtual void CreateUI(int uiid)
internal abstract void Submit();
internal class OCMSloginUI : OCMSui
internal override void Submit()
internal class OCMSemailPageResponsibleUI : OCMSui
internal override void Submit()
internal class OCMSemailUserUI : OCMSui
internal override void Submit()
internal class OCMSaddUserUI : OCMSui
internal override void Submit()
...
|

October 9th, 2003, 11:54 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
And the shorter version...
Code:
public abstract class OCMSPage : UserControl
internal abstract void Initialize();
protected abstract void MakeBody();
public class PageMain : OCMSPage
public class PageError : OCMSPage
public class PageUI : OCMSPage
private OCMSui LoadUI()
internal override void Initialize()
protected override void MakeBody()
internal abstract class OCMSui : HtmlForm
internal virtual void CreateUI(int uiid)
internal abstract void Submit();
internal class OCMSloginUI : OCMSui
internal override void Submit()
internal class OCMSemailPageResponsibleUI : OCMSui
internal override void Submit()
internal class OCMSemailUserUI : OCMSui
internal override void Submit()
internal class OCMSaddUserUI : OCMSui
internal override void Submit()
...
Thanks for your time!
Jacob.
|

October 9th, 2003, 12:26 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Ok. I have a better understanding of your structure now, but I'm still a little boggled... You have this OCMSui class that is derived from HtmlForm. But how do you use that? I'm just not seeing it. I mean let's talk basics: you have an ASPX page, on it is an HtmlForm. You have a class that comes from HtmlForm... are they related? I'm trying to understand how the controls (buttons) you are putting in your HtmlForm are used.
Ok, I just read the post you edited. hadn't seen that before, just saw the email version that didn't have the code in it. Why are you creating an HtmlForm object then adding it to the page? I'm thinking this is what the problem is all about. If you do this, you are messing up the whole ASP.Net webform structure.
Peter
|
|
 |