If this is an example of a database table and an example of a Select element. Then I think that 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 move on to coding. I will try to explain what I write and how I write; since you are new :-)
HTML code for you
HTML code for your work would be simple, like this
<select name="someName"> <!-- No options please! --> </select>
Getting UserId
Now, as soon as the user is logged in. Try to get UserId in any of the following ways.
JQuery
To use jQuery, you will need to use the value generated by the server, since jQuery cannot interfere with server requests and code. Thus, you can create a simple hidden input and assign it the value of the current registered user. Here is an example
<input type="hidden" id="userId" value="@WebSecurity.CurrentUserId" />
Now, using jQuery, you can get the value for this input. There will be code for this
var userId = $('#userId').val();
ASP.NET
To use ASP.NET, you are not doing anything. All you do is use the built-in ASP.NET method as
WebSecurity.CurrentUserId;
and it will give you an integer value. What will be the userId of the current user.
Using WebSecurity
WebSecurity, as indicated in the following link, is a data class. Which can be used in your application to reduce code and help you get user properties faster.
This class is used as a value for a variable or as a parameter. This method returns a specific property of the user. For example, you can get the User UserId, its UserName, your CreateDate, or you can use WebSecurity to log in to the user or log him out of the system.
Here is an example of using WebSecurity. Just create a variable in jQuery and get its value. You use this method and get the value.
JQuery Method
var userId = $('someElement').val(); /* get the value of some element as userId */
WebSecurity.CurrentUserId Method
var userId = WebSecurity.CurrentUserId;
Then you can use it in your database query,
db.Query("SELECT * FROM UserProfile WHERE UserId =@0", userId);
or, write it inside the document
Response.Write(userId);
Or do what you want to do. You can find out the syntax of the Class and other materials in the MSDN links. :-)
http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity(v = vs .111) .aspx
Submit Ajax Request
Now send your Ajax request. If you know, then great! If not, here is an example of an 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 } });
As soon as this is done. It will update the "Select Item" options. But wait, what would he add to this?
It's you who controls it. You are editing the server side code as follows.
Retrieving data from a database.
If you are new to Databases and ASP.NET, you need to learn a little first.
http://www.asp.net/web-pages/tutorials/data/5-working-with-data
Well, you can find out about this a bit later. I’ll still explain to you all my code. :-)
So, for a database, you first need to create a database connection, and then you can search for 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 receiving all these values, all you have to do is create a response that will be sent to the client.
Hope you are using web page technology. Good! You are one step safer than others.
Just click “Down” and go down to the last few lines and create a new div element
<div> <!--empty div element --> </div>
Now write an if else statement in it and create a response that will be added to the select element.
PS Another method (Actual method) for issuing a response uses the Actuall HttpResponse class and then gives it values. Like this
Response.Write("Hello World!");
If you write the line above, it will add that line to your select statement ( albeit illegal ), but it will help you understand its use.
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.aspx