开发者

How to select first value in list box by default when the page loads

<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&gt;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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜