基于用户ID限制下拉选项 [英] Limiting dropdown options based on userID

查看:170
本文介绍了基于用户ID限制下拉选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题,因为我不能确定的解决我的问题的最好办法。

I am asking this question because I'm unsure of the best way to solve my problem.

问题: 我有一个pre填充下拉1000左右的号码列表。我需要限制向下基于哪个用户正在使用下拉哪些号码出现在下降​​。

Problem: I have a pre-populated drop down list of 1,000 or so numbers. I need to limit which numbers appear in the drop down based on which user is using the drop down.

解决方案我想到了:

  1. 隐藏所有的数字与jQuery
  2. 使用jQuery的/ AJAX调用数据库,通过用户ID
  3. 在DB返回基于用户ID值的列表
  4. 有相同的值作为数字从数据库返回的下拉显示选项

可以说这是我的 HTML

<select>
<option>Please Select..</option>
<option value="101"> CC 101 </option>
<option value="102"> CC 102 </option>
<option value="103"> CC 103 </option>
<option value="104"> CC 104 </option>
<option value="105"> CC 105 </option>
<option value="106"> CC 106 </option>
</select> 

这是我的数据库表

=======================
|   User1    |  101   |
|   User2    |  101   |
|   User2    |  102   |
|   User2    |  103   |
|   User3    |  103   |
=======================

我要弄清楚,例如,如何通过用户2,然后返回101,102,103。

I need to figure out, for example, how to pass user2 and then return 101,102,103.

我只知道基本的JS / jQuery的,我不是很有经验的数据块,所以我欢迎任何建议,可以帮助我实现我的最终目标。

I only know basic js/jQuery, and I am not very experienced with DBs, so I am welcome to any suggestions that can help me accomplish my end goal.

编辑/注:由于愚蠢的,因为这听起来....安全性是不是在这里一个巨大的交易。这将在公司内部网站,在那里如果用户觉得需要破解周围,并选择不同数量的使用,它真的会那么重要。我也没有预见任何具有愿望/需要本公司的员工/要选择不同的选项比他们被允许。

EDIT/NOTE: As dumb as this sounds.... Security is not a huge deal here. This will be used on an internal company website, where if a user felt the need to hack around and select a different number, it really wouldn't matter that much. I also don't foresee any of the employees of the company having the desire/need/want to select a different option than they are allowed.

此外,该列表的必须是pre-填充然后是数字隐藏。它的方式,我使用的平台设置,所以我必须使用显示/隐藏,或者类似的东西。

Also, the list must be pre-populated then numbers hidden. Its the way the platform I am using is set up, so I have to use show/hide, or something similar.

推荐答案

如果是这样的示例数据库表和选择元素的例子。然后,我认为最好的方法是将不是你自己的写东西,只是让数据库选择什么,分享什么,在哪里共享。

If that's the example Database Table and an example of a Select element. Then I think the best method would be to not write anything on your own and just let the Database choose what to share and where to share.

下面我们一起去的编码。我会尽量解释什么,我写作,我如何写作;因为你是一个新手: - )

Here we go with the coding. I will try to explain what I am writing and how I am writing; since you're a newbie :-)

在HTML $ C $下你的工作,会是这么简单

The HTML code for your job, would be simple as this

<select name="someName">
  <!-- No options please! -->
</select>

获取用户ID

现在,一旦用户已登录,尝试通过以下的方法中的任何一个,以获得用户ID

Getting the UserId

Now, once the user has logged in. Try to get the UserId by any one of the following method.

要使用jQuery,你将需要使用服务器生成的值,因为jQuery不能干预的服务器请求和code。所以,你可以创建一个简单的隐藏的输入,并给它登录用户的电流值。下面是例子

To use jQuery you will need to be using the Server's generated value, because jQuery cannot interfere the Server Requests and code. So, you can create a simple hidden input and give it the value of the current logged in user. Here is the example

<input type="hidden" id="userId" value="@WebSecurity.CurrentUserId" />

现在,使用jQuery你可以得到这个输入值。这里将是code为

Now, using jQuery you can get the value for this input. Here would be the code for that

var userId = $('#userId').val();

ASP.NET

要使用ASP.NET,你没有做任何事情。所有你要做的是使用ASP.NET的内置方法

ASP.NET

To use ASP.NET, you don't do anything. All you do is you use the Built-in method of the ASP.NET as

WebSecurity.CurrentUserId;

和它会给你一个整数值。这将是当前登录的用户,对用户ID

and it would give you an integer value. Which would be the userId of the currently logged in user.

在WebSecurity如下链接状态,是一类数据。您也可以在应用程序中使用,以减少下来code,并帮助您以更好的速度得到了用户的属性。

The WebSecurity as the following link states, is a Class of data. Which can be used in your application, to lessen down the code and to help you get the User's properties at a better rate.

此类被用作变量的值,或作为一个参数。这一方法将返回一个特定的用户的属性。例如,你可以得到用户的用户ID,他的用户名,他的CREATEDATE,也可以使用 WebSecurity 来登录用户或登录他出去。

This class is used as a value for the variable, or as a parameter. This method would return a particular user's property. For example, you can get User's UserId, his UserName, his CreateDate, or you can use WebSecurity to Login a user, or Log him out.

下面将利用WebSecurity的一个例子。您只需创建的方式在jQuery的一个变量,并得到其数值。您可以使用此方法,并获得价值。

Here would be an example of using WebSecurity. Just the way you create a variable in jQuery and get its value. You use this method and get the value.

jQuery的方法

var userId = $('someElement').val(); 
/* get the value of some element as userId */

WebSecurity.CurrentUserId法

var userId = WebSecurity.CurrentUserId;

然后你可以使用它的数据库查询中,

Then you can use it inside the Database query,

db.Query("SELECT * FROM UserProfile WHERE UserId =@0", userId);

,或把它写在文件里面

or, write it inside the document

Response.Write(userId);

或你想要做什么都。你可以学习类和其他东西的语法在MSDN的链接。 : - )

Or do what ever you want to do. You can learn the syntax of the Class and other stuff in the links of MSDN. :-)

http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity(v=vs.111).aspx

现在发送Ajax请求。如果你知道,那么完美的事情!如果不是,则在这里将被发送Ajax请求的示例

Now send the Ajax request. If you know, then wonderfull! If not, then here is the example of the Ajax request to be sent

$.ajax({
  url: 'page_url',
  data: 'userId=' + userId, // userId that you got from input
  success: function (data) { // note the data parameter
    /* write in the select statement */
    $('select').html(data); // usage of data parameter
  }
});

在它完成。这将更新选择元素的选项。别急,那会是什么补充呢?

Once it is done. It would update the Select element's options. But wait, what would it add to it?

据你,谁控制它。您可以编辑服务器端code以下内容。

It you, who control it too. You edit the server-side code to the following.

如果你是一个新手,你需要先了解了一下数据库和ASP.NET。

If you're a newbie to the Databases and ASP.NET you need to first learn a bit.

http://www.asp.net/网页页/教程/数据/ 5-劳动与数据

好了,你可以了解到稍后太。我仍然会解释我所有的code给你。 : - )

Ok, you can learn that a bit later too. I will still explain all my code to you. :-)

所以,数据库,首先需要创建一个连接到数据库,然后你可以搜索其表和其他内容。这里是code

So, for Database you first need to create a Connection to the database and then you can search its tables and other content. Here is the code

var db = Database.Open("databaseName"); // Open a connection
var userId = Request.QueryString["userId"]; // get userid from ?userId=int
var selectQuery = "SELECT * FROM table_name WHERE UserId =@0"; // Query
var results = db.Query(selectQuery, userId); // Get data in a variable

让所有这些值后,所有你需要做的是建立一个响应,发送到客户端。

After getting all these values, all that you need to do is to create a Response, to be sent to the client.

我希望,你使用的网页技术。好!您只差一步比其他人在这里更安全。

I hope, you're using Web Pages technology. Good! You're one step safer than others here.

只需preSS向下翻页和向下移动到最后几行,并创建一个新的div元素

Just press Page Down and move down to the last few lines and create a new Div element

<div>
  <!--empty div element -->
</div>

现在,写在里面的,如果else语句,并创建一个将被添加到选择元素的响应。

Now, write an if else statement in it and create a Response which would be added to the select element.

PS其他方法(实际方法)给予了回应,使用的是实际工作的Htt presponse类,然后给它赋予价值。像这样

P.S Other method (Actual method) of giving out a Response, is using the Actuall HttpResponse class and then giving the values to it. Like this

Response.Write("Hello World!");

如果你写了上面的线,就成了这个字符串添加到您的select语句(虽然非法),但是这将让你了解它的用法。

If you write the above line, it would add this string to your select statement (although illegal) but that would make you understand its usage.

http://msdn.microsoft。 COM / EN-US /库/ system.web.htt presponse.aspx

这篇关于基于用户ID限制下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆