<asp:ListBox ID="listbox_Userrole" runat="server" DataSourceID="SK" DataTextField="RoleName" DataValueField="RoleName" ></asp:ListBox>
<asp:SqlDataSource ID="SurelyKno开发者_Python百科wn" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT RoleName FROM tbl_role WHERE RoleID>1"></asp:SqlDataSource>
How to select first value in list box by default when the page loads.
In your code-behind, add the following to your Page_Load
event handler:
protected override void Page_Load(object sender, EventArgs e)
{
this.listbox_Userrole.SelectedIndex = 0;
}
In page load, set listbox_userrole.selectedindex = 0
Use the DataBound event of the list box like this :-
protected void lstBox_DataBound(object sender, EventArgs e)
{
if (lstBox.Items.Count > 0)
{
lstBox.SelectedIndex = 0;
}
}
Hope this helps.
精彩评论