简体   繁体   中英

How can I set current user as default value in the drop down list ASP.Net and VB.Net application

In my asp.net and vb.net web application form ; the drop down list for Main Contact holds a list of names. This list is different for different user who are logged in to the application.

Data are populated by select query from database fields. (select query fetches data by Innerjoining two tables- EmployerUser table & User table. Primary key of User table is ID which is a foreign Key in the EmployerUser table named UserID.)

In Front end ASP.net coding is

               <div class="form-element">
                    <label>
                        Main contact (required)
                    </label>
                    <asp:DropDownList ID="comMainContact" runat="server" CssClass="chosen" EnableViewState="True"
                        DataTextField="name" DataValueField="id" />
                </div>

In Back end VB.net coding for the drop down list is

       comMainContact.DataSource = LocalHelper.CachedEmployerUserList(LocalHelper.UserEmployerID)
       comMainContact.DataBind()


       Public Shared Function CachedEmployerUserList(ByVal employerid As Integer) As DataTable
               Dim CacheName As String = "helper_cachedemployeruserlist_" & CStr(employerid)
               Dim DataList As DataTable = Nothing

               Try
                  DataList = HttpContext.Current.Cache.Item(CacheName)
               Catch

               End Try

               If DataList Is Nothing Then

                     DataList = Core.DB.GetData("SELECT [User].id AS 'id', [User].name + ' ' + [User].Surname AS 'name' FROM EmployerUser INNER JOIN [User] ON EmployerUser.userid = [User].id WHERE [User].deleted = 0 AND [User].active = 1 AND [User].emailverified IS NOT NULL AND EmployerUser.employerid = @employerid ORDER BY [User].surname, [User].name", Core.DB.SIP("employerid", employerid))
        HttpContext.Current.Cache.Insert(CacheName, DataList, Nothing, DateAdd(DateInterval.Minute, 1, Now), System.Web.Caching.Cache.NoSlidingExpiration)

               End If

               Dim FinalList As DataTable = DataList.Copy

               Return FinalList

         End Function

The problem is : This coding does not set a default value for the Main Contact . I want to set a default value in drop down list for the Main Contact. In most of the cases the default value is the user himself. So it would be ideal if the current user is set as default value in the drop down list as well as the user is also able to change value from the drop down list if himself is not the Main Contact.

How can I do that.

I can separately determine who is the currently logged in user.

The coding to determine current user is (Current logged in User can be determined via select query which fetches data from User table.)

Public Class User

 Public Shared Function UserFullName(ByVal User As Mypeoplebiz.User, Optional ByVal Title As Boolean = True) As String

        If Title Then

            Return Core.DB.GetString("SELECT name FROM Title WHERE id = @titleid", Core.DB.SIP("titleid", User.TitleID)) & " " & User.Name & " " & User.Surname

        Else
            Return User.Name & " " & User.Surname

        End If

    End Function

End Class

How can I set current user is set as default value in the drop down list and give the user the option of changing value from the drop down list .

Appreciate your help.

Thanks

Set the SelectedValue property of the dropdown list to the username string:

comMainContact.SelectedValue = UserFullName(arguments)

See https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

The following lines of code solved my problem. So I want to share it if any one have similar problem.

    Private strUserName As String = ""

            strUserName = Core.DB.GetString("SELECT name + ' '+surname FROM [user] WHERE id = " & LocalHelper.UserID)
            comMainContact.DataSource = LocalHelper.CachedEmployerUserList(LocalHelper.UserEmployerID)
            comMainContact.DataBind()
            comMainContact.SelectedIndex = comMainContact.Items.IndexOf(comMainContact.Items.FindByText(strUserName))

Hope you will find the post useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM