My scenario is this: I have asp.net page which having the drop down control with Product list. in database my Product Table has fields as ProductId, Product Name and Price. where as I have filled this drop down control by this method:
private void FillProducts()
{
List<Product> productList = objProductManager.GetProducts();
if (productList.Count > 0)
{
drpProducts.DataSource = productList;
开发者_运维问答 drpProducts.DataTextField = "ProductName";
drpProducts.DataValueField = "ProductId";
drpProducts.DataBind();
}
}
Which is working perfect. But i want to get selected product Price value on client side. for that I don't want round trip at server to get that. is any property of dropdown control other than DataTextFeild Or DataValueField to hold the Price Field of product ? so that i would avoid to go back at server. Or suggest any remedy for the same. Please guide me.
That's where Global variables and Session Variables takes place...
In a Web Environment you can have both, a Global Variable for cross users (where you can have the entire product list cached) or in a Session variable that you can use for only that user session.
You method would look like:
public MyPMan objProductManager = new MyPMan();
public List<Product> ProductList
{
get
{
if(Session["MyApp-ProductList"] == null)
Session["MyApp-ProductList"] = objProductManager.GetProducts();
return (List<Product>)Session["MyApp-ProductList"];
}
}
private void FillProducts()
{
//List<Product> productList = objProductManager.GetProducts();
if (this.ProductList.Count > 0)
{
drpProducts.DataSource = this.ProductList;
drpProducts.DataTextField = "ProductName";
drpProducts.DataValueField = "ProductId";
drpProducts.DataBind();
}
}
and you can easily, on submit or anything, just grab the price as
double p = this.ProductList.Where(x => x.ID.Equals(12)).Price;
This way, you only query the DB once per session, and it's available across the website, my suggestion is that you create a Static Business Object to be available every where and simply :)
then for example, you would do this as the first line
if (MyStaticObject.ProductList.Count > 0)
if you want to have the Product list across sessions and users, use the Application
variable and set that in global.asax
file.
added
How to do client validation using this:
I love jQuery Validation as it's the easiest and a fantastic client validation tool, so cool that even Microsoft now uses on ASP.NET MVC 2 and more in MVC3 ;)
you can easily pass values from ASP.NET into Javascript as
var myJavaScriptVariable = '<%= myAspVariable %>';
the other way around is more tricky ;)
and you can easily pass a JSON and use that JSON string, I would suggest Json.NET
Or you can use the built-in validation in ASP.NET WebForms... please provide what are you doing in your form (Repeater, GridView, List) in order to give you the exact code.
added (part 2)
to convert your Product List into a Json Array in javascript, you can do this:
in ASP.NET MVC 3
var jsonMVC = @Html.Raw(Json.Encode(reg));
in ASP.NET WebForms (using JSon.NET)
var jsonWebForms = <%= Newtonsoft.Json.JsonConvert.SerializeObject(MyStaticObject.ProductList) %>;
and then you can easily access all your object, for example:
var product = jsonMVC[0];
alert(product.Price);
There are more techniques, but I hope this ones helps.
Remember, I still strongly suggest that you use the built-in ASP.NET Control Validation, as you will have that out of the box, if you using a gridview
use the RowDataBound
Event to build up your stuff.
I would prefer an ajax solution here. Alternatively to Microsoft own UpdatePanel, an javascript/ajax/json call that sends the selected value in dropbox back to the server without reloading.
Speaking generically, one straightforward way to approach this in any web application would be to include an array of [ProductId,ProductPrice] with the page, independently from the dropdown, and use that in client-side JavaScript in conjunction with your 's current ProductId value.
You could always include a second dropdown which contains the ProductID-Price relation and hide is using css.
It would be as though you have a master-child table relation but with two dropdowns. The it's only a question of manipulating the dropdowns with, say, jQuery
精彩评论