vb asp.net : A way to avoid repeating a code in common in ItemTemplate and EditItemTemplate - asp.net

I'm a newbie in asp.net.
When using FormView, there is a big amount of code in ItemTemplate, EditItemTemplate and InsertItemTemplate which is almost identical.
For example:
<asp:ListBox ID="ListBox2" runat="server" Rows="1" CssClass="field"
DataSourceID="StatusList" DataTextField="DESCRIPTION"
DataValueField="STAT_ID" SelectedValue='<%# Bind("STAT_ID") %>'>
</asp:ListBox>
(Note: at the exception that Eval() would be used instead of Bind() in ItemTemplate)
I've been trying to avoid repeating this code but without the expecting result:
ListView allows the use of LayoutTemplate - but I didn't see any examples that insert this kind of code in LayoutTemplate. And inserting this code in LayoutTemplate would result in an error.
DetailView allows to produce code automatically but I'd like to use a specific design (for ex. using "fieldset" that encompasses some fields).
What would be the best way to avoid repeating this kind of code ?

You don't have to much choice about seperately specifying the Bind/Eval part, but you do have some control over the other pieces. You can make a custom UserControl that contains your layout.
Usually I include a property on this usercontrol called "Mode" which I either set to Edit or View, then based off of this property I change enabled/visible properties on the controls. You'll also need to include a property for each value you want bound/displayed in the usercontrol.
Put some labels, textboxes, etc... in your designer and hook them up to properties in your code behind, put the usercontrol on your page in your item/edit template and eval/bind to your data to the various properties (make sure to set the mode so it displays right).

Related

Eval inside an ASP.net repeater doesn't work inside another control

I have a Reapter control with a custom server side control inside of it. When I place the code:
<%# Eval("DateAdded") %>
inside the item template of the repeater it works fine, but when I place it inside the custom server control inside the repeater, it doesn't work.
Is it possible to do something like Parent.Eval() or Container.Eval() to get back to the context of the Reapeter rather than the custom control?
The repeater is databound, the control that you are placing this eval statement in isn't. You'll need to pass the value to the control using a property.
For Example:
<uc1:MyControl MyProperty='<%# Eval("DateAdded") %>' />
You can now access the MyProperty property inside your control to access this value.
It should work. Can you supply more context/code for the server control?
Also, are you using single quotes to set properties on a control dynamically using eval? Double quotes should throw an error, not just be ignored.
MyProperty='<%# Eval("DateAdded") %>'

Nested Repeater not being recognised in code behind

I have a nested repeater set up but the child repeater control is not being recognised in the code behind. It's not even being added to the designer file. I've tried this on an aspx and an ascx page but both gives the same problem.
<asp:Repeater ID="RepeaterParent" runat="server">
<ItemTemplate>
<asp:Repeater ID="RepeaterChild" runat="server">
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
with this on the page the code behind only recognises the RepeaterParent but not RepeaterChild.
Can anyone help me out here?
Many thanks!
Like any other control that is used within a repeater (or template) control, you need to retrieve the control using FindControl.
So, in your item data bind event handler for the parent, you would do:
var childRepeater = RepeaterParent.FindControl("RepeaterChild") as Repeater;
The RepeaterChild will be accessible when you use FindControl("RepeaterChild") on the parent repeater I think. Can't remember the exact syntax.
Also note that the FindControl method will also take the context of the current item of the parent repeater, as the name you specify will repeat. Naming containers do some work in the background to provide unique naming, but it is hard to track sometimes.

Nested ASP.Net controls not defined

I have a couple of controls that are set to runat="server", but are showing up as "not declared" in the vb code behind. They are not being setup in the designer.vb file at all, even if the design.vb is re-created.
The only thing that I can think might be causing this is that the controls are inside of a custom control. The code looks something like this (it has been modified because of NDA):
<abc:MyCustomControl>
<additionalItems>
<asp:CheckBox id="coolCheckboxOfPower" runat="server" Text="Triple Rainbow!">
</asp:CheckBox>
</additionalItems>
</abc:MyCustomControl>
So using the example above, if I try to use coolCheckboxOfPower in my vb page, it says it is not declared.
It has been suggested to me that asp controls cannot be nested. Is this true, and if so, how do I get around that?
Asp controls can certainly be nested. Just look at asp:Panel, asp:ListView etc. You have to do some extra work when creating your control to enable this to happen. Namely you have to make have an ITemplate property on your control. Check out the following Building Templated Custom ASP.NET Server Controls to get you started

CheckboxList - Use web.config value in text field

The scenario I'm dealing with is I have a set of entries in a database that have an image associated with them. This image needs to be 'accepted' or 'rejected' via a web interface.
I'm using ASP.NET WebForms.
I'm attempting to databind my recordset of entires to a CheckBoxList control. I would like to combine data from my dataset with information from the web.config file and plain text to display an image:
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="DataSource1"
DataTextField="ImageIdentifier"
DataValueField="EntryId"
DataTextFormatString="<img src='<%$ AppSettings:GetImageUrl %>{0}' />" />
This approach correctly outputs the plain text and the DataTextField value, however it does not interpret the code inside the <% %> block and prints the whole thing literally in the generated HTML, in spite of it being correctly highlighted in the editor.
Is this possible to achieve declaratively? If not, is there a better way than iterating through the entries in the list in code on the OnDataBound event?
Thanks in advance,
Jamie
I believe you're using the wrong <% tag. In order to evaluate in a binding expression like that it should be <%#
What does your web.config look like? You'll won't be able to use that binding syntax here - you'll have to hook into the databound event of the checkbox and iterate each checkbox, updating them as necessary from the values in your web.config.
EDIT
If you don't want to iterate each checkbox after the checkboxlist has been databound, you'll have to update your dataset first, before you bind to it.

How to populate UpdatePanel in Repeater in ASPX (not code-behind)?

I have a Repeater which displays a list of items (in a grid-like/table view). When a user clicks an item, I display an UpdatePanel under this item with additional information relevant to the item (similar to Accordion control). I know how to populate the elements of the UpdatePanel in code-behind (I pass the ID of the selected element in the Repeater control as CommandArgument, get additional info for this ID, and set up the text fields of the active UpdatePanel controls). But I'm wondering if I could set up binding directly in the ASPX (instead of code-behind). When I used the <%= %> syntax to assign text fields of the UpdatePanel control the values of the page properties, e.g. <%= Comment %>, it sort of worked, but it changed the fields of all UpdatePanels in the repeater. Is there any way to bind the active UpdatePanel to the current values and leave already bound UpdatePanels unchanged?
Are you looking to display a container that displays additional information? Is there other activity in the "box" that requires it be an updatepanel?
<asp:repeater>
<itemtemplate>
<%# Eval("Name") %> <%# Eval("LastName") %><br />
<span onclick="$get('<%# Eval("Id") %>')">View Age</span>
<div id="<%# Eval("Id")%>" style="display:none;">
Age: <%# Eval("Age") %>
</div>
<itemtemplate>
</asp:repeater>
Ithink that's right, some syntax may be off a bit (typing without intellisense). Would that work?
I used ID as a unique identifier for the div id and the onclick command. You could also use jquery, asp:controls or whatever else you wanted.
Easiest way is to nest a FormView inside the update panel. Then the only thing you need to do in the code behind is get the additional info, assign it to the FormView.DataSource, and call FormView.DataBind(). Everything in the FormView will use the <%# Eval("SomeColumn") %> syntax. You'll probably need to use a FindControl() to get a reference to the FormView. I'd type up the code for you but I'll save you some headaches down the road and say DON'T DO THIS.
The update panel is about the most inefficient way to do any ajax stuff. The only way to get it all to wire up correctly with this repeater and server side code is to either have a gigantic viewstate or to rebind the repeater in your page load. You are turning a request that could be 300ms into something that will take over a second...or longer! Get familiar with a good ajax framework and don't be afraid to write real html. At the very least, use a webservice that loads a usercontrol with your markup.
I know the update panel is easy, and it's built in. It might even be adequate for what you are doing, but you must resist. You'll be glad you did.

Resources