0

I've struggled with this for days now, trying to access html inputs, i.e. checkboxes or textboxex generated at runtime based on DB results. My HTML which is added as a literal control to a asp placeholder looks like this:

<div class='cart-item'>
  <div class='product-name'> <h3>gold baseball figure</h3></div>
  <img src='Graphics/gold-trophy.png'></img>
  <div class='inventory-short-description'> <p>A finely crafted gold plated baseball figuring to top your choice of trophey base (sold seperatly).</p></div>        
  <div class='clear'></div>
  <div class='item-price'><p>$22.95</p></div>
  <div class='cart-item-checkbox'><label><input type='checkbox' id='1' name='1' runat='server'/> Select </label>
  <a href='products.aspx?viewItem=1'>view item </a></div></div

Question is, how do I access this check box in the .cs code behind page?

My code which is generating this html and adding it to the page is in the overrided OnInit method. Looking at the placeholder on postback shows that the checkbox is in the literal control.

I've tried:

Page.FindControl() returns null when searching for dynamic control

ASP.NET page_init event?

FindControl() return null

http://forums.asp.net/t/1336244.aspx?finding+HTML+control

....And countless others.

Yesterday I used a hackish way to set the value of these checkboxes to a asp.net hidden field using jquery. My coworkers (all java devs) say this seems to be the wrong way to access these elements. Is there a better way? I'm beginning to think I've coded myself into a spot I can't get out of.

Thanks for the help.

Community
  • 1
  • 1
Craig Smith
  • 66
  • 1
  • 9

1 Answers1

2

In your .aspx

<asp:Repeater ID="rptItems" runat="server" EnableViewState="False">
        <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" /> 
        </ItemTemplate>
</asp:Repeater>

In your .aspx.cs file you can access repeater in postback events as below

 foreach (RepeaterItem rptItem in rptItems.Items)
        {
            TextBox txtName = (TextBox)rptItem.FindControl("txtName");
            if (txtName != null) { System.Diagnostics.Debug.Write(txtName.Text); }          
        }
ram hemasri
  • 1,624
  • 11
  • 14
  • how do I access this textbox. Your example would have 20 textboxes with the same ID. Also VS gives error when I try to set id with something like ID="<%#getProductId()%>" Error 1 The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: – Craig Smith Sep 26 '15 at 20:33
  • You can have asp:button control on your page and in button_click event loop through Repeater Items – ram hemasri Sep 26 '15 at 20:41
  • also, no way to call public methods of this object. Making fields public is not good programming as it shows here: http://code.runnable.com/UjMj269qAWwQAAB3/asp-net-how-to-use-repeater – Craig Smith Sep 26 '15 at 20:48
  • @CraigSmith will you be able to share your code on runnable or github? – ram hemasri Sep 27 '15 at 08:26
  • I just got this working. After I changed my object to have public fields (something which I'm told not to do...) the aspx page can get the values in the repeater. On postback I've got the values back from the textbox. This method is working, but wrong on at least 2 levels. I now have invalid HTML --> multiple elements with the same ID and no way to control access to my data object. – Craig Smith Sep 27 '15 at 08:36
  • @CraigSmith if you use asp:textbox control with an ID txtName, .net creates unique control IDs like ct100_txtName_. Are you not getting these IDs in your HTML? If you want your data fields/properties accessible outside your class then they need to be public. – ram hemasri Sep 27 '15 at 08:42