GroupName and Templates


I

Iain

I have a kind of hierachical templated list class where my templates are
set for each datatype that is in the list (well, kind of tree, I suppose).

In this case my data wants to be represented by a radio button which is
contained in a template.

Of course I want all radio buttons at a given level of the tree to be in
the same group.

This fails because they are each in their own naming container.

I guess I could create a COntainer control which did not derive from
INamingContainer (so the parent would provide the prefix to the GroupName
which is what I want), however, I may want to have elements underneath the
radiobutton which should be separately named from each other (so the naming
container needs to be at the level of the button).

My remaining two options (that I've thought of so far) are.

Create a custom control which acts (more or less) like a radiobutton but
handles groupnames better (er more flexibily) and have the use put that in
the template.

Do much as above, but allow the template to contain a normal asp Readio
Button and after calling InstantiateIn, automatically replace the
RadioButton in the controls collection with a custom control with the
approapriate rendering characteristics.

Both seem a hell of a lot of work for something simple!

I was hoping for a way round this (like an event that I can use to override
the the GRoupName on rendering.

Any ideas?


Iain
 
Ad

Advertisements

I

Iain

I have a kind of hierachical templated list class where my templates are
set for each datatype that is in the list (well, kind of tree, I suppose).

In this case my data wants to be represented by a radio button which is
contained in a template.

Of course I want all radio buttons at a given level of the tree to be in
the same group.

This fails because they are each in their own naming container.
....

God LOVES a cheat ...


public class GandalfRadioButton : RadioButton
{
private string mManualGroupName;

public string ManualGroupName
{
get { return mManualGroupName; }
set { mManualGroupName = value; }
}

public override void RenderControl(HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();
HtmlTextWriter MyWriter = new HtmlTextWriter(sw);
base.RenderControl(MyWriter);
string s = sw.ToString();
int start = s.IndexOf("name")
int end = start;
int QuoteCount = 0;
while (start < s.Length && QuoteCount < 2)
{
if (s[end] == '"')
++QuoteCount;
++end;
}
writer.Write(s.Substring(0, start));
writer.Write("name=\"");
writer.Write(mManualGroupName);
writer.Write(s.Substring(end));
}
}


Iain
 
Ad

Advertisements

I

Iain

On Fri, 11 Nov 2005 17:34:36 +0000, Iain wrote:

From what I can glean RadioButton does not handle Postback data quite like
other controls. It uses the GroupName as the key (which makes sense, I
guess) and then looks at the value. So this (so far seems to work).

I should pay more attention to the return from LoadPostData, but I don't
much care about getting change events in this application.


Iain
public class GandalfRadioButton : RadioButton, IPostBackDataHandler
{
private string mManualGroupName;

public string ManualGroupName
{
get { return mManualGroupName; }
set { mManualGroupName = value; }
}

private string ReplaceNVPair(string input, string name, string
newvalue)
{
StringBuilder sb = new StringBuilder(input.Length + 50);
int start = input.IndexOf(name, 0,
StringComparison.OrdinalIgnoreCase);
int end = start;
int QuoteCount = 0;
while (start < input.Length && QuoteCount < 2)
{
if (input[end] == '"')
++QuoteCount;
++end;
}
sb.Append(input.Substring(0, start));
sb.Append(name);
sb.Append("=\"");
sb.Append(newvalue);
sb.Append(input.Substring(end - 1));
return sb.ToString();

}
public override void RenderControl(HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();
HtmlTextWriter MyWriter = new HtmlTextWriter(sw);
base.RenderControl(MyWriter);
string s = sw.ToString();
s = ReplaceNVPair(s, "name", mManualGroupName);
s = ReplaceNVPair(s, "value", ClientID);
writer.Write(s);
}

#region IPostBackDataHandler Members

bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
string s = postCollection[mManualGroupName];
if (s == ClientID)
Checked = true;
else
Checked = false;
return false;
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
base.RaisePostDataChangedEvent();
}

#endregion
}
 

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

Top