Hafizan Aziz Hafizan Aziz - 3 months ago 25
HTML Question

c# set value button programatically.

If I had 100 buttons I want to set text = "Save" and the id is an auto number like saveButton_1, saveButton_2 what the best way to do it?The button isn't dynamic generated can be accessed via code behind.

Current i do like but seem to long.

saveButton_1.value = saveButton_2.value ... till 100 = "Save"

Answer

Expanding a bit @Adil 's answer, if you have your buttons within a container you can get them using the HtmlButton type

IEnumerable< HtmlButton> btns = buttonContainer.Controls.OfType<HtmlButton>();

if(btns != null)
{
     //get only the ones that have a special class to know which ones you need to update
     var btnList = btns.Where(x => x.Attributes["class"] == "save-button");

     //update the text for all that passed the filter
     foreach(HtmlButton ctrl in btnList)
     {
        ctrl.InnerText = "Save Me";
     }
}

Html

<div id="buttonContainer" runat="server">
    <button id="save1" type="button" class="save-button"  runat="server">save 1</button>
    <button id="save2" type="button" class="save-button" runat="server">save 1</button>
    <button id="save3" type="button" class="save-button" runat="server">save 1</button>
    <button id="save4" type="button" class="save-button" runat="server">save 1</button>
    <button id="save5" type="button" runat="server">not save</button>
    <button id="save6" type="button" class="save-button" runat="server">save 1</button>
    <button id="save7" type="button" class="save-button" runat="server">save 1</button>
    <button id="save8" type="button" class="save-button" runat="server">save 1</button>
    <button id="save9" type="button" class="save-button" runat="server">save 1</button>
</div>

enter image description here

If you don't have the button on the same container then you will need to loop through the controls you have in order get to the proper htmlbuttons