Mertez
Mertez

Reputation: 1131

How can I loop through html controls in ASP.NET page with No "runat=server"

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) %>' />

Upvotes: 2

Views: 1548

Answers (1)

EdSF
EdSF

Reputation: 12371

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.

Upvotes: 1

Related Questions