ControlHelper

CleanTextBoxes(TextBox[] textBoxes)

Trims the strings within the array of text boxes specified.

Code in front
<asp:TextBox ID="TextBox1" runat="server" Text=" text " />
<asp:TextBox ID="TextBox2" runat="server" Text="  more text   " />

Code behind
var textboxes = new TextBox[] { TextBox1, TextBox2 };
ControlHelper.CleanTextBoxes(textboxes);

Console.WriteLine(TextBox1.Text); // "text"
Console.WriteLine(TextBox2.Text); // "another text"

FindControl<T>(Control control, string id)

Finds a control of a specified type and specified ID within a control.

Code in front
<asp:Panel ID="BigPanel" runat="server">
	<asp:TextBox ID="TextBox1" runat="server" Text="text" />
</asp:Panel>

Code behind
var find1 = ControlHelper.FindControl<TextBox>(BigPanel, "TextBox1");
var find2 = ControlHelper.FindControl<Control>(BigPanel, "TextBox1");
var find3 = ControlHelper.FindControl<Control>(BigPanel, "TextBox2");

Console.WriteLine(find1.Text); // "text"
Console.WriteLine(find2.Text); // compilation error - find2 needs to be cast as TextBox to access Text property
Console.WriteLine(find3 == null); // true

FindControl<T>(Control control)

Finds the first control of a specified type within a control.

Code in front
<asp:Panel ID="BigPanel" runat="server">
	<asp:TextBox ID="TextBox1" runat="server" Text="text" />
	<asp:Label ID="Label1" runat="server" Text="text" />
	<asp:Label ID="Label2" runat="server" Text="text" />
</asp:Panel>

Code behind
var find1 = ControlHelper.FindControl<Label>(BigPanel);
var find2 = ControlHelper.FindControl<DropDownList>(BigPanel);

Console.WriteLine(find1.ID); // "Label1"
Console.WriteLine(find2 == null); // true

InsertMonthOptions(ListControl list)

Inserts a list of months, in full names, intro a list.

var list = new DropDownList();
ControlHelper.InsertMonthOptions(list);

list.SelectedIndex = 2;
Console.WriteLine(list.SelectedValue); // 3
Console.WriteLine(list.SelectedText); // March

InsertNumberOptions(ListControl list, int start, int end, string format)

Inserts a list of numbers into a list control, from start to end, in a format specified by format. See Numeric Format Strings on MSDN.

There's a overload negating the format parameter that assumes a format of "#".

var list1 = new DropDownList();
var list2 = new DropDownList();
ControlHelper.InsertNumberOptions(list1, 2000, 2010);
ControlHelper.InsertNumberOptions(list2, 1, 12);

list1.SelectedIndex = 4;
list2.SelectedIndex = 4;

Console.WriteLine(list1.SelectedValue); // 2004
Console.WriteLine(list2.SelectedValue); // 05
Console.WriteLine(list2.SelectedText); // 05

Last edited Aug 23, 2010 at 3:04 AM by weiwong, version 8

Comments

No comments yet.