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

发布于 2024-10-20 18:07:07 字数 696 浏览 0 评论 0 原文

我的场景是这样的: 我有 asp.net 页面,其中包含产品列表的下拉控件。在数据库中,我的产品表包含 ProductId、产品名称和价格等字段。我已经通过这种方法填充了这个下拉控件:

   private void FillProducts()
            {

            List<Product> productList = objProductManager.GetProducts();
            if (productList.Count > 0)
                {
                drpProducts.DataSource = productList;
                drpProducts.DataTextField = "ProductName";
                drpProducts.DataValueField = "ProductId";

                drpProducts.DataBind();
                }
            }

哪个工作完美。但我想在客户端获得所选产品的价格值。为此,我不想在服务器上往返来获得它。除了 DataTextFeild 或 DataValueField 之外,下拉控件是否有任何属性来保存产品的价格字段?这样我就可以避免回到服务器。或者建议任何补救措施。 请指导我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2024-10-27 18:07:07

这就是全局变量会话变量发生的地方...

Web环境中,您可以同时拥有这两者,即全局变量对于跨用户(您可以缓存整个产品列表)或在仅可用于该用户会话的会话变量中。

您的方法看起来像:

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();
    }
}

您可以轻松地在提交或任何其他操作时获取价格,这样

double p = this.ProductList.Where(x => x.ID.Equals(12)).Price;

,每个会话您只需查询一次数据库,并且它可以在整个网站上使用,我的建议是您创建一个静态业务对象 <

例如,

if (MyStaticObject.ProductList.Count > 0)

如果您希望跨会话和用户获得产品列表,则可以将其作为第一行,使用 Application 变量并将其设置为 代码>global.asax 文件。


添加了

如何使用此进行客户端验证:

我喜欢 jQuery Validation,因为它是最简单且出色的客户端验证工具,非常酷,甚至 Microsoft 现在也在 ASP.NET MVC 2 以及 MVC3 中使用更多;)

您可以轻松地将值从 ASP.NET 传递到 Javascript,因为

var myJavaScriptVariable = '<%= myAspVariable %>';

相反的方式更加棘手;)

并且您可以轻松传递 JSON 并使用该 JSON 字符串,我建议 Json.NET

或者您可以使用 ASP.NET WebForms 中的内置验证...请提供您在表单(Repeater、GridView、List)中执行的操作为了给你确切的代码。


添加(第 2 部分)

将产品列表转换为 JavaScript 中的 Json 数组,您可以执行以下操作:

在 ASP.NET MVC 3

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

在 ASP.NET WebForms 中(使用JSon.NET),

var jsonWebForms = <%= Newtonsoft.Json.JsonConvert.SerializeObject(MyStaticObject.ProductList) %>;

然后您可以轻松访问所有对象,例如:

var product = jsonMVC[0];
alert(product.Price);

还有更多技术,但我希望这些有所帮助。

请记住,我仍然强烈建议您使用内置的 ASP.NET 控件验证,因为如果您使用gridview使用RowDataBound 事件来构建您的内容。

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.

寻找我们的幸福 2024-10-27 18:07:07

我更喜欢这里的 ajax 解决方案。作为 Microsoft 自己的 UpdatePanel 的替代方案,JavaScript/ajax/json 调用可将 dropbox 中选定的值发送回服务器,而无需重新加载。

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.

葮薆情 2024-10-27 18:07:07

一般来说,在任何 Web 应用程序中解决此问题的一种直接方法是在页面中包含一组 [ProductId,ProductPrice] ,独立于下拉列表,并在客户端 JavaScript 中将其与当前的 ProductId 结合使用价值。

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.

摇划花蜜的午后 2024-10-27 18:07:07

您始终可以包含第二个下拉列表,其中包含 ProductID-Price 关系,并使用 css 隐藏。

这就好像您有一个主子表关系,但有两个下拉列表。这只是一个使用 jQuery 等操作下拉菜单的问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文