Append to Existing Attribute Item

  • Thread starter Thread starter Eric Lemmon
  • Start date Start date
E

Eric Lemmon

Hello C# Gurus,

Please excuse my VB mindset.

I am writing a class library in C# that is used by a VB ASP.NET app. When SQL Server detects a data validation exception, I want to use a C# function within the library to alert the user and send focus to the offending control.

In VB.NET, I can code something like this (where I just put a runat=server onto the <BODY> tag and named it BodyTag) :

DirectCast(Page.FindControl("BodyTag"), _
HtmlGenericControl).Attributes. _
Item("onload") &= "alert('Data Invalid');"

Is there an equivalent statement for C# that can reference a control attribute by name? Whenever I try to reference an item in the Attributes collection, it barks at me because item does not exist. Trying to use the Keys collection does not work either because it is not indexed.

I would just use the Attributes.Add method, but I cannot overwrite the existing onload attribute; I can only append to it.

Many thanks,

Eric
 
Hi Eric,

This would be the equivalent of your VB code rendered in C#:

(HtmlGenericControl)(Page.FindControl("BodyTag")).Attributes["onload"] += "alert('Data Invalid');";

Regards,
Daniel
Hello C# Gurus,

Please excuse my VB mindset.

I am writing a class library in C# that is used by a VB ASP.NET app. When SQL Server detects a data validation exception, I want to use a C# function within the library to alert the user and send focus to the offending control.

In VB.NET, I can code something like this (where I just put a runat=server onto the <BODY> tag and named it BodyTag) :

DirectCast(Page.FindControl("BodyTag"), _
HtmlGenericControl).Attributes. _
Item("onload") &= "alert('Data Invalid');"

Is there an equivalent statement for C# that can reference a control attribute by name? Whenever I try to reference an item in the Attributes collection, it barks at me because item does not exist. Trying to use the Keys collection does not work either because it is not indexed.

I would just use the Attributes.Add method, but I cannot overwrite the existing onload attribute; I can only append to it.

Many thanks,

Eric
 
Thank you so much, Daniel.

With the exception of an extra pair of parentheses, your code worked perfectly. (The extra parens were needed to limit the cast to (Page.FindControl("BodyTag")), as opposed to the whole line.)

Here is the statement that worked:
((HtmlGenericControl)(Page.FindControl("BodyTag"))).Attributes["onload"] += "alert('Data Invalid');";

Thanks again!

Eric





Hi Eric,

This would be the equivalent of your VB code rendered in C#:

(HtmlGenericControl)(Page.FindControl("BodyTag")).Attributes["onload"] += "alert('Data Invalid');";

Regards,
Daniel
Hello C# Gurus,

Please excuse my VB mindset.

I am writing a class library in C# that is used by a VB ASP.NET app. When SQL Server detects a data validation exception, I want to use a C# function within the library to alert the user and send focus to the offending control.

In VB.NET, I can code something like this (where I just put a runat=server onto the <BODY> tag and named it BodyTag) :

DirectCast(Page.FindControl("BodyTag"), _
HtmlGenericControl).Attributes. _
Item("onload") &= "alert('Data Invalid');"

Is there an equivalent statement for C# that can reference a control attribute by name? Whenever I try to reference an item in the Attributes collection, it barks at me because item does not exist. Trying to use the Keys collection does not work either because it is not indexed.

I would just use the Attributes.Add method, but I cannot overwrite the existing onload attribute; I can only append to it.

Many thanks,

Eric
 
Back
Top