Button event not firing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Forumtroll

    Button event not firing

    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.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Configur ation;
    using System.Data;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.H tmlControls;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Xml;

    public partial class uc_XmlGui : System.Web.UI.U serControl
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {
    this.FormSubmit ted += new EventHandler(Xm lGui_FormSubmit ted);
    }

    void XmlGui_FormSubm itted(object sender, EventArgs e)
    {
    }

    private XmlDocument m_xmlguidatasou rce;
    private string m_targetform;
    private string tableStyle;
    private string headercolumnsty le;
    private string datacolumnstyle ;
    private string tablefooterstyl e;
    private string tableheaderstyl e;

    /// <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_xmlguida tasource;
    }
    set
    {
    this.m_xmlguida tasource = value;
    }
    }

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

    /// <summary>
    /// binds he datasource to the GUI
    /// </summary>
    public override void DataBind()
    {
    XmlElement e = m_xmlguidatasou rce.DocumentEle ment;
    XmlNamespaceMan ager xnm = new
    XmlNamespaceMan ager(m_xmlguida tasource.NameTa ble);
    xnm.AddNamespac e("a", "http://m2c.no/GuiSchematics.x sd");
    // finding the proper Form definition
    XmlNodeList nl = e.SelectNodes("/*/a:Form", xnm);
    foreach (XmlNode _form in nl)
    {
    XmlElement form = _form as XmlElement;
    if (form.GetAttrib ute("Name").Equ als(m_targetfor m))
    {
    // We've got the right form!
    // Now getting the styles
    XmlNodeList styles = form.SelectNode s("a:Styles/*",
    xnm);
    foreach (XmlNode style in styles)
    {
    switch (style.Name)
    {
    case "TableStyle ":
    this.tableStyle = style.InnerText ;
    break;
    case "TableHeaderSty le":
    this.tableheade rstyle = style.InnerText ;
    break;
    case "HeaderColumnSt yle":
    this.headercolu mnstyle = style.InnerText ;
    break;
    case "DataColumnStyl e":
    this.datacolumn style = style.InnerText ;
    break;
    case "TableFooterSty le":
    this.tablefoote rstyle = style.InnerText ;
    break;
    }
    }
    // done fetching the styles. now styling the table
    style(this.tblD ynamicTable, this.tableStyle );
    // now fetching the rows
    XmlNodeList rows = form.SelectNode s("a:Rows/*",
    xnm);
    foreach (XmlNode _row in rows)
    {
    XmlElement row = _row as XmlElement;
    bool req =
    bool.Parse(row. GetAttribute("R equired"));
    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<stri ng, objectcontrols = new
    Dictionary<stri ng, object>();
    controls.Add("l abel", new Label());
    controls.Add("c heckbox", new CheckBox());
    TextBox __tb = new TextBox();
    __tb.TextMode = TextBoxMode.Mul tiLine;
    controls.Add("m ultiline", __tb);
    controls.Add("t extbox", new TextBox());
    Button b = new Button();
    b.Click += new EventHandler(b_ Click);
    controls.Add("b utton", b);
    if
    (bool.Parse(col .GetAttribute(" IsHeaderColumn" )))
    style(cell, this.headercolu mnstyle);
    else
    style(cell, this.datacolumn style);
    // Set the colspan property if needed
    if
    (col.GetAttribu te("ColumnSpan" ).Equals(string .Empty) == false)
    cell.ColumnSpan =
    int.Parse(col.G etAttribute("Co lumnSpan"));
    // configuring the column and adding to
    cell.Controls
    collection
    switch
    (col.SelectNode s("a:Column.Con trolType", xnm)[0].InnerText.ToLo wer())
    {
    case "label":

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as Label).Text =
    col.SelectNodes ("a:Column.Cont ent", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as Label).ID =
    col.SelectNodes ("a:Column.Cont rolName", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as Label).ToolTip =
    col.SelectNodes ("a:Column.Tool Tip", xnm)[0].InnerText;

    cell.Controls.A dd((controls[col.SelectNodes ("a:Column.Cont rolType",
    xnm)[0].InnerText.ToLo wer()] as Label));
    break;
    case "multiline" :

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).Text =
    col.SelectNodes ("a:Column.Cont ent", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).ID =
    col.SelectNodes ("a:Column.Cont rolName", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).Column s =
    int.Parse(col.S electNodes("a:C olumn.MultiLine CharWidth", xnm)
    [0].InnerText);

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).Rows =
    int.Parse(col.S electNodes("a:C olumn.MultiLine RowHeight", xnm)
    [0].InnerText);

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).MaxLen gth =
    int.Parse(col.S electNodes("a:C olumn.MaxLength ", xnm)[0].InnerText);

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).ToolTi p =
    col.SelectNodes ("a:Column.Tool Tip", xnm)[0].InnerText;

    cell.Controls.A dd((controls[col.SelectNodes ("a:Column.Cont rolType",
    xnm)[0].InnerText.ToLo wer()] as TextBox));
    multiline = true;
    break;
    case "textbox":

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).Text =
    col.SelectNodes ("a:Column.Cont ent", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).ID =
    col.SelectNodes ("a:Column.Cont rolName", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).MaxLen gth =
    int.Parse(col.S electNodes("a:C olumn.MaxLength ", xnm)[0].InnerText);

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as TextBox).ToolTi p =
    col.SelectNodes ("a:Column.Tool Tip", xnm)[0].InnerText;

    cell.Controls.A dd((controls[col.SelectNodes ("a:Column.Cont rolType",
    xnm)[0].InnerText.ToLo wer()] as TextBox));
    break;
    case "checkbox":

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as CheckBox).Text =
    col.SelectNodes ("a:Column.Cont ent", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as CheckBox).ID =
    col.SelectNodes ("a:Column.Cont rolName", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as CheckBox).ToolT ip =
    col.SelectNodes ("a:Column.Tool Tip", xnm)[0].InnerText;

    cell.Controls.A dd((controls[col.SelectNodes ("a:Column.Cont rolType",
    xnm)[0].InnerText.ToLo wer()] as CheckBox));
    break;
    case "button":

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as Button).Text =
    col.SelectNodes ("a:Column.Cont ent", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as Button).ID =
    col.SelectNodes ("a:Column.Cont rolName", xnm)[0].InnerText;

    (controls[col.SelectNodes ("a:Column.Cont rolType", xnm)
    [0].InnerText.ToLo wer()] as Button).ToolTip =
    col.SelectNodes ("a:Column.Tool Tip", xnm)[0].InnerText;

    cell.Controls.A dd((controls[col.SelectNodes ("a:Column.Cont rolType",
    xnm)[0].InnerText.ToLo wer()] 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
    requiredfieldva lidator
    if (multiline)
    {
    tblDynamicTable .Rows.Add(trow) ;
    trow = new TableRow();
    trow.Cells.Add( new TableCell());

    TableCell validatorCell = new TableCell();
    validatorCell.C olumnSpan = 2;
    if (req)
    {
    RequiredFieldVa lidator rfv = new
    RequiredFieldVa lidator();
    rfv.ControlToVa lidate =
    row.GetAttribut e("RequiredFiel dName");
    rfv.ErrorMessag e =
    row.GetAttribut e("RequiredFiel dValidatorError Message");
    validatorCell.C ontrols.Add(rfv );
    }
    // Adding eventual
    RegularExpressi onValidator
    XmlElement validator =
    row.SelectSingl eNode("a:Valida tor", xnm) as XmlElement;
    if (validator != null)
    {
    // using default regexes
    if
    (validator.GetA ttribute("UseDe faultRegEx").Eq uals(bool.TrueS tring))
    {
    XmlNodeList regexes =
    e.SelectNodes(" a:RegEx", xnm);
    foreach (XmlNode _regex in
    regexes)
    {
    XmlElement regex = _regex as
    XmlElement;
    if
    (regex.GetAttri bute("ID").Equa ls(validator.Ge tAttribute("Def aultRegExIdenti fier")))
    {
    RegularExpressi onValidator
    rev = new RegularExpressi onValidator();
    rev.ValidationE xpression =
    regex.GetAttrib ute("Expression ");
    rev.ControlToVa lidate =
    validator.GetAt tribute("Contro lToValidate");
    rev.ErrorMessag e =
    regex.GetAttrib ute("ErrorMessa ge");

    validatorCell.C ontrols.Add(rev );
    break;
    }
    }
    }
    // using custom regexes
    else
    {
    RegularExpressi onValidator rev =
    new RegularExpressi onValidator();
    rev.ValidationE xpression =
    validator.GetAt tribute("Custom RegEx");
    rev.ErrorMessag e =
    validator.GetAt tribute("ErrorM essage");
    rev.ControlToVa lidate =
    validator.GetAt tribute("Contro lToValidate");
    validatorCell.C ontrols.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)
    {
    RequiredFieldVa lidator rfv = new
    RequiredFieldVa lidator();
    rfv.ControlToVa lidate =
    row.GetAttribut e("RequiredFiel dName");
    rfv.ErrorMessag e =
    row.GetAttribut e("RequiredFiel dValidatorError Message");
    validatorCell.C ontrols.Add(rfv );
    }
    // Adding eventual
    RegularExpressi onValidator
    XmlElement validator =
    row.SelectSingl eNode("a:Valida tor", xnm) as XmlElement;
    if (validator != null)
    {
    // using default regexes
    if
    (validator.GetA ttribute("UseDe faultRegEx").Eq uals(bool.TrueS tring))
    {
    XmlNodeList regexes =
    e.SelectNodes(" a:RegEx", xnm);
    foreach (XmlNode _regex in
    regexes)
    {
    XmlElement regex = _regex as
    XmlElement;
    if
    (regex.GetAttri bute("ID").Equa ls(validator.Ge tAttribute("Def aultRegExIdenti fier")))
    {
    RegularExpressi onValidator
    rev = new RegularExpressi onValidator();
    rev.ValidationE xpression =
    regex.GetAttrib ute("Expression ");
    rev.ControlToVa lidate =
    validator.GetAt tribute("Contro lToValidate");
    rev.ErrorMessag e =
    regex.GetAttrib ute("ErrorMessa ge");

    validatorCell.C ontrols.Add(rev );
    break;
    }
    }
    }
    // using custom regexes
    else
    {
    RegularExpressi onValidator rev =
    new RegularExpressi onValidator();
    rev.ValidationE xpression =
    validator.GetAt tribute("Custom RegEx");
    rev.ErrorMessag e =
    validator.GetAt tribute("ErrorM essage");
    rev.ControlToVa lidate =
    validator.GetAt tribute("Contro lToValidate");
    validatorCell.C ontrols.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.tblD ynamicTable.Row s[this.tblDynamic Table.Rows.Coun t - 1],
    this.tablefoote rstyle);
    // now styling header row
    style(this.tblD ynamicTable.Row s[0],
    this.tableheade rstyle);
    }
    }
    EnsureChildCont rols();
    base.DataBind() ;
    }

    private void style(WebContro l item, string Css)
    {
    string[] directives = Css.Replace(" ",
    "").Split(";".T oCharArray());
    for (int i = 0; i < directives.Leng th - 1; i++)
    {
    string[] kvpair = directives[i].Split(":".ToCh arArray());
    item.Style.Add( kvpair[0], kvpair[1]);
    }
    }

    void b_Click(object sender, EventArgs e)
    {
    this.FormSubmit ted.Invoke(send er, e);
    }
    }
  • Forumtroll

    #2
    Re: Button event not firing

    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?

    Comment

    • Hans Kesting

      #3
      Re: Button event not firing

      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
      CausesValidatio n property (of that button) to false.

      Hans Kesting


      Comment

      • Forumtroll

        #4
        Re: Button event not firing

        On Jun 2, 1:00 pm, Hans Kesting <news.han...@sp amgourmet.comwr ote:
        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
        CausesValidatio n 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?

        Comment

        Working...