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>
Related
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.
I have a table with the following structure,
I need to restructure the table to show the CustomSpaceName in the following order,
Space3 Personal Case Quick case
Space1 Space2
For each entry I will create a link button and pass the CustomSpaceId in query string.
So which is the ASP.NET Data Control matches best with my requirement. I think using the loop and generate table structure is a BAD idea.
No Need of doing that with a old method when ASP.net gives you GridView and other Data Bounding controls
Basically gridview will do the same operation that you told in a efficient way.
You can use the in-built methods.
Grid View
Repeater
ListView
DataList
Here I will recommend DataList.
Use RepeatColumns="4" property.
<asp:DataList ID="DataList1" RepeatColumns="4" runat="server">
<HeaderTemplate>
<asp:Label runat="server" ID="lbl1" Text='Header'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lbl1" Text='<% Eval("CustomSpaceName ") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
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.
I have a table in the itemtemplate of a gridview.
My requirement is something like what's shown in the below link
http://img253.imageshack.us/img253/5987/requirement.jpg
i.e. Say 3, 4 and 5 are user id's.
The middle column can contain some information about that user.
Say for User 3 we have Info 1, Info 2 and Info 3 in those three rows.
Similarly, for user 4 and 5, we would have some values in the middle column.
The number of rows in the middle column can vary. It depends on the number of rows
returned by the DB call for that user.
So, how can I achieve this using grid view?
Will it be possible for me to add rows in the databound event dynamically by looking
at the previous or next row in the datasource? If so, how should I go about it?
Or is there an easier/better way to do it?
Thanks
You can take a literal control in the itemtemplate field for the middle column and bind it on rowdatabound event with dynamic created table with the user data.
You might want to maybe think about using a Repeater to generate the outer table and perhaps embed a Gridview into each row to do the detail - I think this might give you a bit more flexibility in your layout.
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label1" Text='<%# DataBinder.Eval(Container.DataItem, "UserID") %>' runat="server" />
</td>
<td>
<asp:GridView ID="Gridview1" runat="server" />
</td>
<td>
<asp:Button ID="Button1" Text="Submit" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
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.