将数据从 ASP.NET 页面传递到动态加载的 ASCX 用户控件

发布于 2024-08-21 00:05:16 字数 283 浏览 3 评论 0原文

我正在使用 C# 和 Ajax 开发 ASP.NET 应用程序。

我有一个页面,其中包含动态加载的用户控件。我需要将一些数据(整数值和一些字符串)传递给已动态加载的用户控件。

现在我使用Session来传递这些值,但我想我可以使用另一种方式;像 VIEWSTATE 或隐藏输入之类的东西。

你给我推荐什么?

更新:

动态加载控件这一事实很重要,因为控件会在每次回发时加载,并且我无法在控件上存储任何值。

I'm developing an ASP.NET application with C# and Ajax.

I have a page that holds user controls loaded dynamically. I need to pass some data (integer values and some strings) to the user control that has been loaded dynamically.

Now I use Session to pass these values, but I think I can use another way; something like VIEWSTATE or hidden input.

What do you recommend me?

UPDATE:

The fact that I load the controls dynamically is important because controls are loaded on every postback, and I can't store any value on controls.

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

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

发布评论

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

评论(6

与之呼应 2024-08-28 00:05:16

使用要传递给它的数据的数据类型在用户控件上创建一个属性,并在创建控件时将其填充到页面中。

public class myControl : Control
{
  ...
  public int myIntValue {get; set;}
  ...
}

在后面的代码中:

myControl ctrl = new myControl();
ctrl.myIntValue = 5;

您也可以直接在标记中执行此操作:

<uc1:myControl ID="uc1" runat="server" myIntValue="5" />

Create a property on your user control with the datatype of the data you want to pass to it, and populate it in your page on creation of the control.

public class myControl : Control
{
  ...
  public int myIntValue {get; set;}
  ...
}

In the code behind:

myControl ctrl = new myControl();
ctrl.myIntValue = 5;

You can also do this directly in markup:

<uc1:myControl ID="uc1" runat="server" myIntValue="5" />
一张白纸 2024-08-28 00:05:16

在用户控件中设置公共属性。

public string TestValue { get;set;};

然后,当您将用户控件放入 aspx 页面中时:

<uc1:UserControl ID="uc1" runat="server" TestValue="Testing" />

您还可以更改后面代码中的值:

uc1.TestValue = "some value";

Setup public properties within your user control.

public string TestValue { get;set;};

And then when you put your user control in your aspx page:

<uc1:UserControl ID="uc1" runat="server" TestValue="Testing" />

You can also change the values within your code behind:

uc1.TestValue = "some value";
往日情怀 2024-08-28 00:05:16

要真正回答你的问题,就像其他人似乎都不希望你这样做一样,我同意......我以前做过这种事情。

我要做的第一件事是让您的页面实现一个接口。

在控件中:

IVansFannelDataProviderPage provider = this.Page as IVansFannelDataProviderPage;
if (provider != null) { //从接口获取数据 } else throw YouCantPutThisControlOnThisKindOfPageException();

这不是最优雅的方法,但是当我们有很多控件想要共享一个非常昂贵的对象时这符合要求。

这种方法效果很好,但它会使您的控件在未实现该接口的页面上无法使用,从而使控件与您的页面耦合过于紧密。其他人都说让页面从控件获取数据是正确的;您将控件放在页面上,而不是控件中的页面。

您应该有一个很好的理由来这样做。对于我们来说:共享对象的加载非常昂贵,并且无论哪个控件正在该对象上工作,页面加载/保存它都非常有用。

遗憾的是,许多没有真正实现该接口的页面必须硬塞以提供某种支持或代理支持,只是为了让控件正常工作,并使页面和控件的可重用性大大降低。

如果我必须再次执行此操作,我会让页面通过事件将数据发送到控件,如果我需要偷懒的话,可能会通过反射。

To actually answer your quesiton, as much as everyone else seems to not want you to do this, and I agree...I have done this sort of thing before.

The first thing I'd do is make your page implement an interface.

In the control:

IVansFannelDataProviderPage provider = this.Page as IVansFannelDataProviderPage;
if (provider != null) { //grab data from interface } else throw YouCantPutThisControlOnThisKindOfPageException();

It's not the most elegant way to do it, but when we had a lot of controls wanting to share a very expensive object this fit the bill.

This works well, but it makes your control unusable on pages that don't implement the interface--and that makes the controls too tightly coupled to your page. Everyone else saying to have the page get data from the controls is correct; you put controls on the page, not pages in controls.

You should have a very good reason in order to do it. For us: the load of the shared object was very expensive, and having the page load/save it no matter what control was working on that object was quite useful.

It was too bad that a lot of pages that didn't really implement the interface had to be shoehorned to provide some sort of support or proxy support just to get the controls to work, and made the pages and controls that much less reusable.

If I had to do this over again, I'd have the page send data to the controls with events, probably through reflection if I needed to be lazy.

薔薇婲 2024-08-28 00:05:16

您必须在每次回发时重新加载这些控件...请阅读本文。这可能有帮助。

动态 Web 控件、回发、和查看状态

You'll have to re-load those controls on each post-back... Give this a read. It might help.

Dynamic Web Controls, Postbacks, and View State

仅此而已 2024-08-28 00:05:16

您可以在 HttpContext.Items 集合中设置值并在控件中读取它们。这与使用 Session 类似,只不过它仅针对每个请求可用,而不是在整个会话生命周期内可用。

https://web. archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx

恕我直言,这有点懒,但在某些情况下可能是一个很好的解决方案。

You could set values in the HttpContext.Items collection and read them in your controls. This is like using Session except that it's only available per request not for the whole session lifetime.

https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx

IMHO this is a bit lazy but it might be a good solution in some situations.

请爱~陌生人 2024-08-28 00:05:16

在我看来,这有点违背了使用 ascx 控件的目的,因为这破坏了控件封装。您的页面应该通过订阅控件发布的事件来从控件获取数据。

Kind of defeats the purpose of using an ascx control IMO since this breaks control encapsulation. Your page should be -getting- data from the control through subscribing to events published by the control.

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