CodeVerge.Net Beta
Login Idy
Register Password
  Forgot?
Explore    Item Entry    Members   
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML





Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 6/14/2004 1:55:45 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 4 Views: 22 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
5 Items, 1 Pages 1 |< << Go >> >|
Tim_H
Asp.Net User
Problems with ClientID on Composite Control6/14/2004 1:55:45 PM

0/0

Hello all

I am having a problem with a composite control. When I have the control setup with the code below, and I put multiple controls on the same page everything works as I expect it to (each sub-control is given a unique ClientID). However, when I add the control to a datagrid, each of the sub-controls is given the exact same ClientID.

I have been playing around with it some and if I remove the INamingContainer from the composite control, it will render correctly in the datagrid(I am assuming that the INamingContainer of the datagrid is taking over and making sure that each control has a unique ClientID.) However, when I do this, on a page that has multiple instances of the control(not in a datagrid), I get the message "Multiple controls with the same ID 'txtDate' were found. FindControl requires that controls have unique IDs."

Any help would be appriciated!

Here is the code for the control:


public class PopUpCalendar : System.Web.UI.WebControls.WebControl, INamingContainer
{
protected TextBox txtDate = new TextBox();
protected Image imgCal = new Image();
protected Image imgClear = new Image();
private System.Resources.ResourceManager m_objRes = new System.Resources.ResourceManager("Calendar.CalScript", typeof(PopUpCalendar).Assembly); // 'used to retrieve client side javascript
#region "Properties"
private string p_sDateFormat = "MM/dd/yyyy";
private string p_sCalImage = "Images/cal.gif";
private string p_sClearImage = "Images/icon_goback.gif";
private bool p_bShowCalImage = true;
private bool p_bShowClearImage = true;
private string p_sJavaFunction = "";
private string p_sSelectedDate = "";
private int p_iTextBoxWidth = 75;

public string DateText
{
get
{
return txtDate.Text;
}
}

[Bindable(true),
Category("Appearance"),
Description("Initial value for the control"),
DefaultValue("")]
public string SelectedDate
{
get
{
return p_sSelectedDate;
}
set
{
this.p_sSelectedDate = value;
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Description("String to format the selected date"),
DefaultValue("MM/dd/yyyy")]
public string DateFormat
{
get
{
return this.p_sDateFormat;
}
set
{
if(value.Trim()=="")
{
this.p_sDateFormat = "MM/dd/yyyy";
}
else
{
this.p_sDateFormat = value;
}
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Description("Javascript function to run after the date has been selected"),
DefaultValue("")]
public string JavaScriptToRunAfterDateSelection
{
get
{
return this.p_sJavaFunction;
}
set
{
this.p_sJavaFunction = value;
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Editor("System.Web.UI.Design.ImageUrlEditor, System.Design", typeof(UITypeEditor)),
Description("Path to the image for the calendar image"),
DefaultValue("Images/cal.gif")]
public string CalendarImage
{
get
{
return this.p_sCalImage;
}
set
{
this.p_sCalImage = value;
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Editor("System.Web.UI.Design.ImageUrlEditor, System.Design", typeof(UITypeEditor)),
Description("Path to the image for the clear date image"),
DefaultValue("Images/icon_goback.gif")]
public string ClearImage
{
get
{
return this.p_sClearImage;
}
set
{
this.p_sClearImage = value;
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Description("Show the calandar image"),
DefaultValue(true)]
public bool ShowCalendarIcon
{
get
{
return this.p_bShowCalImage;
}
set
{
this.p_bShowCalImage = value;
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Description("Show the clear date image"),
DefaultValue(true)]
public bool ShowClearIcon
{
get
{
return this.p_bShowClearImage;
}
set
{
this.p_bShowClearImage = value;
EnsureChildControls();
}
}

[Bindable(true),
Category("Appearance"),
Description("Width of the textbox control"),
DefaultValue("")]
public int TextBoxWidth
{
get
{
return p_iTextBoxWidth;
}
set
{
this.p_iTextBoxWidth = value;
EnsureChildControls();
}
}

#endregion
protected override void CreateChildControls()
{
this.Controls.Clear();
txtDate.Width = TextBoxWidth;
txtDate.ID = "txtDate";
txtDate.ReadOnly = true;
if(SelectedDate!="" && SelectedDate!=null)
{
txtDate.Text = string.Format("{0:" + DateFormat + "}", Convert.ToDateTime(SelectedDate));
}
this.Controls.Add(txtDate);
imgCal.AlternateText = "Show Calendar";
imgCal.ImageUrl = CalendarImage;
imgClear.AlternateText = "Clear Date";
imgClear.ImageUrl = ClearImage;
if(ShowCalendarIcon)
{
this.Controls.Add(imgCal);
}
if(ShowClearIcon)
{
this.Controls.Add(imgClear);
}
}
protected override void OnPreRender(System.EventArgs e)
{
string sScript;

sScript = m_objRes.GetString("calendar");
Page.RegisterClientScriptBlock("PopUpCal", "<script language=javascript>\n" + sScript + "</script>");
}
protected override void Render(HtmlTextWriter writer)
{
this.EnsureChildControls();
if(JavaScriptToRunAfterDateSelection!="")
{
txtDate.Attributes.Add("onclick", "javascript:popUpCalendar(this, '" + txtDate.ClientID + "', '" + DateFormat.ToLower() + "', '" + JavaScriptToRunAfterDateSelection + "(\"" + txtDate.ClientID + "\");');");
}
else
{
txtDate.Attributes.Add("onclick", "javascript:popUpCalendar(this, '" + txtDate.ClientID + "', '" + DateFormat.ToLower() + "', null);");
}
if(ShowCalendarIcon)
{
if(JavaScriptToRunAfterDateSelection!="")
{
imgCal.Attributes.Add("onclick", "javascript:popUpCalendar(this, '" + txtDate.ClientID + "', '" + DateFormat.ToLower() + "', '" + JavaScriptToRunAfterDateSelection + "(\"" + txtDate.ClientID + "\");');");
}
else
{
imgCal.Attributes.Add("onclick", "javascript:popUpCalendar(this, '" + txtDate.ClientID + "', '" + DateFormat.ToLower() + "', null);");
}
imgCal.Attributes.Add("onmouseover", "javascript:this.style.cursor='hand';");
}
if(ShowClearIcon)
{
imgClear.Attributes.Add("onclick", "javascript:popUpClear('" + txtDate.ClientID + "');");
imgClear.Attributes.Add("onmouseover", "javascript:this.style.cursor='hand';");
}
base.Render(writer);
}
Tim_H
Asp.Net User
Re: Problems with ClientID on Composite Control6/14/2004 6:13:34 PM

0/0

Nevermind.

fixed by not using INamingContainer and changing the following line from:

txtDate.ID = "txtDate";


to:

txtDate.ID = this.ID + "_txtDate";

Andy Smith
Asp.Net User
Re: Problems with ClientID on Composite Control6/14/2004 7:41:10 PM

0/0

I'm not really sure what exactly is causing your ID issues... but I see a few things to improve the code, and your problems might go away as a result.

1) CreateChildControls should be used to Insantiate and add ALL child controls to the parent.
2) Don't use those private variables in your properties. Store the values in viewstate. Apply the values in the begginning of Render.
3) use the Visible property of the child controls to decide if they will be rendered. Add them to the Controls collection always.

Here's an abbreviated version of code for you:
public class FooControl : WebControl, INamingContainer {

private WebControl childControl1;
private WebControl childControl2;

public property String SomeImportantProperty {
get {
Object state = ViewState[ "SomeImportantProperty" ];
if ( state != null ) {
return (String)state;
}
}
set {
ViewState[ "SomeImportantProperty" ] = value;
}
}

public property String ShowControl2 {
get {
Object state = ViewState[ "ShowControl2" ];
if ( state != null ) {
return (String)state;
}
}
set {
ViewState[ "ShowControl2" ] = value;
}
}



protected override void CreateChildControls() {
this.Controls.Clear();

this.childControl1 = new WebControl();
this.childControl1.ID = "ChildControl1";

this.childControl2 = new WebControl();
this.childControl2.ID = "ChildControl2";

this.Controls.Add( childControl1 );
this.Controls.Add( childControl2 );
}

protected override void RenderChildren( HtmlTextWriter writer ) {
this.childControl1.ImportantProperty = this.SomeImportantProperty;
this.childControl2.Visible = this.ShowControl2;
base.RenderChildren( writer );
}

}
SStorhaug
Asp.Net User
Re: Problems with ClientID on Composite Control1/18/2008 5:29:30 PM

0/0

Hi Andy,

I am just curious why you suggest to always add the controls and use the visible property to control rendering.  I am building a composite control to organize some old code and make it more manageable.  An example of where the control will be placed is on the following page: http://www.shuttercontractor.com/products/louver-shutters/vinyl-shutters/standard-sized-shutters/alcoa-AL-ML.aspx.  The dropdowns are automatically generated and populated based on values in the database.  The control can be configured in one of 5 ways:

  1. Textbox Control
  2. DropDownList Control
  3. Label Control
  4. CheckBox Control
  5. Two DropDownList Controls

In any given control instance, I will never need any of the other controls in the page lifecycle.  I was thinking it would save me some memory on the server if I didn't load the controls I don't need. Do you think I need to load them in this case?

TIA


-NightOwl888
Exterior Window Shutters
[email protected]
Asp.Net User
Re: Problems with ClientID on Composite Control1/18/2008 7:41:54 PM

0/0

The post you replied to was made three and a half years ago, and in the wrong forum.  You'd do better posting this again in the Custom Server Controls forum.

Jeff


Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
5 Items, 1 Pages 1 |< << Go >> >|



Search This Site:


Meet Our Sponsors:



Other Resources:

Duplicate POP3 Messages - novell.support.groupwise.6x.gwia - Web ... Duplicate POP3 Messages, > ROOT > NEWSGROUP > Novell Forums > novell.support.groupwise.6x.gwia, Date: 4/21 ... problems with clientid on composite control ...
Problem with Composite Control and Ajax - ASP.NET Forums ... General ASP.NET " Web Forms " Problem with Composite Control and Ajax ... now we are trying to put it on an Ajax panel, and we are running into problems. ...
ClientID Not Accurate - ASP.NET Forums ... you can use $get(this.ClientID+"_Gridv") to orient it in composite control. ... Questions / Problems with this site? Visit our hoster: ORCS Web. Running IIS 7.0 ...
CodeProject: An ASP.NET Composite Control for US and UK dates. Free ... ... to differentiate between these problems and resolve the error ... script.Append(sVbCrLf) // script.Append("ValidatorEnable(" & ClientID & ", true) ...
Custom Composite control error You should use ClientID instead of ID. The ID is the value set by the ... ASP.Net Memory problems.. Third Level Domain And ASP.NET 2.0 ...
How to build a simple List to List Composite Web Control - guyS's WebLog ... to the JS function the unique name of the web control by using this.ClientID ... If anyone is having problems with the view state on the destination listbox when ...
ClientID and Request.form() I am writing a composite control that allows two-way binding between a ... Login using NT login details from web form problems. DropDownList won't select item...
ASP.NET - Building Controls I have a composite control that contains a texbox and comparevalidator ... problems with simple custom control. 06 Jun 2007 02:46 GMT. 2 ...


 
All Times Are GMT