Search code examples
c#htmlasp.netwebformsinnerhtml

Get values of input from InnerHTML


I have a problem with getting values from inputs in c#. I have a scenario where some users see a different table. Each user has his own levelofapprove(from 1 to 5).

switch (levelofapprove_converted)
            { //HiringManager
                case 1:
                    id_hiringmanager_div.InnerHtml = @"<table class=""table"" id=""id_hiringmanager"" runat=""server"">
<td style=""height: 37px""><input type=""checkbox"" class=""form-check-input"" id=""id_check_claims"" runat=""server""></td></table>";
                    break;
                //IT
                case 2:
 id_hiringmanager_div.InnerHtml = @" <table class=""table"" id=""id_IT"" runat=""server"">                
                <tr>
                    <td style=""color: rgb(0,0,0); height: 37px;"">Delete User Access Rights</td>
                    <td style=""height: 37px""><input type=""checkbox"" class=""form-check-input"" id=""id_check_access_rights"" runat=""server""></td>
                </tr>
</table>";
                    break;
            }

Here is a design how it looks like, with InnerHtml I am populating div with a different table for each person.

<div runat="server" id="id_hiringmanager_div"></div>
  <table class="table">
    <tr>
      <td colspan="2">
       <button class="btn btn-primary btn-lg btn-block" onserverclick="btn_approve_Click" runat="server">Approve</button>
      </td>
     </tr>
  </table>

So when I am trying to get value from a checkbox or call for OnclickEvent nothing is happening. Error list says that is doesn't see such control. Here is how i am trying to call them:

bool check_pending_leave = id_check_pending_leave.IsChecked;

Solution

  • So your problem is that you are creating the inputs over a string. Also the runat="server" that I suggest will not help as the server doesn't know that they are created.

    You have to options: First is to create the html markup in the aspx/ascx files and put them in asp:panel and hide/show them with the codebehind logic you have.

    Or you do it like you started and add on the inputs the tag name="id_check_claims" and then in code behind you need to call the inputs like:

    //IMPORTANT: if checked it will return "on", not 1 or true
    var strValue = Page.Request.Form["id_check_claims"];
    

    Hope this helps.