Problems using GetAttribute and SetAttribute of the IAttributeAccessor interface with web controls

D

duff

Hiya,

I'm trying to replace the commented out code below with a neater
version using the IAttributeAccessor interface (so that I don't have to
write 'if' statements for all control types).

At the moment this doen't work. The test line I put in

(string s = ControlAttributes.string s = ControlAttributes.GetAttribute
("Text");

is giving me s as null. Neither the GetAttribute nor the SetAttrubute
fail - they just don't seem to do anything.

Any clues what I'm doing wrong?

Kirsty


___________________________________

public void SetCaptions
(
System.Web.UI.HtmlControls.HtmlForm WebForm,
System.Data.DataSet Captions
)
{
System.Data.DataTable CaptionDataTable = new System.Data.DataTable();
string ControlName = "";
string ControlText = "";
System.Web.UI.Control Control;

// Get the captions data table from the dataset
CaptionDataTable = Captions.Tables[_CaptionDataTableName];

// Go through the data, setting the Text property of each control
foreach (System.Data.DataRow Row in CaptionDataTable.Rows)
{
ControlName = Row[_ControlNameFieldName].ToString();
ControlText = Row[_ControlCaptionFieldName].ToString();
Control = WebForm.FindControl (ControlName);

if (!(Control == null))
{
System.Web.UI.IAttributeAccessor ControlAttributes =
(System.Web.UI.IAttributeAccessor)Control;
string s = ControlAttributes.GetAttribute ("Text");
ControlAttributes.SetAttribute ("Text",ControlText);


// if (Control is System.Web.UI.WebControls.Label)
// {
// System.Web.UI.WebControls.Label Lbl =
(System.Web.UI.WebControls.Label)Control;
// Lbl.Text = ControlText;
// } // (ControlType is System.Web.UI.WebControls.Label)

} //(!(Control ==null))
} //foreach

} //SetCaptions
 
W

William F. Robertson, Jr.

From the help page for IAttributeAccessor

http://msdn.microsoft.com/library/d...rfsystemwebuiiattributeaccessorclasstopic.asp

"Defines methods used by ASP.NET server controls to provide programmatic
access to any attribute declared in the opening tag of a server control."

If you were too look at your label control that was rendered to the browser
you would probably see

<span Text=""></span>

When you are calling the GetAttribute( "Text" ) there is not a client side
text attribute defined yet.

To accomplish this you will need to use reflection.

Control = WebForm.FindControl (ControlName);

if ( Control != null ) {
Type t = Control.GetType();
System.Reflection.PropertyInfo info = t.GetProperty( "Text" ); //gets the
property called Text if it exists on the control.

if ( info != null ) //this will actually set the Text property of all
controls that have one
info.SetValue( Control, LabelText, null );
}

You are confusing client side attributes with class properties.

HTH,

bil

Hiya,

I'm trying to replace the commented out code below with a neater
version using the IAttributeAccessor interface (so that I don't have to
write 'if' statements for all control types).

At the moment this doen't work. The test line I put in

(string s = ControlAttributes.string s = ControlAttributes.GetAttribute
("Text");

is giving me s as null. Neither the GetAttribute nor the SetAttrubute
fail - they just don't seem to do anything.

Any clues what I'm doing wrong?

Kirsty


___________________________________

public void SetCaptions
(
System.Web.UI.HtmlControls.HtmlForm WebForm,
System.Data.DataSet Captions
)
{
System.Data.DataTable CaptionDataTable = new System.Data.DataTable();
string ControlName = "";
string ControlText = "";
System.Web.UI.Control Control;

// Get the captions data table from the dataset
CaptionDataTable = Captions.Tables[_CaptionDataTableName];

// Go through the data, setting the Text property of each control
foreach (System.Data.DataRow Row in CaptionDataTable.Rows)
{
ControlName = Row[_ControlNameFieldName].ToString();
ControlText = Row[_ControlCaptionFieldName].ToString();
Control = WebForm.FindControl (ControlName);

if (!(Control == null))
{
System.Web.UI.IAttributeAccessor ControlAttributes =
(System.Web.UI.IAttributeAccessor)Control;
string s = ControlAttributes.GetAttribute ("Text");
ControlAttributes.SetAttribute ("Text",ControlText);


// if (Control is System.Web.UI.WebControls.Label)
// {
// System.Web.UI.WebControls.Label Lbl =
(System.Web.UI.WebControls.Label)Control;
// Lbl.Text = ControlText;
// } // (ControlType is System.Web.UI.WebControls.Label)

} //(!(Control ==null))
} //foreach

} //SetCaptions
 
B

Bruce Barker

thats because the Text property of a Label is not an attribute. if you look
at the rendered html, you should see something like:

<span Text="valueOf Controltext" id="labelname"></span>

where your code added the Text attribute (meanless to the browser)

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top