FindControl() Keeps Returning Null! - asp.net

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.

Related

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>

ASP multiple repeater use same itemtemplate

In an .aspx page (.NET 2.0), I use several asp:repeater like this one :
<asp:Repeater ID="id_repeater" runat="server">
<headertemplate>
</headertemplate>
<itemtemplate>
<tr>
<td>cell 1.1</td>
<td>cell 1.2</td>
</tr>
<tr>
<td>cell 2.1</td>
<td>cell 2.2</td>
</tr>
</itemtemplate>
Every repeater has the same itemtemplate.
It works fine, but if I have to modify on itemtemplate, I need to update others...
An I'm a lazy developer ;) So I would like to know if it's possible to declare one time the itemtemplate and make all repeaters using it.
thanks in advance.
Create a user control somewhere and then use that as your item template.
<asp:Repeater ID="id_repeater" runat="server">
<headertemplate>
</headertemplate>
<itemtemplate>
<my:control runat="server" id="myUserControl" />
</itemtemplate>
</asp:Repeater>
Create your own user control (ascx file) that will contain that repeater, then use it always when you need the same control.
Rather than focusing on maintaining a separate item template, consider placing this in a custom user control. Any time you find yourself repeating similar blocks of code, you're probably missing an opportunity to move it into a class or a control.

Variable number of columns with Repeater control ASP.NET 3.5

One of pages contains a Repeater control. Earlier, I bind a static number of columns to the repeater. For example, my stored procedure will return Name,Age,Salary,Phone,DOB of an employee. So I could use like the following
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary")%>' ToolTip="Salary"></asp:Label>
</td>
</tr>
</ItemTemplate>
Now I want to change the design. I have a settings page and I will say which columns should be listed here. Some time I need to list only Name and Age etc. So I can not hard code the design of Repeater. What is the best way to handle the situation ? Is it good to dynamically add labels to the item template?
One approach is to have a user control for each field that may be displayed. Whether the same user controls for all the field that is configurable or a separate control for each field. Then when you bind the repeater you can set the Visible property of the control accordingly.
Is that the direction you wanted to go?
You could use a GridView with TemplateFields. Depending on the settings you show or hide the columns. Here you find a documentation of the TemplateField.
EDIT: Another control with more flexibility than Repeater is the ListView.
If you want to use the Repeater Control you can use placeholders to turn on and off single columns. Just put every column into a PlaceHolder and turn on and off the visibility. Instead of a PlaceHolder you can of course use a UserControl as well.
EDIT 2: Solution with PlaceHolder could look like this:
<ItemTemplate>
<tr>
<asp:PlaceHolder Visible="<%# IsSalaryVisible %>" runat="server">
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary")%>' ToolTip="Salary"></asp:Label>
</td>
</asp:PlaceHolder>
<asp:PlaceHolder Visible="<%# IsNameVisible %>" runat="server">
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Name")%>' ToolTip="Salary"></asp:Label>
</td>
</asp:PlaceHolder>
</tr>
</ItemTemplate>

User control inside update panel causing full page postback

I have a user control with linkbuttons (used for paging) and a repeater inside an update panel. The paging works correctly, but is causing a full page postback every time I click through to the next page.
The update panel looks like this:
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="rptOrganizations" runat="server">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Organization</th>
<th>State</th>
<th>Accredited Since</th>
</tr>
</thead>
</table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="ltlInstitution" runat="server" />
</td>
<td>
<asp:Literal ID="ltlState" runat="server" />
</td>
<td>
<asp:Literal ID="ltlAccreditedDate" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<uc2:RepeaterPaging ID="rpPager" runat="server" PageSize="10" OnNextButtonClickEvent="btnNext_Click" OnPreviousButtonClickEvent="btnPrev_Click" />
</ContentTemplate>
</asp:UpdatePanel>
And the contents of the user control look like this:
<asp:LinkButton ID="btnPrev" runat="server" OnClick="btnPrev_Click">Previous</asp:LinkButton> |
<asp:LinkButton ID="btnNext" runat="server" OnClick="btnNext_Click">Next</asp:LinkButton>
<asp:Literal ID="ltlNumResults" runat="server" /> results returned.
So far, I have tried adding an async postback trigger for the user control, which does cause an async postback but does not update the rest of the text in the update panel. In otherwords, the async postback occurs and the next page shows up, but the original text in the repeater is there as well just below it.
I have also confirmed that I have IDS set on my linkbuttons, since that can trigger a full postback inside an update panel.
I have tried changing the update panel mode (Always, Conditional, ChildrenAsTriggers, etc.).
None of it makes a difference - the only thing that actually causes an async postback is to use the trigger, but then the rest of the content in the update panel is not updated, so I get duplicate content. Any ideas?
Full postback happens if your UpdatePanel cannot render its contents to a <div> (e.g., when it is situated inside of <tr>). So check you html inside of UpdatePanel, you might find the answer there (also, look for some incorrect xhtml, like incorrectly closed elements).
Remove the update mode="Always" Don't put anything over that and it should work.
One more thing are you adding script manager to your page or control not?
Without script manager it will not work.

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