将数据从 ASP.NET 页面传递到动态加载的 ASCX 用户控件 [英] Pass data from a ASP.NET page to ASCX user controls loaded dynamically

查看:29
本文介绍了将数据从 ASP.NET 页面传递到动态加载的 ASCX 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

你推荐我什么?

更新:

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

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.

推荐答案

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

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;}
  ...
}

在后面的代码中:

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

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

You can also do this directly in markup:

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

这篇关于将数据从 ASP.NET 页面传递到动态加载的 ASCX 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆