Regular Expressions
Regular Expressions
For a recent prototype, we needed to add required-field validators to a bunch of fields in asp.net DetailsView controls. We had to convert each BoundField to a TemplateField and add a RequiredFieldValidator. In other words, we needed to transform <asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-CssClass="importantHeader" /> to <asp:TemplateField HeaderText="Country" HeaderStyle-CssClass="importantHeader"> <ItemTemplate> <%# Eval("Country") %></ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox><asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="TextBox3" Text="Required" /></EditItemTemplate> ...
Now that we’ve got an asp.net DetailsView control with fields from a SubSonic ObjectDataSource and we’ve set the header text, it’s time to work on the code-behind code needed to update the object. The DetailsView template has the following fields: <asp:BoundField DataField="DirectMarketing" HeaderText="Direct Marketing" SortExpression="DirectMarketing" /> <asp:BoundField DataField="AffiliateMarketing" HeaderText="Affiliate Marketing" SortExpression="AffiliateMarketing" /> [... and lots of other fields] We’ll use Visual Studio 2008’s find and replace to generate the update code. Find what:...
Regular expressions in Visual Studio’s find and replace can save a lot of time. For a recent project, we used a DetailsView bound to an ObjectDataSource generated by SubSonic. The bound fields were almost, but not quite, what we needed. They were quickly updated with some regular expression magic. The DetailsView has camel-cased text for the field names and the header text. <Fields> <asp:BoundField DataField="CustomerBase" HeaderText="CustomerBase" SortExpression="CustomerBase" /> ...