如何将数据传递给用户控件? [英] How to pass data to user controls?

查看:66
本文介绍了如何将数据传递给用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页上有很多用户控件。我怎样才能将一些

数据传递给它?我没有看到在

页面代码隐藏页面中实例化用户控件的位置。


谢谢。

解决方案

>我在网页上有很多用户控件。如何将一些

数据传递给它?我没有看到用户控件在
页面代码隐藏页面中实例化的位置。




我是.NET的新手,但是你问如何通过它的父aspx页面通过

查询字符串传递数据?如果是这样,我可以帮助

...


-Darrel


Do你的意思是在构造函数中传递信息?你不能因为ASP.NET为你实例化控件并用一些

代码隐藏变量绑定标签。


但是,您可以使用属性和方法在创建

之后传递信息,可能在Page_Load事件中。或者你可以在

页面上设置属性,这些属性被转换成ASP.NET设置的属性调用。


你至少应该有一个受保护的变量代码隐藏是

命名与页面上的标记相同。


" Frank Rizzo" <无**** @ nospam.com>在消息中写道

新闻:#s ************** @ TK2MSFTNGP11.phx.gbl ...

我已经在网页上获得了许多用户控件。我怎样才能将一些
数据传递给它?我没有看到在页面代码隐藏页面中实例化用户控件的位置。

谢谢。



Peter Rilling写道:

你的意思是在构造函数中传递信息?你不能因为ASP.NET为你实例化控件并将标签与一些代码隐藏变量绑定在一起。


通过构造函数或通过属性。问题是我放在页面上的

UserControl没有像按钮控件那样在

代码隐藏文件中实例化。所以我不确定

如何引用它。


例如,UserControl被称为AddComments,我的网页是

main.aspx.cs


我无法在main.aspx.cs中找到AddComments实例化的地方。

我可以实例化它手动像


AddComments oComment = new AddComments(...)

但ASP.NET似乎没有考虑到我做的任何事情

UserControl。


我想,我需要引用UC的实例,即

ASP.NET使用。

但是,您可以使用属性和方法在创建信息之后传递信息,也可以在Page_Load事件中传递信息。或者,您可以在
页面上设置属性,这些属性将转换为ASP.NET设置的属性调用。


你能给我一个这样的例子吗?

你的代码隐藏中至少应该有一个受保护的变量,它名为
与页面上的标签相同。

Frank Rizzo <无**** @ nospam.com>在消息中写道
新闻:#s ************** @ TK2MSFTNGP11.phx.gbl ...

我已经在网页上获得了许多用户控件。我怎样才能将一些
数据传递给它?我没有看到在页面代码隐藏页面中实例化用户控件的位置。

谢谢。




I''ve got a number of user controls on the web page. How can I pass some
data to it? I don''t see where the user control is instantiated in the
page code-behind page.

Thanks.

解决方案

> I''ve got a number of user controls on the web page. How can I pass some

data to it? I don''t see where the user control is instantiated in the
page code-behind page.



I''m a complete newbie to .NET, but are you asking how to pass data via a
Query string of via it''s parent aspx page? If so, I can maybe help with
that...

-Darrel


Do you mean passing information in the constructor? You cannot since
ASP.NET instantiates the control for you and binds the tag with some
code-behind variable.

You can however, use properties and methods to pass information in after it
is created, maybe in the Page_Load event. Or you can set attributes on the
page that get translated into property calls that ASP.NET sets.

You should at least have a protected variable in your code-behind that is
named that same as the tag on the page.

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...

I''ve got a number of user controls on the web page. How can I pass some
data to it? I don''t see where the user control is instantiated in the
page code-behind page.

Thanks.



Peter Rilling wrote:

Do you mean passing information in the constructor? You cannot since
ASP.NET instantiates the control for you and binds the tag with some
code-behind variable.
Either via the constructor or via properties. The problem is that the
UserControl that I placed on the page is not instantiated in the
code-behind file like a button control would be. So I am not sure about
how to even reference it.

For instance, the UserControl is called AddComments and my web page is
main.aspx.cs

I can''t find anywhere in main.aspx.cs where AddComments is instantiated.
I can instantiate it manually like

AddComments oComment = new AddComments(...)
But ASP.NET doesn''t seem to take into account anything I do with the
UserControl.

I guess, I need to have a reference to the instance of the UC that''s in
use by ASP.NET.
You can however, use properties and methods to pass information in after it
is created, maybe in the Page_Load event. Or you can set attributes on the
page that get translated into property calls that ASP.NET sets.
Could you give me an example of this?

You should at least have a protected variable in your code-behind that is
named that same as the tag on the page.

"Frank Rizzo" <no****@nospam.com> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...

I''ve got a number of user controls on the web page. How can I pass some
data to it? I don''t see where the user control is instantiated in the
page code-behind page.

Thanks.




这篇关于如何将数据传递给用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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