简体   繁体   English

将元数据分配给asp.net控件

[英]Assign metadata to an asp.net control

I would like to know if it is possible to assign "metadata" to an asp.net control that could be retrieved later in a post back? 我想知道是否可以为“ asp.net”控件分配“元数据”,以便稍后在发回信息时进行检索? For instance, imagine that I have a LinkButton: 例如,假设我有一个LinkBut​​ton:

LinkButton link = new LinkButton();
link.ID = "LinkButton_0";
link.Text = "Click here for more info";
link.Click += new EventHandler(link_Click);

Now I want to assign more data to the control so that when the post back event occurs I can retrieve this data to find out what to do. 现在,我想为控件分配更多数据,以便在发生回发事件时,我可以检索此数据以找出要做什么。 I thought about using the "Attributes" property but I guess it is not a good solution because it may affect the control's html layout: 我考虑过使用“ Attributes”属性,但我猜它不是一个好的解决方案,因为它可能会影响控件的html布局:

link.Attributes.Add("param0", "value0");
link.Attributes.Add("param1", "value1");
link.Attributes.Add("param2", "value2");

I need to do this because my page has plenty of link buttons that are created dynamically and all of them raise the same post back event. 我需要这样做是因为我的页面上有许多动态创建的链接按钮,并且所有这些按钮都引发相同的回发事件。

Some people suggested I simply concatenate all "metadata" in the control's ID, but I'm trying to find a better solution. 有人建议我只是将控件ID中的所有“元数据”串联起来,但我正在尝试寻找更好的解决方案。

Thanks. 谢谢。

Since you don't want these metadata in markup I presume that you don't need it on client side, so you can put your meta data in Session, and name session item like control ID + paramID, something like this : 由于您不想在标记中使用这些元数据,因此我假设您不需要在客户端使用它们,因此您可以将元数据放在Session中,并为会话项命名,例如控件ID + paramID,如下所示:

this.Session.Add(link.ID + "-param1", "Value0");

btw. 顺便说一句 this is a example if you do that from Page methods, if you don't have page instance you can access session like that : 这是一个示例,如果您通过Page方法执行此操作,则如果没有页面实例,则可以访问以下会话:

HttpContext.Current.Session.Add(link.ID + "-param1", "Value0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM