DataBound Events

Posts   
 
    
Rickster
User
Posts: 4
Joined: 20-Nov-2009
# Posted on: 21-Nov-2009 00:31:11   

I am have some issues with some bound controls and a LLBLGenProDataSource. I have a GridView that is bound to a data source (DS1) and I have a Link Button that when clicked takes 2 key fields from that bound row object and creates a new EntityCollection and binds it to another datasource (DS2). The second datasource (DS2) is bound to a FormView and opens it is Edit mode. I have DataBound event attached to the FormView that needs to get some more data from other tables for display and create some custom controls and fille them with data. When this event fires for the first time in a session the DataItem of the FormView is always null. If i fire the event the second time, it has data in it and works fine. If I edit that data and re-post the page and re-fire the event, the DataItem is null again. When the EntitiyCollection is created on DS2 it has a data element in it but when the DataBound event fires for the first time both the collection and the FormView.DataItem are null. I must be doing something wrong because I have run the CRUD demo and it worked fine. It seems tha I am dong nearly the same thing but it does not work.

Thanks

Rick

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Nov-2009 07:08:43   

Hi Rick,

  • What LLBLGen version and runtime library version are you using?
  • Please post the relevant code for the steps you are mentioning.
David Elizondo | LLBLGen Support Team
Rickster
User
Posts: 4
Joined: 20-Nov-2009
# Posted on: 23-Nov-2009 16:52:18   

Here are the versions I am using:

SD.LLBLGen.Pro.DQE.SqlServer.NET20.dll 2.6.8.1114 SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll 2.6.8.1114

I have a databound GirdView that looks like this (simplified)


<asp:GridView ID="GridInvid" runat="server" CellPadding="4" 
            DataSourceID="dsArcSgtlIndiv" ForeColor="#333333" GridLines="None" 
            AutoGenerateColumns="False" onrowdatabound="GridInvid_RowDataBound" 
            onrowcommand="GridInvid_RowCommand" onrowcreated="SgtlGrid_RowCreated" 
            EnableViewState="False">
               <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
               <Columns>
                   <asp:BoundField DataField="FullName" HeaderText="Name" 
                       SortExpression="FullName">
                       <HeaderStyle CssClass="tblHeaderStyle" />
                       <ItemStyle CssClass="tblRowStyle" />
                   </asp:BoundField>
                   <asp:BoundField DataField="MemberId" HeaderText="Mbr #" 
                       SortExpression="MemberId">
                       <HeaderStyle CssClass="tblHeaderStyle" />
                       <ItemStyle CssClass="tblRowStyle" />
                   </asp:BoundField>
                   <asp:BoundField DataField="IndivSsnId" HeaderText="SSN" 
                       SortExpression="IndivSsnId">
                       <HeaderStyle CssClass="tblHeaderStyle" />
                       <ItemStyle CssClass="tblRowStyle" />
                   </asp:BoundField>
                   <asp:ButtonField CommandName="Change" Text="Edit" HeaderText="Edit"  
                       HeaderStyle-CssClass="tblHeaderStyle"
                       ShowHeader="True" >
                       <HeaderStyle CssClass="tblHeaderStyle" />
                   </asp:ButtonField>
                   <asp:ButtonField Text="Details" CommandName="Details" 
                       HeaderStyle-CssClass="tblHeaderStyle"
                       ShowHeader="True" HeaderText="Details" >
                       <HeaderStyle CssClass="tblHeaderStyle" />
                   </asp:ButtonField>
               </Columns>
               <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
               <PagerStyle BackColor="#D9D1B3" ForeColor="#333333" HorizontalAlign="Center" />
               <SelectedRowStyle BackColor="#f2e8da" Font-Bold="True" ForeColor="Black" />
               <HeaderStyle BackColor="#D9D1B3" Font-Bold="True" ForeColor="White" />
               <AlternatingRowStyle BackColor="#EEE3DB" />
        </asp:GridView>
.....

                <asp:FormView ID="fvEdit" runat="server" DataSourceID="dsSgtlExamStat" 
        DataKeyNames="IndivId,CymdDt" BackColor="#DEBA84" BorderColor="#DEBA84" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
            DataMember="DefaultView" GridLines="Both" Width="683px" 
            DefaultMode="Edit" >
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <EditItemTemplate>
                    <hr />
                    <h3>Exam Status for: <asp:Label ID="lblFullName" runat="server" Text="" /></h3>
                    <table border="0">
                    <tr>
                        <td>Order #:</td>
                        <td><asp:Label ID="lblOrderNum" runat="server" Text='<%# Bind("OrderNum") %>' /></td>
                    </tr>
                    <tr>
                         <td>Status:</td>
                        <td><asp:DropDownList ID="cboStatus" runat="server" >
                            <asp:ListItem Value=" "> </asp:ListItem>
                            <asp:ListItem Value="I">Issued</asp:ListItem>
                            <asp:ListItem Value="D">Declined</asp:ListItem>
                            <asp:ListItem Value="O">Open</asp:ListItem>
                            <asp:ListItem Value="P">Pending</asp:ListItem>
                            <asp:ListItem Value="C">Completed</asp:ListItem>
                        </asp:DropDownList> </td>
                    </tr>
                    <tr>
                        <td>Exam Status:</td>
                        <td><asp:DropDownList ID="cboExamStatus" runat="server" >
                            <asp:ListItem Value=" "> </asp:ListItem>
                            <asp:ListItem Value="S">Submitted</asp:ListItem>
                            <asp:ListItem Value="R">Recieved</asp:ListItem>
                            <asp:ListItem Value="N">No Exam</asp:ListItem>
                            <asp:ListItem Value="X">Order</asp:ListItem>
                        </asp:DropDownList> </td>
                    </tr>
                    <tr>
                        <td>Service Code:</td>
                        <td><asp:DropDownList ID="cboServiceCode" runat="server" ></asp:DropDownList></td>
                    </tr>
                    <tr>
                        <td>Notes:</td>
                        <td><asp:TextBox ID="txtNotes" runat="server" MaxLength="255" Width="500px" Text='<%# Bind("Notes") %>' /></td>
                    </tr>
                    <tr>
                        <td>Coverage:</td>
                        <td><asp:Label ID="lblCoverage" runat="server" Text="" /></td>
                    </tr>
                    </table>
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Save" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <InsertItemTemplate>No record Found</InsertItemTemplate>
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#FFFBD6" Font-Bold="True" ForeColor="Black" />
        </asp:FormView>


On the change command this code is executed


protected void GridInvid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Change")
            {
                EditExam(index);
            }
        }catch 
        {}
    }

The Edit exam method looks up the datarow from the grid and binds to the FormView


private void EditExam(int index)
    {
        ArcSgtlIndivEntity enitiy = dsArcSgtlIndiv.EntityCollection[index] as ArcSgtlIndivEntity;

        PredicateExpression p = new PredicateExpression();
        p.Add(SgtlExamStatFields.IndivId == enitiy.IndivId);
        p.AddWithAnd(SgtlExamStatFields.CymdDt == enitiy.CymdDt);
        SgtlExamStatCollection exams = new SgtlExamStatCollection();
        exams.GetMulti(p);
        dsSgtlExamStat.EntityCollection = exams;
        dsSgtlExamStat.DataBind();
    }

When the FormView data bound event fires here is the code that executes:


    protected void fvEdit_DataBound(object sender, EventArgs e)
    {
        try
        {
            if (fvEdit.DataItem != null)
            {
                SgtlExamStatEntity exam = fvEdit.DataItem as SgtlExamStatEntity;

                ArcIndivEntity arcEntity = GetArcIndivEntity(exam.IndivId, exam.CymdDt);

                if (arcEntity != null)
                {
                    Label lblFullName = fvEdit.FindControl("lblFullName") as Label;
                    lblFullName.Text = String.Format("{0:s} {1:s}", arcEntity.FirstName, arcEntity.LastName);
                }

                Label lblCoverage = fvEdit.FindControl("lblCoverage") as Label;
                DropDownList cboStatus = fvEdit.FindControl("cboStatus") as DropDownList;
                DropDownList cboExamStatus = fvEdit.FindControl("cboExamStatus") as DropDownList;
                DropDownList cboServiceCode = fvEdit.FindControl("cboServiceCode") as DropDownList;

                lblCoverage.Text = String.Format("{0:c}", exam.CoverageAmount);

                cboStatus.SelectedValue = exam.StatCd;
                cboExamStatus.SelectedValue = exam.ExamStatCd;

                SgtlMedStatCollection list = new SgtlMedStatCollection();
                list.GetMulti(null);
                foreach (SgtlMedStatEntity med in list)
                {
                    cboServiceCode.Items.Add(new ListItem(med.Description, med.SrvcCd));
                }
                cboServiceCode.SelectedValue = exam.SrvcCd;
            }
        }
        catch
        { }
    }

When the EditExam method is called there is always 1 exam entity in the data source but when the databound event fires on the FormView the DataItem is null. The second time I click on the button all events fire in the same sequence but this time the data item exists where is should.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 24-Nov-2009 11:01:29   

I think you need to call DataBind() on the FormView.

private void EditExam(int index)
    {
        ArcSgtlIndivEntity enitiy = dsArcSgtlIndiv.EntityCollection[index] as ArcSgtlIndivEntity;

        PredicateExpression p = new PredicateExpression();
        p.Add(SgtlExamStatFields.IndivId == enitiy.IndivId);
        p.AddWithAnd(SgtlExamStatFields.CymdDt == enitiy.CymdDt);
        SgtlExamStatCollection exams = new SgtlExamStatCollection();
        exams.GetMulti(p);
        dsSgtlExamStat.EntityCollection = exams;
        FvEdit.DataBind();//-------------------------------------------------<<<<<
    }
Rickster
User
Posts: 4
Joined: 20-Nov-2009
# Posted on: 24-Nov-2009 16:15:48   

Thanks but I tried that and still get the same behaivor. Is it better to set the DataSourceID of the FormView in the code? Should I call DataBind on both the data source and the FormView?

Thanks

Rickster
User
Posts: 4
Joined: 20-Nov-2009
# Posted on: 24-Nov-2009 17:38:37   

Setting LivePersistence to true on the datasource seems to do the trick here. Not sure why.