1

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.

2
  • You can replace ProductId with product Price and get the dropdownlist's SelectedValue in javascript Commented Mar 9, 2011 at 8:24
  • very funny....! and if i would work with product id in fact product id and name are in use in code behind. Commented Mar 9, 2011 at 8:57

4 Answers 4

2

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 gridviewuse the RowDataBound Event to build up your stuff.

Sign up to request clarification or add additional context in comments.

6 Comments

I really impressed , this is good solution i think. but I am useing this price for validation purpose here. like there are another text boxes.as "Paid amount" and "Number Of products". here i want to check that filled amount should exactly equals to price of multiplies of number of product entered.suppose a pen is of 10$ and number of pen will 4 then he must fill there 40$ . like this scenario. I know I can do thin in custom validation by java script . but again question would be how to access the application variable / Session variables in javascript ? are you getting my point?
added client validation idea as I don't know what are you doing.
forget about it, tell me how can i access Session variable that holding the Product list in javascript?
added how to create a JSON object of your Product List.
somehow i am unable to access the Session value in my javascript getting ')' expected error. on var productPrice= '<%= Session("ProductPrice"); %>' can you tell me why should this ?
|
1

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.

2 Comments

ya...i think this is one of the best way again.
What? one more call to do what you already have available? Nahhhh!!
0

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.

1 Comment

Do you mean to say I have to maintain the product price at application level in array form so that I can pick selected product Id 's price ? but how if i added new product then ?
0

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

3 Comments

i don't think so I have to maintain another control which will make my page weighted to load. performance will be issue i think in this scenario . isn't it?
It will most certainly not make your page any lighter, that's true, but that might be true of most of the possible solutions. At the end of the day it will mean holding an extra list of values in your page... How about a gol ol'Ajax request?
Frankly telling you this would wrong approach to fix the aim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.