0

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? For instance, imagine that I have a LinkButton:

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:

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.

Thanks.

Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44

1 Answers1

0

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 :

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 :

HttpContext.Current.Session.Add(link.ID + "-param1", "Value0");
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • Actually I didn't say I don't want these "metadata" in markup, I was just wondering if it was a good idea, and apparently there is some discussion about the subject [here](http://stackoverflow.com/questions/1305734/is-it-ok-to-add-your-own-attributes-to-html-elements). Thanks for you answer though, I'll give it a try. – Thomas C. G. de Vilhena Apr 29 '12 at 20:26