简体   繁体   English

在这种情况下如何从asp.net的下拉控件中获取价值?

[英]how to get value from dropdown control in asp.net in this scenario?

My scenario is this: I have asp.net page which having the drop down control with Product list. 我的情况是这样的:我有asp.net页面,该页面具有“产品”列表的下拉控件。 in database my Product Table has fields as ProductId, Product Name and Price. 在数据库中,我的产品表的字段为ProductId,Product Name和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 ? 除DataTextFeild或DataValueField以外,下拉控件的任何属性都可以保存产品的价格字段吗? 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. Web环境中,您可以同时具有 ,用于跨用户的全局变量 (可以在其中缓存整个产品列表)或在只能用于该用户会话的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. 如果要在会话和用户之间拥有“产品”列表,请使用Application变量并将其设置在global.asax文件中。


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 ;) 我喜欢jQuery Validation,因为它是最简单,最出色的客户端验证工具,太酷了,甚至Microsoft现在都在ASP.NET MVC 2和MVC3中使用它;)

you can easily pass values from ASP.NET into Javascript as 您可以轻松地将值从ASP.NET传递给Javascript,如下所示:

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 并且您可以轻松地传递JSON并使用该JSON字符串,我建议使用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. 或者,您也可以在ASP.NET WebForms中使用内置的验证...请提供您在表单(Repeater,GridView,List)中正在执行的操作,以便为您提供准确的代码。


added (part 2) 添加 (第2部分)

to convert your Product List into a Json Array in javascript, you can do this: 要将您的产品列表转换为javascript中的Json数组,您可以执行以下操作:

in ASP.NET MVC 3 在ASP.NET MVC 3中

var jsonMVC = @Html.Raw(Json.Encode(reg));

in ASP.NET WebForms (using JSon.NET) 在ASP.NET WebForms中 (使用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. 记住,我仍然强烈建议您使用内置的ASP.NET控件验证,因为如果使用gridview使用RowDataBound事件来构建您的东西,因为开箱即用。

I would prefer an ajax solution here. 我在这里更喜欢ajax解决方案。 Alternatively to Microsoft own UpdatePanel, an javascript/ajax/json call that sends the selected value in dropbox back to the server without reloading. 除了Microsoft自己的UpdatePanel之外,还可以使用javascript / ajax / json调用,将保管箱中的选定值发送回服务器而无需重新加载。

一般而言,在任何Web应用程序中解决此问题的一种直接方法是在页面中包含[ProductId,ProductPrice]数组,与下拉列表无关,并在客户端JavaScript中与您当前的ProductId结合使用值。

You could always include a second dropdown which contains the ProductID-Price relation and hide is using css. 您始终可以包括第二个下拉列表,其中包含ProductID-Price关系,并且hide使用的是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 这只是使用jQuery处理下拉菜单的问题

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

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