We help IT Professionals succeed at work.
Get Started

How can I fill in ASP:TextBox.Text property dynamically as I generate table?

666 Views
Last Modified: 2013-11-26
Hello,

I'm super new to ASP, and struggling with basic concepts. In the attached code snippet, you can see that I'm able to lay down table rows containing <INPUT> tags, dynamically filling in the Text property via the embedded code block. For instance, "<%= s.Name %>" evaluates to the Name property of the business object (for that row).

I wanted to use the AJAX CalendarExtender control, but it appears to support binding only to an ASP:TextBox control, as opposed to the generic <INPUT>. Ok, so I swapped out the <INPUT> for the <ASP:TextBox>. The problem is that it immediately balked at my previously-ok use of the embedded code block. I've been unable to discover an equivellent means of inserting the date value from the BO into the ASP:TextBox.

To be clear, please keep in mind that I'm laying down potentially hundreds of rows, each to receive it's corresponding date values.

A related topic: I noticed that only the first row gets the date picker. I assume I need to generate a unique (incrementing) ID for each ASP:TextBox, so that each new CalendarExtender will bind to its corresponding TextBox.

But, when I tried something like:

<% int nextId = 1; foreach (Site s in ViewData.Model.Sites)
                   { %>
...
<asp:TextBox ID="Date<%=nextId%>"

I get:

Parser Error Message: 'Date<%=nextId%>' is not a valid identifier.

Obviously, I'm missing something pretty basic here. I don't understand why it's ok to evaluate code blocks in the <INPUT> construct, but not the <ASP:TextBox>.
<tbody>
    <% foreach (Site s in ViewData.Model.Sites) 
       { %>
            <tr>
                <% if (s.IsSelected) { %><td><input type="checkbox" name="selectedSites" checked="checked" value="<%= s.Id %>" /></td><% }
                <td>
                     <%--<input id="Date2" class="shortdate" type="text" value="<%= s.EffectiveStartDate.ToShortDateString() %>" /> --%>
 
                    <asp:TextBox ID="Date2" runat="server" Text='<%= s.EffectiveStartDate.ToShortDateString() %>' >
                    </asp:TextBox>
 
                    <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="Date2" CssClass="">
                    </cc1:CalendarExtender>
 
                </td>
                <td><input type="text" value="<%= s.EffectiveEndDate.ToShortDateString() %>" /> </td>
                <td><%= s.Name %>  </td>
            </tr>
    <% } %>
</tbody>

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE