-1

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!

Lex
  • 1

1 Answers1

0

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).

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • thanks for your reply. how do you find the control by its value? – Lex Jan 22 '21 at 03:59
  • Coded for 20+ years on mainframes, desktop, web based, lots of JavaScript and in 20 years of code from Android software to oil spill simulations for major oil companies? Never in all those years did I ever need to find, reference a control by the "value" in the control. Either something so strange, so crazy, so unusual here that I am at a total loss for words, or I am simply not grasping or understanding your question? Certainly one might want to find or get a row of data, but that means you should be using a table or some kind of grid or repeater here that includes repeating the buttons too. – Albert D. Kallal Jan 22 '21 at 12:00
  • You can certainly loop/traverse all controls and for each control look for a value - but as I stated - this seems oh so wrong. So you can do a for/each on all controls, and check the value of each control. Your question did not hint or ask or even suggest that this would be a requirement. My example shows with great ease how you can use a string to get/reference a control value - so that resolution occurs at runtime. But searching controls for a value? Sure, you can do this - but you be iterating all controls in the web page. – Albert D. Kallal Jan 22 '21 at 12:03
  • Sorry for the noob question, just started learning c# and asp.net. anyway thanks for your responses. I've already figure it out. Calling a codebehind did the job. `` – Lex Jan 24 '21 at 22:08
  • Sure, but what you figure out does not suggest you finding a "value" you are specifing a control - so as I stated if my name is Albert, I would not search for Albert, but might search or find or use a control called FirstName. As I stated, I suppose one might have a grid of data and want to find a particular PK id in that grid, but ultimately we looking for a value by a given control, or set of controls. We not searching for a value, but wanting a given control to get some value just like your btn_100 is a given control. So at the end of the day you WERE looking by some control - not by value – Albert D. Kallal Jan 25 '21 at 04:20