2

How can I loop through html controls inside an ASPX page which don't have "runat=server" attribute? Because of using code blocks and dynamic variables on them I cannot add the attribute:

<input lang='<%= language.ID %>' checked='<%= check(x) %>' />
2
  • Why do you need to access those controls? to get the values, to add attributes? Commented Feb 11, 2018 at 19:24
  • 1
    Without "runat=server" there is no control, it is just plain text, as far as Asp.net is concerned. Commented Feb 11, 2018 at 21:10

1 Answer 1

1

How can I loop through html controls inside an ASPX page which don't have "runat=server" ...

Depends on what you want you're looking to do.

  • As the other answers have stated, end of the day, it's all HTML and therefore a Form will submit (by default an HTTP POST for ASP.Net Web Forms hence Postback) somewhere and you can loop through HttpRequest.Form on the server side (aka "code-behind").

  • As stated, in the end everything renders as a standard HTML element that you can provide id or class and "loop" through them and do what you want on the front end.

    Here's a trivial example:

var eles = document.getElementsByClassName('inputs');    
var count = eles.length;
var str = "there are " + count + " elements:<br>";
for (var i = 0; i < count; i++) {
  str = str + "name: " + eles[i].name + " id: " + eles[i].id + "<br>";
}

var result = document.getElementById("result");
result.innerHTML= str;
<input name="foo" lang='some-server-generated-string-1' id="ele1" class="inputs" />
<input name="bar" lang='some-server-generated-string-2' id="ele2" class="inputs" />
<p id="result"></p>

Hth.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.