从 Web 用户控件获取控制值

发布于 2024-09-27 07:18:35 字数 1014 浏览 2 评论 0原文

假设我的 Web 用户控件中有三个 DropDownList 控件,并且它们相互依赖。

  • 类别
  • 品牌
  • 产品

说明: 当我从类别下拉列表中选择一个类别后,相关品牌会加载到品牌 DropDownList 中,当我选择特定品牌时也会发生同样的情况,并且它们都位于 Web 用户控件中,因为我在不同的页面,我不想在所有页面上复制并粘贴相同的代码。

问题:页面可能包含 GridViewDataSource 控件,它们需要额外的 Where 参数来获取所有数据需要,并且该参数可能取决于 Products DropDownList 控件中所选的产品。

问题:那么我怎样才能从产品 DropDownList 中获取选定的产品值并将其绑定到 SQLDataSource 或任何其他 DataSource 控制。

我的想法:我相信我可以通过以下方式解决这个问题。

  • 我可以使用静态变量,一旦选择产品,该变量就会更新。该字段变量可以是公共的,这样每个人都可以访问它。
  • Selected Products DropDownList 可以为我创建一个 QueryString 字段来捕获所选值。
  • 以同样的方式,下拉列表可以动态创建一个会话变量,我可以获取该值
  • 它也许可以创建一个隐藏字段。

但是:嗯,这些是我的一些想法,但我发现它们实施起来太天真了。我需要一些优雅且令人满意的东西来解决这个问题。它应该类似于从 Web 用户控件到外部世界的网关。

也许一个单独的类或属性可以帮助我解决网关问题。

无论如何,我正在等待您的答复。

Lets say I have three DropDownList controls in a web user control and they depend on each other.

  • Categories
  • Brands
  • Products

Explanation:
After I choose a category from Categories dropdown list, related brands are loaded in Brands DropDownList and same happens when I choose specific brand and they are all located in a web user control since I am using it too much on different pages, I don't want to copy and paste the same code on all the pages.

Problem: The pages can contain a GridView and DataSource control which needs an additional Where parameter to fetch all the data needed in and that parameter could depend on selected product within the Products DropDownList control.

Question: So how can I get that Selected Product Value from Products DropDownList to bind it to SQLDataSource or any other DataSource control.

My Thoughts: I belive I can solve this problem in the ways following.

  • I can use static variable which is updated once Products selected. That field variable could be public so everyone can reach it
  • Selected Products DropDownList can create a QueryString Field for me to grap the selected value.
  • In the same way, the dropdownlist can create a Session variable on the fly and I can fetch the value
  • It can create a hidden field maybe.

But: Well those are some of my thoughts but I found them so naive to implement. I need something elegant and satisfying to solve this problem. It should be something like a gateway from the Web User Control to outside world.

Maybe a separate class or a Property can help me in the gateway solution.

Anyways, I am waiting for your answers.

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

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

发布评论

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

评论(2

_畞蕅 2024-10-04 07:18:36

您可以将 DropDownList.SelectedIndexChanged 事件绑定到同一函数,并测试每个 DropDownList 的 SelectedValue 属性。如果它们的 SelectedValues 有效,则将网格绑定到您的数据源。

过去,当我需要用户输入一定量的数据才能查询数据库之前,我就这样做过。如果 DropDownLists 无效,我会在 GridView 上设置 Hidden 属性,并在正确绑定时重置它。

You could bind the DropDownList.SelectedIndexChanged events to the same function, and test the SelectedValue properties of each DropDownList. If their SelectedValues are valid, bind the grid to your DataSource.

I've done this in the past when I needed users to input a certain amount of data before I could query the database. I set the Hidden property on my GridView if the DropDownLists weren't valid, and reset it when they were properly bound.

抚笙 2024-10-04 07:18:35

如果我正确理解了问题:

您可以向用户控件添加一个属性,以公开产品 DDL 选择的值。

您还可以从用户控件添加并引发一个事件,该事件在产品 DDL 选择的值更改时触发。创建包含产品值的自定义事件参数,允许将其直接传递到事件处理程序。

然后您的页面可以处理用户控件引发的事件,具有产品值并绑定网格。

If I'm understanding the question correctly:

You can add a property to the user control that exposes the products DDL selected value.

You can also add and raise an event from the user control that fires when the products DDL selected value changes. Creating a custom event argument that contains the product value, allows it to get passed directly to the event handler.

Then your pages can handle the event raised by the user control, have the product value, and bind the grid.

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