Here is how to set focus in a user control. I used it in an update panel.
ScriptManager.GetCurrent(this.Page).SetFocus(txtTarget);
Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.
Showing posts with label C# Web User Control. Show all posts
Showing posts with label C# Web User Control. Show all posts
Thursday, March 20, 2008
Wednesday, February 27, 2008
Web User Control Library error
When you create a User Control from a class and try to drag and drop the control into the design page, you may get an Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC)) error. Talk about confusing. My solution was to make my (user control) class public.
Saturday, February 16, 2008
FindContol Solution to add Javascript to a control from a user control
Here is a great solution to finding a control using FindControl method from a user control. TargetcontrolID is the public property for TextBoxFooExtender (User Control). _txt is the targeted TextBox assuming it is found.
protected override void CreateChildControls()
{
txt = (TextBox)this.Parent.FindControl(TargetControlID);
if (txt != null) {
_txt.Attributes.Add("ondblclick", "alert('foo')");
}
base.CreateChildControls();
}
Labels:
C# AJAX,
C# javascript,
C# MasterPage,
C# Web User Control,
FindControl
Tuesday, February 12, 2008
Understanding Default Values for User Web Controls
Understanding default values for User Web Controls can be confusing.
First, there is the DefaultValue for the code behind, which does not act as a default value, but as a default property value for the properties window. Here is the Default Property Value for the properties window set in the ascx.cs page:
First, there is the DefaultValue for the code behind, which does not act as a default value, but as a default property value for the properties window. Here is the Default Property Value for the properties window set in the ascx.cs page:
Now, what you really want is the default value for the control to be set in the ascx page as follows:
[Browsable(true)]
[DefaultValue("")]
public string Text
{
get { return Label.Text; }
set { Label.Text = value;
}
}
You can then manipulate the value in the DataBind, CreateChildControls, or Property section. Be aware that the property section doesn't necessarily fire, hence the need to use the ascx properties.
<asp:Label ID="Label" runat="server" Text=""></asp:Label>
Sunday, February 10, 2008
Creating a ListItemCollection Template User Control
Creating a ListItemCollection Template User Control. Here are the 3 files necessary. A link to the reference link is below.
First: The user control code-behind:
Credit to: http://developers.de/blogs/damir_dobric/archive/2007/03/30/Implementing-Custom-UserControl-with-ListItem-collection.aspx
First: The user control code-behind:
Next, the User control
[DefaultEvent("SelectedIndexChanged"),
Designer("System.Web.UI.Design.WebControls.ListControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
ParseChildren(true, "Items"), ControlValueProperty("SelectedValue"),
DataBindingHandler("System.Web.UI.Design.WebControls.ListControlDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public partial class ComboBoxControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
private ListItemCollection m_Items;
[DefaultValue((string)null), MergableProperty(false),
PersistenceMode(PersistenceMode.InnerDefaultProperty),
Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
public virtual ListItemCollection Items
{
get
{
if (this.m_Items == null)
{
this.m_Items = new ListItemCollection();
}
return this.m_Items;
}
}
protected override void CreateChildControls()
{
if (this.m_Items == null)
{
this.m_Items = new ListItemCollection();
}
if (m_Items.Count>0) {
foreach (ListItem item in m_Items)
{
_ComboBoxControl.Items.Add(item);
}
}
}
public override void DataBind()
{
CreateChildControls();
ChildControlsCreated = true;
_ComboBoxControl.DataBind();
base.DataBind();
}
}
And finally the aspx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ComboBoxControl.ascx.cs" Inherits="ComboBoxControl" %>
<asp:DropDownList ID="_ComboBoxControl" runat="server">
</asp:DropDownList>
<uc1:ComboBoxControl ID="ComboBoxControl1" runat="server">
<Items>
<asp:ListItem Value="Value1" Text="Text1" />
<asp:ListItem Value="Value2" Text="Text2" />
<asp:ListItem Value="Value3" Text="Text3" />
<asp:ListItem Value="Value4" Text="Text4" />
</Items>
</uc1:ComboBoxControl>
Credit to: http://developers.de/blogs/damir_dobric/archive/2007/03/30/Implementing-Custom-UserControl-with-ListItem-collection.aspx
Tuesday, February 5, 2008
Web Control IDs INamingContainer to avoid duplicate IDs
The ID property of the WebUserControl for controls that may be used mulitiple times in an aspx page should not be repeated. Simple implment the INamingContainer to prevent duplicate IDs as follows:
Marker Interface - An interface that does not require explicit implementation.
public partial class UCRepeater : System.Web.UI.UserControl, INamingContainer
Labels:
C# Interface,
C# Web User Control,
INamingContainer
Saturday, December 29, 2007
Web User Control shortcut
I just discovered a Web User Control shortcut for using your controls in apsx pages.
As per the reference link's example, add your implementation of this example:
elements.
Then you can reference it in aspx as
Reference: Link
As per the reference link's example, add your implementation of this example:
All in the Web.config file in the
<add tagPrefix="scottgu" src="~/Controls/Footer.ascx" tagName="footer"/>
<add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
Then you can reference it in aspx as
Reference: Link
Tuesday, December 4, 2007
Creating a Web User Control
Creating a Custom Web Control.
Within Visual Studio:
To keep organized, I create a company/website/user folder. Within the folder I create another folder named Controls. For example I'll have this directory structure: FooInc/Controls.
Within that folder, rt. click and Add New Item. Select the WebUserControl option. I'll name this one WebUserControl_DatePanel.ascx.
Switch to design view. Add a Panel (named Panel1 by default). Add a Label (named Label1 by default) within Panel1. Set Panel1's background to cyan for a custom feel.
In the load Event of WebUserControl_DatePanel.ascx, add the following:
Label1.Text = DateTime.Now.ToString();
Now save and close those files.
In your project, create a new Web Form named WebUser.aspx. Switch to design view.
From the Solution Explorer, drag the WebUserControl_DatePanel.ascx onto the page (much like adding a CSS file). Switch to Source View.
You will notice the following header:
There it is. You can add custom functions at the web user control source if you want additional functionality.
Within Visual Studio:
To keep organized, I create a company/website/user folder. Within the folder I create another folder named Controls. For example I'll have this directory structure: FooInc/Controls.
Within that folder, rt. click and Add New Item. Select the WebUserControl option. I'll name this one WebUserControl_DatePanel.ascx.
Switch to design view. Add a Panel (named Panel1 by default). Add a Label (named Label1 by default) within Panel1. Set Panel1's background to cyan for a custom feel.
In the load Event of WebUserControl_DatePanel.ascx, add the following:
Label1.Text = DateTime.Now.ToString();
Now save and close those files.
In your project, create a new Web Form named WebUser.aspx. Switch to design view.
From the Solution Explorer, drag the WebUserControl_DatePanel.ascx onto the page (much like adding a CSS file). Switch to Source View.
You will notice the following header:
You will also notice the following web user control in the body:
<%@ Register Src="FooInc/Controls/WebUserControl_DatePanel.ascx" TagName="WebUserControl_DatePanel"
TagPrefix="uc1" %>
<uc1:WebUserControl_DatePanel ID="WebUserControl_DatePanel1" runat="server" />
There it is. You can add custom functions at the web user control source if you want additional functionality.
Subscribe to:
Posts (Atom)