Increment control ID in ASP.NET WebForms automatically - asp.net

I have a huge ASPX web form which has around 100 fields which need to be input fields. This web form is an excel sheet converted into to an HTML Page.
I have marked table cells which need to be input fields with a string "txtb" (TextBox).
Once I was done with the look and feel, I tried replacing all txtb string with <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> thinking that the ID=TextBox1 will be incremented automatically by the editor itself, like TextBox2, TextBox3 and so on.
But all the replaced txtb strings show <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> only.
I know that if we copy <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> and paste manually inside the editor, the editor will increment the id by 1 for each paste.
How can I get this to happen for automatic replace? Please help.
<tr>
<td colspan="2">
txtb
</td>
<td>
txtb
</td>
<td>
txtb
</td>
<td>
txtb
</td>
</tr>
<tr>
<td colspan="2">
txtb
</td>
<td>
txtb
</td>
<td>
txtb
</td>
<td>
txtb
</td>
</tr>
It is just some huge file that looks like this. I want to replace each txtb instance with <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>, but the ID should be incremented on each replace. I wish PowerShell was installed on this machine. Otherwise I could have scripted this. Since there is not much time and no other way I can think of, I am posting here for help.

I would recommend this library and query your page for 'txtb' tags and replace them with textboxes. You can count the number of 'txtb' occurences and append them to the ID attribute of your dynamicly created textbox.
http://htmlagilitypack.codeplex.com/
See this links for some examples:
http://htmlagilitypack.codeplex.com/wikipage?title=Examples
Does this help you?

I think the editor will take care of IDs if you first replace the txtb with
<asp:TextBox id="TextBox" ... />
in the Excel itself and then copy/paste it into the editor.

Related

Why is ErrorMessage in RequiredFieldValidator always displayed if it contains markup?

I have encountered some odd behavior, and was hoping someone could shed some light on it. I've got a form laid out using tables (don't need the lecture on this, as its still the most consistent way to get a clean tabular layout in every single browser). I want a required field validator above one of my fields. If I put the validator in a row, it works fine. If I put the validator between rows, with the markup for the rows in the Text or ErrorMessage attribute, it is always displayed, no matter what. It was my understanding that the RequiredFieldValidator could contain markup in the ErrorMessage text, so I'm curious if anyone knows why this is happening.
This works fine:
<table>
<tr><td colspan="2">
<asp:RequiredFieldValidator runat="server" ID="valNewCustPhone" Display="Dynamic" ControlToValidate="txtNewCustPhone" ErrorMessage="Please enter a contact phone number, including<br>area code and country code for non-US numbers." ValidationGroup="NewCustomer"></asp:RequiredFieldValidator>
</td></tr>
<tr>
<td class="rightbold">Phone Number</td>
<td><asp:TextBox runat="server" ID="txtNewCustPhone" MaxLength="25" Width="100"></asp:TextBox></td>
</tr>
</table>
This will always display the ErrorMessage text on the page, even on initial page load, and even after a postback when the field contains data:
<table>
<asp:RequiredFieldValidator runat="server" ID="valNewCustPhone" Display="Dynamic" ControlToValidate="txtNewCustPhone" ErrorMessage="<tr><td colspan=2>Please enter a contact phone number, including<br>area code and country code for non-US numbers.</td><tr>" ValidationGroup="NewCustomer"></asp:RequiredFieldValidator>
<tr>
<td class="rightbold">Phone Number</td>
<td><asp:TextBox runat="server" ID="txtNewCustPhone" MaxLength="25" Width="100"></asp:TextBox></td>
</tr>
<table>

ASP.Net Repeater from Entity Framework (Webform Markup)

I'm having problem finding information on selecting data from an Entity Framework entity from within the markup of an ASP.Net Repeater
I have a Repeater like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="s_Options">
<HeaderTemplate>
<table>
<tr>
<th>Manager Name</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><% !!!!! MY PROBLEM IS HERE %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And I have an Entity called Option.
Option has 3 members:OID, Option_Type, and Option_Value
I am trying to populate this repeaters with Option_Values where Option_Type = "This Option" but I am completely lost on how to do this in the item template and I am having trouble wording my question correctly to find answers to it elsewhere.
First of all, if doing this in the markup for the Repeater is not the best way, please let me know.
Additionally, I am looking for any help on how to filter this entity and how the markup looks.
Also, if this is something that has been covered somewhere else, then I apologize, I must be asking the question incorrectly. If you could help me articulate what I'm asking in a more constructive way, please let me know.
If I understand you question correctly, you want -
<ItemTemplate>
<td><%# Eval("Option_Type") %></td>
</ItemTemplate>
Take a look at the bottom of Displaying Data with the DataList and Repeater Controls article.
Use this code and put your table column name in Eval..
<table style="width: 400px">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="LBLtEXT" runat="server" Text='<%#Eval("Your filed name ")%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

FindControl() Keeps Returning Null!

I realise there are many posts on the web about this already but I cant seem to find any for my particular problem!
I have a dynamic table that is populated using a repeater.
the code is:
<asp:Repeater ID="rptPending" runat="server">
<HeaderTemplate>
<table id="tblPending" cellpadding="0" cellspacing="0" border="0" class="display">
<thead>
<tr>
<th>Company Name</th>
<th>Telephone</th>
<th>Fax Number</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td runat="server">
<asp:TextBox ID="a" runat="server" TextMode="MultiLine" Rows="1"
onChange="__doPostBack($(this).attr('name'),$(this).parent().attr('id'));"
Text='<%# Eval("_companyName")%>'></asp:TextBox>
</td>
<td runat="server">
<asp:TextBox ID="b" runat="server" TextMode="MultiLine" Rows="1"
onChange="__doPostBack($(this).attr('name'),$(this).parent().attr('id'));"
Text='<%# Eval("_telephone")%>'></asp:TextBox>
</td>
<td runat="server">
<asp:TextBox ID="c" runat="server" TextMode="MultiLine" Rows="1"
onChange="__doPostBack($(this).attr('name'),$(this).parent().attr('id'));"
Text='<%# Eval("_faxNo")%>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
Once this table has been populated with data from a datasource, a jquery script iterates through every table cell and edits the id's accordingly. cell_0_0, cell_0_1, cell_0_2 etc.
Now When the text on these text areas change, a postback is initiated, with the __EVENTTARGET being the textarea ID, and the __EVENTARGUMENT being the table cell (parent) ID.
These get sent to the server-side no problem. The issue I am having is GETTING THE TEXT inside the text area...
I have tried adding controls using FindControl("cell_0_0"); Which returns null. Then i found out the FindControl() function IS NOT recursive, so i copied a recursive function off the net... and it Still Fails!
Basically ALL i need to do is GET the value (either innerText or InnerHTML). Ive tried using Control, HtmlControl, HtmlTableRow, and HtmlTextArea.
I just cant seem to get the value. Ive tried recursing throught i a noted earlier, but the controls arent even registered. Im confused.
PLEASE HELP!
Thanks
Alex
Well this is a usually issue, because of the timing of the creations of the controls, and when you create control inside a repeater the timing is even more complex because repeater must first full bind, and then search for the controls.
In my programs to avoid all that I just get the posted value from the Form and I actually not first search to find the control. So just get your posted values from the Request.Form and move on.
Request.Form
All the post data lives on Request.Form, so you can simple get the one you need, or find the one you need. Just a note, to get a value using the Form use the UniqueID, and not the ClientID. Even better get the value from your custom name ids.

Localization html static text [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
asp.net globalization difference in inline tags
I read on MSDN about localization of static text and Localize control but I still don't understand: how should I localize for example a html table's static text?
<table>
<tr>
<td>
<%=Resources.Resource1.String1 %>
</td>
</tr>
<tr>
<td>
<%=Resources.Resource1.String2 %>
</td>
</tr>
</table>
or
<table>
<tr>
<td>
<asp:Localize ID="Column1" runat="server" Text="<%$ Resources: Resource1, String1 %>" />
</td>
</tr>
<tr>
<td>
<asp:Localize ID="Column2" runat="server" Text="<%$ Resources: Resource1, String2 %>" />
</td>
</tr>
</table>
The first is obviously is more readable and I don't need to change translations on runtime. Maybe it has some disadvantages vs second? I'm sorry for my stupid question but I want to be sure that way which I'll select will be more preferable and maintainable.
The examples you showed do not really make any difference. The first one will be easier to code - you will see IntelliSense (code completion) hint.
Personally I prefer Implicit Localization for it will give you defaults and I feel the code is less cluttered:
<table>
<tr>
<td>
<asp:Localize ID="Column1" runat="server" meta:resourcekey="Column2" Text="Column 1 name" />
</td>
</tr>
<tr>
<td>
<asp:Localize ID="Column2" runat="server" meta:resourcekey="Column1" Text="Column 2 name" />
</td>
</tr>
</table>
Also, I find App_LocalResources to be a better way for Localizing Asp.Net applications - you would need to split your translation into several .resx files, which might be just easier to maintain in large code base (for example it would be easier to use Translation Memory software; it should cost less).
For more information on Implicit Localization, see my previous answer.
The second one will give you a Control whose ID you can reference from code to change on the fly. The first one does not have this ability. For example:
if (condition)
Column1.Text += " - 123";
else
Column1.Text += " - 456";

ASP.NET User control with ASP:CreateUserWizard

When using asp:login, asp:forgotpassword and asp:createuserwizard controls within an ascx. (e.g registration.ascx) it can't find e.g UserName, even though it exists within LayoutTemplate. Any idea how to fix this?
If you open smart tag for control in design mode,you shall see that there are steps that can be convertible to templates for customization.
CreateUserWizard http://img197.imageshack.us/img197/3823/7d4df5a594a1abdb8fdc8f1.png
For CreateUserWizard , press Customize Create User Step and you will observe that,control is converted into a table template which you can see how controls named i.e. UserName,Password...
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep runat="server" ID="CreateUserWizardStep1">
<ContentTemplate>
<table border="0">
<tr>
<td align="center" colspan="2">
Sign Up for Your New Account</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
.......
Also know that If you wish to find control from inner templates , you must look for templates with FindControl method to reach the control you desire.
To Find UserName Control in above code,use
CreateUserWizardStep1.TemplateControl.FindControl("UserName") as TextBox
Hope this helps to solve your problem

Resources