Question

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)

Was it helpful?

Solution

Ultimately with an ASP.NET Button control, the HTML value comes from the Text property on the ASP.NET Button. I'm guessing if that's not set, it simply renders another value attribute.

Try setting the .Text property on the Button to "Submit me!" instead of setting its value via the Attributes collection.

So the partial snippet looks like:

case 1:
   myControl = new Button();
   ((Button)myControl).Text="Submit me!";
   myControl.Attributes.Add("type","submit");
   break;

OTHER TIPS

Try using a HtmlInputButton instead.

YOu need to use the Text property. As you said in a comment you don't see the "Text" property in Intellisense. This is because to Intellisense, myControl is a WebControl (that you happen to make a Button). The WebControl is a base class for the specific controls you create, but itself does not have a "Text" property. Use the following instead:

myControl = new Button();
((Button)myControl).Text = "submit";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top