Button event not firing

Discussion in 'ASP .Net' started by Forumtroll, Jun 2, 2008.

  1. Forumtroll

    Forumtroll Guest

    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.Empty) == 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.MultiLineCharWidth", xnm)
    [0].InnerText);

    (controls[col.SelectNodes("a:Column.ControlType", xnm)
    [0].InnerText.ToLower()] as TextBox).Rows =
    int.Parse(col.SelectNodes("a:Column.MultiLineRowHeight", 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("RequiredFieldValidatorErrorMessage");
    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.GetAttribute("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("RequiredFieldValidatorErrorMessage");
    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.GetAttribute("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.Split(":".ToCharArray());
    item.Style.Add(kvpair[0], kvpair[1]);
    }
    }

    void b_Click(object sender, EventArgs e)
    {
    this.FormSubmitted.Invoke(sender, e);
    }
    }
    Forumtroll, Jun 2, 2008
    #1
    1. Advertising

  2. Forumtroll

    Forumtroll Guest

    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?
    Forumtroll, Jun 2, 2008
    #2
    1. Advertising

  3. Forumtroll

    Hans Kesting Guest

    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
    Hans Kesting, Jun 2, 2008
    #3
  4. Forumtroll

    Forumtroll Guest

    On Jun 2, 1:00 pm, Hans Kesting <> 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?
    Forumtroll, Jun 2, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    3
    Views:
    679
  2. Ken Cox [Microsoft MVP]

    Button Click event not firing

    Ken Cox [Microsoft MVP], Apr 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,515
    Andrea Williams
    Apr 28, 2004
  3. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    Replies:
    2
    Views:
    6,460
    rick74
    May 27, 2008
  4. =?Utf-8?B?QmFtbWVyMjI=?=

    Button Event Not Firing From Dynamic WebControls on Web Farm.

    =?Utf-8?B?QmFtbWVyMjI=?=, Jul 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    409
    =?Utf-8?B?QmFtbWVyMjI=?=
    Jul 13, 2004
  5. GauravGupta
    Replies:
    4
    Views:
    2,061
    Shilpa1234
    Jan 19, 2012
Loading...

Share This Page