简体   繁体   English

如何在下拉列表ASP.Net和VB.Net应用程序中将当前用户设置为默认值

[英]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 ; 以我的asp.net和vb.net Web应用程序的形式; 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.) (选择查询通过内部两个表EmployerUser表和User表来获取数据。User表的主键是ID,这是EmployerUser表中名为UserID的外键。)

In Front end ASP.net coding is 在前端ASP.net编码是

               <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 在后端VB.net中,下拉列表的编码为

       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: 将下拉列表的SelectedValue属性设置为用户名字符串:

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 参见https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#代码片断-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 希望你会发现这篇文章有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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