So, I'm trying to dynamically add a button to a site to give the user of an idea of what they are adding to a form. However, when it renders a new button, it keeps providing the value property as "" and ignoring the attribute that I add. I've attempted to fix this to no avail, including removing the value attribute before adding in my version and clearing ALL the attributes.
WebControl myControl = null;
string[] elementInfo = elementCode.Split(new char[] { ';' });
switch (elementID)
{
case 1:
myControl = new Button();
myControl.Attributes.Remove("value");
myControl.Attributes.Add("type","submit");
break;
case 2:
myControl = new TextBox();
myControl.Attributes.Clear();
myControl.Attributes.Add("type", "text");
break;
case 3:
myControl = new CheckBox();
myControl.Attributes.Clear();
myControl.Attributes.Add("type", "checkbox");
break;
}
if (myControl != null)
{
string[] properties;
if (elementCode.Length > 0)
{
foreach (string attr in elementInfo)
{
properties = attr.Split(new char[] { '=' });
myControl.Attributes.Add(properties[0], properties[1]);
}
}
return myControl;
}
else
return null;
I know that the loop is firing and the value returned in line 2 is a single line, "value=submit". In fact the markup comes out thus:
<div id="divLastElement">
<input type="submit" name="ctl03" value="" type="submit" value="Submit Me!" />
</div>
I'm sure it's the first [value=''] that is causing it to be empty, but how do I override this behavior? (You can see that in the switch statement that generates the button I've tried to remove the value key early)