Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Button event not firing

Reply
Thread Tools

Button event not firing

 
 
Forumtroll
Guest
Posts: n/a
 
      06-02-2008
I have a Web User Control that parses an XML file and renders a Form
based on the XML.
The problem is that I create a button on the bottom of the form that
will fire off a subscribeable event, but somehow the button never seem
to fire that particular event.

The code is attached below.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
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 System.Xml;

public partial class uc_XmlGui : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.FormSubmitted += new EventHandler(XmlGui_FormSubmitted);
}

void XmlGui_FormSubmitted(object sender, EventArgs e)
{
}

private XmlDocument m_xmlguidatasource;
private string m_targetform;
private string tableStyle;
private string headercolumnstyle;
private string datacolumnstyle;
private string tablefooterstyle;
private string tableheaderstyle;

/// <summary>
/// Event that fires when the generated form is submitted.
/// </summary>
public event EventHandler FormSubmitted;

/// <summary>
/// Datasource for GUI.s
/// </summary>
public XmlDocument GuiDataSource
{
get
{
return this.m_xmlguidatasource;
}
set
{
this.m_xmlguidatasource = value;
}
}

/// <summary>
/// Name of target form in datasource
/// </summary>
public string TargetForm
{
get
{
return this.m_targetform;
}
set
{
this.m_targetform = value;
}
}

/// <summary>
/// binds he datasource to the GUI
/// </summary>
public override void DataBind()
{
XmlElement e = m_xmlguidatasource.DocumentElement;
XmlNamespaceManager xnm = new
XmlNamespaceManager(m_xmlguidatasource.NameTable);
xnm.AddNamespace("a", "http://m2c.no/GuiSchematics.xsd");
// finding the proper Form definition
XmlNodeList nl = e.SelectNodes("/*/a:Form", xnm);
foreach (XmlNode _form in nl)
{
XmlElement form = _form as XmlElement;
if (form.GetAttribute("Name").Equals(m_targetform))
{
// We've got the right form!
// Now getting the styles
XmlNodeList styles = form.SelectNodes("a:Styles/*",
xnm);
foreach (XmlNode style in styles)
{
switch (style.Name)
{
case "TableStyle":
this.tableStyle = style.InnerText;
break;
case "TableHeaderStyle":
this.tableheaderstyle = style.InnerText;
break;
case "HeaderColumnStyle":
this.headercolumnstyle = style.InnerText;
break;
case "DataColumnStyle":
this.datacolumnstyle = style.InnerText;
break;
case "TableFooterStyle":
this.tablefooterstyle = style.InnerText;
break;
}
}
// done fetching the styles. now styling the table
style(this.tblDynamicTable, this.tableStyle);
// now fetching the rows
XmlNodeList rows = form.SelectNodes("a:Rows/*",
xnm);
foreach (XmlNode _row in rows)
{
XmlElement row = _row as XmlElement;
bool req =
bool.Parse(row.GetAttribute("Required"));
if (bool.Parse(row.GetAttribute("Visible")))
{
// Display the row element only if defined as
visible
// and fetch all columns
TableRow trow = new TableRow();
XmlNodeList columns =
row.SelectNodes("a:Column", xnm);
// Make sure that the RFV and regex validator
// are placed in their own row if a multiline
// is rendered.
bool multiline = false;
foreach (XmlNode _col in columns)
{
XmlElement col = _col as XmlElement;
TableCell cell = new TableCell();
Dictionary<string, object> controls = new
Dictionary<string, object>();
controls.Add("label", new Label());
controls.Add("checkbox", new CheckBox());
TextBox __tb = new TextBox();
__tb.TextMode = TextBoxMode.MultiLine;
controls.Add("multiline", __tb);
controls.Add("textbox", new TextBox());
Button b = new Button();
b.Click += new EventHandler(b_Click);
controls.Add("button", b);
if
(bool.Parse(col.GetAttribute("IsHeaderColumn")))
style(cell, this.headercolumnstyle);
else
style(cell, this.datacolumnstyle);
// Set the colspan property if needed
if
(col.GetAttribute("ColumnSpan").Equals(string.Empt y) == false)
cell.ColumnSpan =
int.Parse(col.GetAttribute("ColumnSpan"));
// configuring the column and adding to
cell.Controls
collection
switch
(col.SelectNodes("a:Column.ControlType", xnm)[0].InnerText.ToLower())
{
case "label":

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as Label).Text =
col.SelectNodes("a:Column.Content", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as Label).ID =
col.SelectNodes("a:Column.ControlName", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as Label).ToolTip =
col.SelectNodes("a:Column.ToolTip", xnm)[0].InnerText;

cell.Controls.Add((controls[col.SelectNodes("a:Column.ControlType",
xnm)[0].InnerText.ToLower()] as Label));
break;
case "multiline":

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).Text =
col.SelectNodes("a:Column.Content", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).ID =
col.SelectNodes("a:Column.ControlName", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).Columns =
int.Parse(col.SelectNodes("a:Column.MultiLineCharW idth", xnm)
[0].InnerText);

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).Rows =
int.Parse(col.SelectNodes("a:Column.MultiLineRowHe ight", xnm)
[0].InnerText);

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).MaxLength =
int.Parse(col.SelectNodes("a:Column.MaxLength", xnm)[0].InnerText);

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).ToolTip =
col.SelectNodes("a:Column.ToolTip", xnm)[0].InnerText;

cell.Controls.Add((controls[col.SelectNodes("a:Column.ControlType",
xnm)[0].InnerText.ToLower()] as TextBox));
multiline = true;
break;
case "textbox":

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).Text =
col.SelectNodes("a:Column.Content", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).ID =
col.SelectNodes("a:Column.ControlName", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).MaxLength =
int.Parse(col.SelectNodes("a:Column.MaxLength", xnm)[0].InnerText);

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as TextBox).ToolTip =
col.SelectNodes("a:Column.ToolTip", xnm)[0].InnerText;

cell.Controls.Add((controls[col.SelectNodes("a:Column.ControlType",
xnm)[0].InnerText.ToLower()] as TextBox));
break;
case "checkbox":

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as CheckBox).Text =
col.SelectNodes("a:Column.Content", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as CheckBox).ID =
col.SelectNodes("a:Column.ControlName", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as CheckBox).ToolTip =
col.SelectNodes("a:Column.ToolTip", xnm)[0].InnerText;

cell.Controls.Add((controls[col.SelectNodes("a:Column.ControlType",
xnm)[0].InnerText.ToLower()] as CheckBox));
break;
case "button":

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as Button).Text =
col.SelectNodes("a:Column.Content", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as Button).ID =
col.SelectNodes("a:Column.ControlName", xnm)[0].InnerText;

(controls[col.SelectNodes("a:Column.ControlType", xnm)
[0].InnerText.ToLower()] as Button).ToolTip =
col.SelectNodes("a:Column.ToolTip", xnm)[0].InnerText;

cell.Controls.Add((controls[col.SelectNodes("a:Column.ControlType",
xnm)[0].InnerText.ToLower()] as Button));
break;
}
// and to finish up, we add the datacell
to the table
trow.Cells.Add(cell);
}
// Add the Validators
// Starting off with the
requiredfieldvalidator
if (multiline)
{
tblDynamicTable.Rows.Add(trow);
trow = new TableRow();
trow.Cells.Add(new TableCell());

TableCell validatorCell = new TableCell();
validatorCell.ColumnSpan = 2;
if (req)
{
RequiredFieldValidator rfv = new
RequiredFieldValidator();
rfv.ControlToValidate =
row.GetAttribute("RequiredFieldName");
rfv.ErrorMessage =
row.GetAttribute("RequiredFieldValidatorErrorMessa ge");
validatorCell.Controls.Add(rfv);
}
// Adding eventual
RegularExpressionValidator
XmlElement validator =
row.SelectSingleNode("a:Validator", xnm) as XmlElement;
if (validator != null)
{
// using default regexes
if
(validator.GetAttribute("UseDefaultRegEx").Equals( bool.TrueString))
{
XmlNodeList regexes =
e.SelectNodes("a:RegEx", xnm);
foreach (XmlNode _regex in
regexes)
{
XmlElement regex = _regex as
XmlElement;
if
(regex.GetAttribute("ID").Equals(validator.GetAttr ibute("DefaultRegExIdentifier")))
{
RegularExpressionValidator
rev = new RegularExpressionValidator();
rev.ValidationExpression =
regex.GetAttribute("Expression");
rev.ControlToValidate =
validator.GetAttribute("ControlToValidate");
rev.ErrorMessage =
regex.GetAttribute("ErrorMessage");

validatorCell.Controls.Add(rev);
break;
}
}
}
// using custom regexes
else
{
RegularExpressionValidator rev =
new RegularExpressionValidator();
rev.ValidationExpression =
validator.GetAttribute("CustomRegEx");
rev.ErrorMessage =
validator.GetAttribute("ErrorMessage");
rev.ControlToValidate =
validator.GetAttribute("ControlToValidate");
validatorCell.Controls.Add(rev);
}
}
// At last adding the validatorcell to the
row and finishing up
trow.Cells.Add(validatorCell);
tblDynamicTable.Rows.Add(trow);
}
else
{
TableCell validatorCell = new TableCell();
if (req)
{
RequiredFieldValidator rfv = new
RequiredFieldValidator();
rfv.ControlToValidate =
row.GetAttribute("RequiredFieldName");
rfv.ErrorMessage =
row.GetAttribute("RequiredFieldValidatorErrorMessa ge");
validatorCell.Controls.Add(rfv);
}
// Adding eventual
RegularExpressionValidator
XmlElement validator =
row.SelectSingleNode("a:Validator", xnm) as XmlElement;
if (validator != null)
{
// using default regexes
if
(validator.GetAttribute("UseDefaultRegEx").Equals( bool.TrueString))
{
XmlNodeList regexes =
e.SelectNodes("a:RegEx", xnm);
foreach (XmlNode _regex in
regexes)
{
XmlElement regex = _regex as
XmlElement;
if
(regex.GetAttribute("ID").Equals(validator.GetAttr ibute("DefaultRegExIdentifier")))
{
RegularExpressionValidator
rev = new RegularExpressionValidator();
rev.ValidationExpression =
regex.GetAttribute("Expression");
rev.ControlToValidate =
validator.GetAttribute("ControlToValidate");
rev.ErrorMessage =
regex.GetAttribute("ErrorMessage");

validatorCell.Controls.Add(rev);
break;
}
}
}
// using custom regexes
else
{
RegularExpressionValidator rev =
new RegularExpressionValidator();
rev.ValidationExpression =
validator.GetAttribute("CustomRegEx");
rev.ErrorMessage =
validator.GetAttribute("ErrorMessage");
rev.ControlToValidate =
validator.GetAttribute("ControlToValidate");
validatorCell.Controls.Add(rev);
}
}
// At last adding the validatorcell to the
row and finishing up
trow.Cells.Add(validatorCell);
tblDynamicTable.Rows.Add(trow);
}
}
}
// now styling footer row

style(this.tblDynamicTable.Rows[this.tblDynamicTable.Rows.Count - 1],
this.tablefooterstyle);
// now styling header row
style(this.tblDynamicTable.Rows[0],
this.tableheaderstyle);
}
}
EnsureChildControls();
base.DataBind();
}

private void style(WebControl item, string Css)
{
string[] directives = Css.Replace(" ",
"").Split(";".ToCharArray());
for (int i = 0; i < directives.Length - 1; i++)
{
string[] kvpair = directives[i].Split(":".ToCharArray());
item.Style.Add(kvpair[0], kvpair[1]);
}
}

void b_Click(object sender, EventArgs e)
{
this.FormSubmitted.Invoke(sender, e);
}
}
 
Reply With Quote
 
 
 
 
Forumtroll
Guest
Posts: n/a
 
      06-02-2008
I've isolated the problem to be due to the validators.

How can I have validators with this, and -- provided the validators
don't trigger -- make sure that the form submit process continues so
the button click event actually triggers?

 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      06-02-2008
Forumtroll presented the following explanation :
> I've isolated the problem to be due to the validators.
>
> How can I have validators with this, and -- provided the validators
> don't trigger -- make sure that the form submit process continues so
> the button click event actually triggers?


It's the job of the validators to prevent a submit (and therefore a
click-event) when "the conditions" are not satisfied.

If you want to ignore the validators for a particular button, set the
CausesValidation property (of that button) to false.

Hans Kesting


 
Reply With Quote
 
Forumtroll
Guest
Posts: n/a
 
      06-02-2008
On Jun 2, 1:00*pm, Hans Kesting <news.han...@spamgourmet.com> wrote:
> Forumtroll presented the following explanation :
>
> > I've isolated the problem to be due to the validators.

>
> > How can I have validators with this, and -- provided the validators
> > don't trigger -- make sure that the form submit process continues so
> > the button click event actually triggers?

>
> It's the job of the validators to prevent a submit (and therefore a
> click-event) when "the conditions" are not satisfied.
>
> If you want to ignore the validators for a particular button, set the
> CausesValidation property (of that button) to false.
>
> Hans Kesting


I've managed to narrow the problem down to the fact that even though
validation succeeds on the client side, the generated client side
scripts never continues to submit the form, thus breaking the submit.

how to circumvent that?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
firing button click event before page load event.....???? GauravGupta ASP .Net 4 01-19-2012 06:52 AM
Button disabled and re-enabled in client side not firing server-side click event. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?= ASP .Net 2 05-27-2008 09:57 AM
Button Event Not Firing From Dynamic WebControls on Web Farm. =?Utf-8?B?QmFtbWVyMjI=?= ASP .Net 0 07-13-2004 12:41 AM
Button Click event not firing Ken Cox [Microsoft MVP] ASP .Net 2 04-28-2004 01:47 AM
Button control _Click event not firing Max ASP .Net 3 12-31-2003 04:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57