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>
Related
Im getting started with ASP.NET. I need to let users click on rows of a table and then show them some extra information relating to that row. This is down using RowDetails templates in WPF. I am wondering is there anything similar for ASP.NET?
I would use a Repeater with a Table, and make it so each RepeaterItem is two table rows. Make the 2nd row hidden, and the 1st row toggle the 2nd one's visibility when clicked on.
I don't remember the exact syntax, but it looks something like this:
<asp:Repeater>
<HeaderTemplate><table></HeaderTemplate>
<ItemTemplate>
<tr click="javascript: ToggleNextRow(this);">
<td><%# Eval("Id") %></td>
<td><%# Eval("Name") %></td>
<td><%# Eval("Description") %></td>
</tr>
<tr style="display: none;">
<td colspan="3"><%# Eval("Details") %></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
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.
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>
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.
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