FormView code duplication

Posts   
 
    
Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 26-Sep-2006 23:07:02   

I know this is not specificaly LLBLGen Pro, but please bear with me...

Anyone have any thoughts on reusing code in a FormView? The contents of an 'edititemtemplate' and an 'insertitemtemplate' usually seem to be identical. I was able to put a UserControl in these templates but it only works for reading data, not inserting or updating. Even worse, I have to build three different pages - where the first has some controls, the second contains the first plus some additional controls, and so on. It seems like the only solution is repeating all this code for each template and each page - yuck. I've been searching all over without any satisfactory answer rage

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Sep-2006 08:09:45   

As far as I know, this should be working.

So how do you attempt to make the user control insert ot update data? Would you post some code?

Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 27-Sep-2006 17:09:40   

Here is my FormView:

<asp:formview id="FormView1" runat="server" datakeynames="Id" datasourceid="PolicyDataSource" defaultmode="Insert">
    <edititemtemplate>
        <h4>Edit this policy:</h4>
        <uc1:entry1control id="EditEntry1Control" runat="server" />
        <p>
            <asp:button id="UpdateButton" runat="server" causesvalidation="True" commandname="Update" text="Update" />
            <asp:button id="CancelButton" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" />
        </p>
    </edititemtemplate>
    <insertitemtemplate>
        <h4>Add a new policy:</h4>
        <uc1:entry1control id="InsertEntry1Control" runat="server" />
        <p>
            <asp:button id="InsertButton" runat="server" causesvalidation="True" commandname="Insert" text="Insert" />
            <asp:button id="CancelButton" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" />
        </p>
    </insertitemtemplate>
    <emptydatatemplate>
        <p>No matches.</p>
    </emptydatatemplate>
</asp:formview>
<sd:llblgenprodatasource id="PolicyDataSource" runat="server" datacontainertype="EntityCollection" entitycollectiontypename="Business.CollectionClasses.PolicyCollection, Business" maxnumberofitemstoreturn="1">
    <selectparameters>
        <asp:querystringparameter name="Id" querystringfield="Id" />
    </selectparameters>
    <updateparameters>
        <asp:querystringparameter name="Id" querystringfield="Id" />
    </updateparameters>
</sd:llblgenprodatasource>

And the user control is just a table with a bunch of text boxes, drop downs etc. bound to the various fields from the llblgenprodatasource on the main page. In this form, I can view data but not update it. When I move the contents of the user control into the main page, the update works.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 28-Sep-2006 08:31:07   

So what happens when you try to use the UserControl in the Insertitemtemplate or the Edititemtemplate? Is there an error/exception that you could post?

Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 28-Sep-2006 16:42:56   

Walaa wrote:

So what happens when you try to use the UserControl in the Insertitemtemplate or the Edititemtemplate? Is there an error/exception that you could post?

No, there is no error. SELECT works fine but when I want to UPDATE or INSERT, the Update button fires but no SQL is sent to the data source, and for some reason the Insert button doesn't fire at all scratch that, the Insert button also fires but does not cause any SQL to be executed.

However it all works nicely if I don't use the UserControls.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 29-Sep-2006 13:25:11   

You've to use Bind() statements instead of Eval() statements, perhaps that's it (I'm not sure if you're using these). The datasourcecontrol binds to the formview, so the formview should write the values into the controls in the formview.

Frans Bouma | Lead developer LLBLGen Pro
Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 29-Sep-2006 16:19:55   

Otis wrote:

You've to use Bind() statements instead of Eval() statements

Yeah, I'm using Bind(). I guess I'll give it a try with a simpler example. So you don't know of anything that would prevent the controls from binding even though they're located in a UserControl and not directly in the FormView? I'm amazed I can't find any definitive answer online - I would think this would be a common request.

Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 29-Sep-2006 17:14:11   

All right, I've created the simplest example I could think of - and I can report that two-way databinding definitely does NOT work from a UserControl within a FormView. The update and insert commands don't fire any SQL on the database.

The web form:

<%@ page language="C#" %>

<%@ register src="Control.ascx" tagname="Control" tagprefix="uc1" %>
<%@ register assembly="SD.LLBLGen.Pro.ORMSupportClasses.NET20" namespace="SD.LLBLGen.Pro.ORMSupportClasses" tagprefix="llblgenpro" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:formview id="FormView1" runat="server" datakeynames="Id" datasourceid="LLBLGenProDataSource1" allowpaging="True" defaultmode="Edit">
                <edititemtemplate>
                    <uc1:control id="Control1" runat="server" />
                    <br />
                    <asp:linkbutton id="UpdateButton" runat="server" causesvalidation="True" commandname="Update" text="Update">
                    </asp:linkbutton>
                    <asp:linkbutton id="UpdateCancelButton" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel">
                    </asp:linkbutton>
                </edititemtemplate>
                <insertitemtemplate>
                    <uc1:control id="Control2" runat="server" />
                    <br />
                    <asp:linkbutton id="InsertButton" runat="server" causesvalidation="True" commandname="Insert" text="Insert">
                    </asp:linkbutton>
                    <asp:linkbutton id="InsertCancelButton" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel">
                    </asp:linkbutton>
                </insertitemtemplate>
            </asp:formview>
        </div>
        <llblgenpro:llblgenprodatasource id="LLBLGenProDataSource1" runat="server" datacontainertype="EntityCollection" entitycollectiontypename="Business.CollectionClasses.PersonCollection, Business">
        </llblgenpro:llblgenprodatasource>
    </form>
</body>
</html>

The user control:

<%@ control language="C#" classname="Control" %>

<script runat="server">

</script>

Id:
<asp:label id="IdLabel1" runat="server" text='<%# Eval("Id") %>'></asp:label><br />
UserName:
<asp:textbox id="UserNameTextBox" runat="server" text='<%# Bind("UserName") %>'>
</asp:textbox><br />
LastName:
<asp:textbox id="LastNameTextBox" runat="server" text='<%# Bind("LastName") %>'>
</asp:textbox><br />
FirstName:
<asp:textbox id="FirstNameTextBox" runat="server" text='<%# Bind("FirstName") %>'>
</asp:textbox>
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 24-May-2007 20:50:03   

Did you find a solution? I am having the same problem (see http://llblgen.com/tinyforum/Messages.aspx?ThreadID=9971).

I did some experimenting a pulled some of my controls OUT of the ascx's that I use in the InsertItemTemplate and EditItemTemplate and placed them DIRECTLY into the template markup and my updating magically started to work!!!!

This is awful. Why the heck can't I use usercontrols in the templates and what are other people doing when their forms for updates and inserts are the same???

A good old fashioned server-side include (<!-- #Include File="UpdateInsertTemplate.inc" -->) will probably work but I'd much rather use an ascx so that I can associate code with the ascx instead of with the aspx that calls the include files.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 25-May-2007 10:48:35   

Of course this won't work. User controls are atomic units. If you want to save yourself time by placing repetitive HTML in controls, that's great, but realize that for ASP.NET they're atomic which means what you define INSIDE a control has to be used INSIDE that control, i.e. you can't simply refer from markup in the page to a control A in a usercontrol. This is also true vice versa: the usercontrol cant simply automatically bind to a datasourcecontrol in the page.

If you simply want to share snippets of the HTML among pages use include files, otherwise use controls which have their own datasourcecontrol and which are atomic.

Frans Bouma | Lead developer LLBLGen Pro
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 25-May-2007 12:10:45   

I ran across this post http://www.velocityreviews.com/forums/t113518-using-a-user-control-as-edititemtemplate-in-a-datalist.html and thought it was possible.

What really confuses me is that the user control DOES have access to the container items since it properly populates its individual controls (textboxes) with the field values that I bind. So why can't it do the binding the other way (persist to db)?

(I tried the include file but that didn't work either since it produces duplicate server control IDs on the same aspx page which the compiler complained about)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 25-May-2007 19:57:33   

Yes, but asp.net treats the controls as separate objects. The formview sees the USERcontrol as a control similar to the textbox. So you can for example <%# Bind...%> stuff to the USER control's properties but not to the textboxes IN that user control FROM the page.

Frans Bouma | Lead developer LLBLGen Pro
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 28-May-2007 19:44:46   

So you can for example <%# Bind...%> stuff to the USER control's properties but not to the textboxes IN that user control FROM the page

But I DO bind controls WITHIN the user control to the containing page's datasource. I have a user control which contains some text boxes and some RadEditor controls (from Telerik) and I populate them with <%# Bind("fieldname") %> calls. The user control markup in the formview template has NO bind code. It's as if the Bind call is smart enough to recursively search through parent containers until it finds a dataitem to bind to during the SELECT. In this case, its as if the Bind call looks to the parent container (the user control) and finds no datasource and so goes to the 'grandparent' (the formview's item template) and finds the datasource there for me.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 28-May-2007 20:35:13   

Emmanuel wrote:

So you can for example <%# Bind...%> stuff to the USER control's properties but not to the textboxes IN that user control FROM the page

But I DO bind controls WITHIN the user control to the containing page's datasource. I have a user control which contains some text boxes and some RadEditor controls (from Telerik) and I populate them with <%# Bind("fieldname") %> calls. The user control markup in the formview template has NO bind code. It's as if the Bind call is smart enough to recursively search through parent containers until it finds a dataitem to bind to during the SELECT. In this case, its as if the Bind call looks to the parent container (the user control) and finds no datasource and so goes to the 'grandparent' (the formview's item template) and finds the datasource there for me.

What you describe is what doesn't work. A control is a separate object on the page, it's not a HTML snippet. The datasource is bound to the formview, the html inside the formview refers to the current row the formview refers to.

In ANY case: it's an ASP.NET issue, not an llblgen pro issue. The datasource control is bound to the formview, not to any textboxes. The formview HTML refers to fields in the current row of the datasource as pointed to by the formview, which you do with the Bind directive. I can't imagine how what you want to do and what you described above would work, as it would kill the complete control==separate object approach of ASP.NET 2.0.

The binding of the datasource is between datasourcecontrol and formview object. Your problem is with formview <-> asp.net control, not with a datasource control related matter. I'm sorry, but I can't help you, as it's an ASP.NET control-usage issue, not something related to a datasourcecontrol (I would be VERY surprised if objectdatasource does work in this scenario for example).

Frans Bouma | Lead developer LLBLGen Pro
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 29-May-2007 19:28:43   

OK. Sorry to bother you with this given that it is an ASP.NET issue (I was aware of that).