Can't access HTML input controls created in code behind in a literal

449 Views Asked by At

I have an aspx page and added a literal control:

<asp:Literal runat="server" ID="ControlLiteral"></asp:Literal>

Then, in codebehind I'm using a for sentence to massively create controls, the idea is something like this:

ControlLiteral.Text = @"<input  id=""txtMaterial1"" type=""text"" value=""escribe un valor"" disabled=""disabled"" onclick=""somefunction(this)"" style=""width:250px;"" />

then I want to get the value after a submit button in codebehind, but problem is I can't find them and I'm starting to think that codebehind is not aware of those controls even though I check the HTML source code and everything is there so... any ideas?

I have tried the following:

  1. added the runat="server" in the string that creates the control, but of course, it doesn't work, it just added that text runat="server" in the html

  2. Request.Form["txtMaterial"] or Request["txtMaterial"] finds nothing

  3. Page.Controls.Find("txtMaterial") returns null

  4. When I do this, this.from1.Controls.Count it retrieves only 11 controls isntead of the 210 controls I added in the for statement, and if retrieve them like this this.from1.Controls[2], those irrelevant html tags created by the asp.net engine

  5. Page.Controls.Count retrieves only 5 controls

Those controls are inside a table, dont know it that has something to do, so, do you have any ideas, it's been a while since the last time I used asp.net but remember that this was possible, please let my know your ideas,

1

There are 1 best solutions below

1
On

Try using submit to call a function in the javascript.

__doPostBack(controlid,json.stringify())

example:

$scope.btn_generatepdf = function () {
      var myJSON = '';
      for (var i = 0; i < $scope.elements.length; i++) {
           myJSON = myJSON + ($scope.elements[i].value) + '|';
      }
      alert(myJSON);

     __doPostBack('btn_generatepdf1', myJSON);
}    

codebehind page_load:

if (page.ispostback)
{
     string parameter = Request["__EVENTARGUMENT"];
     string[] allParams = parameter.Split('|');
     string val = Request.Params.Get("__EVENTTARGET");
}