?
Solved

Formview inside a repeater control

Posted on 2009-04-06
7
Medium Priority
?
2,358 Views
Last Modified: 2013-11-08
I want to nest formviews (have them always be in edit mode) in a repeater control.  Is this possible?  If so, how can I do it?

ie:  My repeater will pull all orders a customer has.  Inside that repeater, I will get the detail of each order and display it in a formview.
0
Comment
Question by:cartch2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:NazoUK
ID: 24085502
Sounds doable although I imagine for more than a handful of records the page will become very bloated.

Probably the easiest way to do it would be to have your formview hooked up to a data source control such as sqldatasource or objectdatasource (depending on where you're getting the data from), which is also in the ItemTemplate of the Repeater. Have a select parameter that corresponds to the primary key of the record in the formview.
Then you'd need to handle the ItemDataBound event of the Repeater to set the parameter of this datasource for each row.
You'd also need to handle the formview events to stop it going into readonly mode after they've updated a record.

This probably sounds a bit vague but I could try to flesh it out a bit for you if you need it.
0
 

Author Comment

by:cartch2008
ID: 24086484
Thanks NazoUK...I'm quite new to asp.net...I have been developing in classic asp for about 10 years though.  There would't be more than 10 records on the page.  Any way you can sketch it out a bit for me?

thanks
0
 
LVL 15

Accepted Solution

by:
NazoUK earned 2000 total points
ID: 24087676
Sure thing, I knocked up a quick working demo. My db has a 'Person' table with fields PersonID, FirstName and LastName.
I have a connectionstring called MyConnectionString defined in the web.config.

I'm not sure how much you do / don't know so I added some comments in the VB code but if there are any parts you don't understand then fire away.
File RepeaterForm.aspx
 
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="RepeaterForm.aspx.vb" Inherits="RepeaterForm" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>FormView in Repeater</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater runat="server" ID="rptPeople" OnItemDataBound="rptPeople_ItemDataBound" DataSourceID="dsPersonList">
            <ItemTemplate>
                <asp:FormView runat="server" ID="fvPerson" DataKeyNames="PersonID" DefaultMode="Edit" 
                OnModeChanging="fvPerson_ModeChanging" DataSourceID="dsPerson">
                    <EditItemTemplate>
                        FirstName: <asp:TextBox runat="server" ID="txtFirstName" Text='<%#Bind("FirstName") %>' /><br />
                        LastName: <asp:TextBox runat="server" ID="txtLastName" Text='<%#Bind("LastName") %>' /><br />
                        <asp:Button runat="server" CommandName="Update" Text="Update" />
                        <hr />
                    </EditItemTemplate>
                </asp:FormView>
                <asp:SqlDataSource runat="server" ID="dsPerson"
                SelectCommand="SELECT PersonID, FirstName, LastName FROM dbo.Person WHERE PersonID=@PersonID"
                UpdateCommand="UPDATE Person SET FirstName = @FirstName, LastName=@LastName WHERE PersonID = @PersonID"
                ConnectionString="<%$ConnectionStrings:MyConnectionString%>"
                >
                    <SelectParameters>
                        <asp:Parameter Name="PersonID" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </ItemTemplate>
        </asp:Repeater>
        <asp:SqlDataSource runat="server" ID="dsPersonList" 
        SelectCommand="SELECT PersonID FROM dbo.Person"
        ConnectionString="<%$ConnectionStrings:MyConnectionString%>" />
    </div>
    </form>
</body>
</html>
 
File RepeaterForm.aspx.vb
 
Option Strict On
Imports System.Data
 
Partial Class RepeaterForm
    Inherits System.Web.UI.Page
 
    Protected Sub rptPeople_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            'The data for each repeater row can be found in e.Item.DataItem
            'Because I am using sqldatasource this will be of type DataRowView so we cast it to this type to work with it
            Dim row As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
            'Get the personID for this row
            Dim personID As Integer = CInt(row.Row("PersonID"))
 
            'We can use e.Item.FindControl to find the formview and sqldatasource in each row.            
            Dim fv As FormView = DirectCast(e.Item.FindControl("fvPerson"), FormView)
            Dim ds As SqlDataSource = DirectCast(e.Item.FindControl("dsPerson"), SqlDataSource)
 
            'Set the Parameter and databind the formview
            ds.SelectParameters("PersonID").DefaultValue = personID.ToString()
            fv.DataBind()
        End If
        
 
    End Sub
 
    Protected Sub fvPerson_ModeChanging(ByVal sender As Object, ByVal e As FormViewModeEventArgs)
        'Always prevent a mode change
        e.Cancel = True
    End Sub
End Class

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:cartch2008
ID: 24100785
This is great!  Thank you.  What if I wanted to reference the txtFirstName control in the databound event of the formview?
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 24105243
Similar principle. Add OnDataBound="fv_DataBound" to the FormView tag and then the code in the snippet
Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        'sender is the FormView that is being databound
        Dim fv As FormView = CType(sender, FormView)
 
        'This seems to get called twice, once with no data (in which case no controls are created) and once with the data. 
        'We are only interested in the case with data.
        If fv.DataItem IsNot Nothing Then
            Dim txtFirstName As TextBox = CType(fv.FindControl("txtFirstName"), TextBox)
            Dim txtLastName As TextBox = CType(fv.FindControl("txtLastName"), TextBox)
 
            'These textbox variables now point to the 2 textboxes in the Formview
 
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:cartch2008
ID: 31567209
This was the best response I have ever received from anyone on experts exhange.  NazoUK has gone above and beyond expectations.  I can't think him/her enough.
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 24114691
Thanks for the kind comments. I appreciate it :-)
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question