ASP.NET Tip: Control the Layout of Your Input Forms


How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017

Although many of your input forms will have a fixed number of fields, in some cases you'll need to vary the number and configuration of your controls. This article demonstrates how to control the layout of your form using a Repeater control and the ItemDataBound event to set various attributes on the controls.

The example form has a simple table that includes the following Repeater control:

<asp:Repeater ID="rptFields" runat="server">
      <td align="right" ID="tdPrompt" runat="server"
          nowrap><%# Eval("Prompt") %>:</td>
      <td><asp:TextBox ID="txtValue" runat="server" />
      <input type="hidden" id="hdnFieldID"
             value='<%# Eval("pkFieldID") %>'
             runat="server" /></td>

Behind the scenes, a table called FormFields (or something appropriate) has these fields:

pkFieldIDPrimary Key, integer
PromptString, contains the prompt to prefix the field with
MaxLengthInteger, indicates maximum number of characters for the field
IsRequiredBit, indicates whether field is required

In the initial Load event for the page, I retrieve a DataTable containing the fields from the database, and then I bind it to the Repeater like this:

rptFields.DataSource = fieldsDataTable;

The key event handler to make this work is the ItemDataBound event handler. It looks something like this:

void rptParameters_ItemDataBound(object sender,
                                 RepeaterItemEventArgs e)
   if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType
       != ListItemType.AlternatingItem)

   DataRow dr = ((DataRowView)e.Item.DataItem).Row;
   HtmlTableCell td = (HtmlTableCell)e.Item.FindControl("tdPrompt");

   if (Convert.ToBoolean(dr["IsRequired"]))
      td.Attributes["class"] = "popupreq";
      td.Attributes["class"] = "popuptext";

   TextBox txt = (TextBox)e.Item.FindControl("txtValue");
   txt.MaxLength = Convert.ToInt32(dr["MaxLength"]);


The first thing you do is make sure that you are looking at an "Item" or "Alternating Item" template. This is because the ItemDataBound event handler is called for the Header and Footer templates, which you don't care about for this code. You then get the DataRow for the current item being bound. Next, you find the table cell that contains the prompt text and change the style to either 'popupreq' or 'popuptext'. In this example, the popupreq style uses bold face, while popuptext uses a non-bold font.

The next step is to find the TextBox control and configure the maximum length of the field. The hidden field in the HTML portion of the page will be populated automatically with the primary key of the field record, which will be used in the back-end code to properly store the data to the database.

When the user hits the Submit button, ASP.NET keeps the values that the user enters into each field and makes them available to the PostBack code. Reading the values is similar to setting them in ItemDataBound, as this snippet shows:

foreach (RepeaterItem ri in rptParameters.Items)
   hdn = (HtmlInputHidden)ri.FindControl("hdnFieldID");
   txt = (TextBox)ri.FindControl("txtValue");
   // Read hdn.Value or txt.Text for persistence code

The reason you stored the field ID is because this data will generally need to be put into a table related to the data being edited. The easiest way I've found to do this is to fill the hidden field with the field's number, which you can use later in a stored procedure or other method to save the field's data to the database.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.


  • Dynamic Form Creation with multiple type fields

    Posted by yenduttjain on 08/29/2006 03:19am

    Hi, This solution is good. But, here you can even have multiple data type fields to show up on the form.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date