Here i am going to populate statically DataTextField and DataValueField in DropDownList using c#.
ddlName.Items.Insert(0, new ListItem("-- Select --", "0"));
Note : In above, there is
first zero stands for position of ListItem and
second zero is DataValue of the ListItem and
"-- Select --" is the DataText for visible in DropDown.
Add Asp:DropDownList Control to the page like:
<tr>
<td><b>Name</b></td>
<td><asp:DropDownList ID="ddlName" runat="server"</asp:DropDownList></td>
</tr>
Add C# code to bind the Name to the DropdownList as below :
private void BindNames()
{
ddlName.Items.Insert(0, new ListItem("-- Select Name --", "0"));
ddlName.Items.Insert(1, new ListItem("Ajay Gangwar", "1"));
ddlName.Items.Insert(2, new ListItem("Shashank Tripathi", "2"));
ddlName.Items.Insert(3, new ListItem("Heena Nautiyal", "3"));
}
Note : Do not forget to call this method on Page_Load().
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindNames();
}
}
No comments:
Post a Comment