[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

TextChanged event in custom control is always firing

Asked by ShawnG in Programming for ASP.NET, .NET Framework 3.x versions, Microsoft Visual C#.Net

Hi!
I've created a custom control consisting of a TextBox, an ImageButton and a CalendarExtender.  The control is used in a GridView.  I also have extended the GridView to keep track of the rows that have been altered.  The GridView adds an event to my custom control that fires when it has been altered.  Below is the code in the extended gridview:

private List<GridViewRow> dirtyRows = new List<GridViewRow>();

    [Bindable(true)]
    [Category("Behaviour")]
    [DefaultValue("")]
    [Localizable(true)]
    [Browsable(false)]
    public List<GridViewRow> GetChangedRows
    {
      get { return this.dirtyRows; }
    }

    protected override void InitializeRow(GridViewRow row, DataControlField[] fields)
    {
      base.InitializeRow(row, fields);
      foreach (DataControlFieldCell cell in row.Cells)
      {
        if (cell.Controls.Count > 0)
        {
          AddChangedHandlers(cell.Controls);
        }
      }
    }

    private void AddChangedHandlers(ControlCollection controls)
    {
      foreach (Control ctrl in controls)
      {
        if (ctrl is CustomControls.DateBoxGrid)
        {
          ((CustomControls.DateBoxGrid)ctrl).TextChanged += new EventHandler(this.HandleRowChanged);
        }
      }
    }

    void HandleRowChanged(object sender, EventArgs args)
    {
      GridViewRow row = ((Control)sender).NamingContainer.NamingContainer as GridViewRow;
      if (null != row && !dirtyRows.Contains(row))
      {
        dirtyRows.Add(row);
      }
    }

The problem occurs when the control is bound to the datasource.  The TextChanged event fires whether I change the value or not.  But if I just set the control's Text property to "01.01.2009" in the html markup it will not fire unless it's changed.  I've posted the code of my custom control in a code snippet.  Please help me out.  Thanks!
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:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
 
namespace CustomControls
{
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:DateBoxGrid runat=server></{0}:DateBoxGrid>")]
  [ValidationProperty("Text")]
  public class DateBoxGrid : WebControl, INamingContainer
  {  
    public DateBox _dateBox = new TextBox();
    public ImageButton _imageButton = new ImageButton();
    public CalendarExtender _calendarExtender = new CalendarExtender();
    public HtmlGenericControl _div = new HtmlGenericControl("div");
 
    [Bindable(true)]
    [Category("Behaviour")]
    [DefaultValue("")]
    [Localizable(true)]
    public event EventHandler TextChanged
    {
      add
      {
        this._dateBox.TextChanged += value;
      }
      remove
      {
        this._dateBox.TextChanged -= value;
      }
    }
 
    [Bindable(true)]
    [Category("Behaviour")]
    [DefaultValue("")]
    [Localizable(true)]
    public string Text
    {
      get { return this._dateBox.Text; }
      set { this._dateBox.Text = value; }
    }
 
    protected override void CreateChildControls()
    {
      this._dateBox.ID = this.ID + "DateBox";
      this._dateBox.CssClass = "GridRoundedDateBox";
      this._imageButton.ID = this.ID + "ImageButton";
      this._imageButton.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(DateBoxGrid), "CustomControls.Images.calendar.gif");
      this._imageButton.CssClass = "PaddingTop";
      this._calendarExtender.ID = this.ID + "CalendarExtender";
      this._calendarExtender.PopupButtonID = _imageButton.ID;
      this._calendarExtender.TargetControlID = _dateBox.ID;
      this._calendarExtender.Format = "dd.MM.yyyy";
      this._div.Attributes.Add("class", "GridRoundedCornersDiv PaddingTop GridBackground91");
 
      this._div.Controls.Add(_dateBox);
      this.Controls.Add(_div);
      this.Controls.Add(_imageButton);
      this.Controls.Add(_calendarExtender);
    }
 
    /// <summary>
    /// Include the embedded resource.
    /// </summary>
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
 
      ScriptManager sm = ScriptManager.GetCurrent(Page);
      if (!sm.IsInAsyncPostBack)
      {
        //Add strylesheet.
        HtmlLink cssLink = new HtmlLink();
        string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControls.CustomControls.css");
        cssLink.Href = cssUrl;
        cssLink.Attributes.Add("rel", "stylesheet");
        cssLink.Attributes.Add("type", "text/css");
        this.Page.Header.Controls.Add(cssLink);
      }
    }
 
    protected override void RenderContents(HtmlTextWriter output)
    {
      this._div.RenderControl(output);
      output.Write("&nbsp;");
      this._imageButton.RenderControl(output);
      this._calendarExtender.RenderControl(output);
    }
  }
}
[+][-]11/06/09 08:41 AM, ID: 25760683Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/06/09 02:11 PM, ID: 25763524Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625