Aug 31 2009

DropDownList in Asp.net

Asp.net Dropdownlist controls is used to create a list of items in dropdownlist

Main Properties of DropDownList

AutopostBack

OnSelectedIndexChanged

Adding Item values to DropDownList as Static

<asp:DropDownList ID=”DropDownList2″ runat=”server”>

<asp:ListItem>Electronics</asp:ListItem>

<asp:ListItem>Movies</asp:ListItem>

<asp:ListItem>Games</asp:ListItem>

</asp:DropDownList>

By Default, DropDownList display First Item as view

i.e SelectedIndex=0 (Electronics)

Result :

You can change Default value of DropDownlist at runtime using selectedIndex

protected void Page_Load(object sender, EventArgs e)

{

DropDownList2.SelectedIndex = 1;

}

Result:

Add DropDownList Items as Dynamically

DropDownList2.Items.Add(”Forums”);

DropDownList2.Items.Add(”Blogs”);

asp.net dropdownlist selectedindexchanged

  1. Drag TextBox control and Dropdownlist in default.aspx
  2. Make Dropdownlist AutoPostBack Property True  //Remember
  3. then double click in Dropdownlist then SelectedindexChanged event fires

Sample code:

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)

{

TextBox1.Text = DropDownList2.SelectedItem.Value;

}

using above code DropdownList SelectedList Item will display in TextBox

.

Note: If You have Any code in Page_Load About Dropdownlist keep in

If(!IsPostBack)

//Page_Load Event

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

DropDownList2.SelectedIndex = 1;

DropDownList2.Items.Add(”Forums”);

DropDownList2.Items.Add(”Blogs”);

}

}

Aug 29 2009

Asp.net Ajax Calender Control

Ajax Calender control:

Ajax Calender Control is an asp.net control associated with Textbox. When the user clicks on the textbox it pops up. It provides client-side date-picking functionality with customizable date format and UI in a popup control. The user can then set a date by clicking on a day, navigate months by clicking on the left and right arrow and perform other such actions without a postback.

How to use Ajax Calender control in Asp.net

1)     In Aspx Page add one TextBox id is txtDate  and Ajax Calender control.

2)    Sample Code :

<%@ Register Assembly=”AjaxControlToolkit” Namespace=”AjaxControlToolkit” TagPrefix=”cc1” %> //add this

<asp:TextBox ID=”txtDate” runat=”server” ></asp:TextBox>

<cc1:CalendarExtender ID=”CalendarExtender1″ runat=”server” PopupButtonID=”txtDate” Format=”dd/MM/yyyy”             TargetControlID=”txtDate”> </cc1:CalendarExtender>

1)       TargetControlID – The ID of the Textbox to extend with the calendar.

(In Our Example TargetControlID is txtDate)

2)       CssClass – Name of the CSS class used to style the calendar. See the Calendar Theming section for more information.

3)     FormatFormat string used to display the selected date.

Formats:       “dd/MM/yyyy”

           "MMMM d, yyyy"

4)    PopupButtonID – The ID of a control to show the calendar popup when clicked. If this value is not set, the calendar will pop up when the textbox receives focus.

(In Our Example PopupControlID  is txtDate)

5)       PopupPosition – Indicates where the calendar popup should appear at the BottomLeft(default), BottomRight, TopLeft, TopRight, Left or Right of the TextBox.

Explaination: When user clicks in TextBox (txtDate), the ajax calendar control will popup and the selected date will be displayed in TextBox (txtDate)

Alibi3col theme by Themocracy