asp.net code behind accessing button using button value

545 Views Asked by At

I'm creating a form validation. i have these buttons with on my .cshtml. When the user clicks it, a css class (selected) will be added on it.

<button type="button" ID="btn_100" runat="server" class="selected">100</button>
<button type="button" ID="btn_200" runat="server">200</button>
....
<button type="button" ID="btn_600" runat="server">600</button>

Once the user submits the form, it will be validated and if it fails, it should keep the button class in it or simply keep the button state.

What i'm thinking is to target the button from code behind using its value. Is there a similar like jQuery selector in asp.net that this?

$('button[value="100"]");

Or have a dynamic button variable, like this

btn_(value_selected).Attribute.Add("class","selected");

Any suggestion? TIA!

1

There are 1 best solutions below

5
Albert D. Kallal On

You can modify the style or anything in code behind.

However, you do have to "find" the control.

So, say you can do this:

   Dim btn1 As Button

   btn1 = Me.FindControl("Button1")

   btn1.Style.Add("border-radius", "5px;box-shadow: 1px 2px 5px #666;")

note how we can supply a "string" value to get/find the button.

C#

Button btn1;

btn1 = this.FindControl("Button1");

btn1.Style.Add("border-radius", "5px;box-shadow: 1px 2px 5px #666;");

Note that "add" for style is really quite much the same as setting - if you were to say accident add the above style setting two times - it does not cumulate up (so if style setting exists - it is replaced).