0
votes

I have DetailsView Template control containing a Label.

<ItemTemplate>
<asp:Label ID="Label_AssemblyPart" runat="server" Text='<%# Bind("AssemblyPart") %>'></asp:Label>
</ItemTemplate>

If I bind an SQLDataSource to the DetailsView in ASP Markup the DetailsView1_OnDataBound routine does not fire on Page_Load which is the correct behavior.

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:inventory_v2ConnectionString %>"
        SelectCommand="SELECT   Part_Catalog.ID,
                                Part_Catalog.OEMPartCode,
                                Part_Catalog.PartCode2,
                                Part_Catalog.UsedByOEM,
                                Models_OEMs.Manufacturer AS JoinedUsedByOEM,
                                Part_Catalog.ItemType,                                    
                                Part_ItemTypes.Type AS JoinedItemType, 
                                Part_Catalog.GroupType,
                                Part_Groups.GroupType AS GroupTypeText,
                                Part_Groups.GroupType + ' - ' + Part_Groups.GroupName AS JoinedGroupType,                                    
                                Part_Catalog.PartDesc,
                                Part_Catalog.PartComment,
                                Part_Catalog.PartCount, 
                                Part_Catalog.PartMin,
                                Part_Catalog.PartActive,
                                Part_Catalog.MFRPartNumber,
                                Part_Catalog.PartCapacity,
                                Part_Catalog.PreTurnRequired,
                                Part_Catalog.AssemblyPart,                                     
                                Part_Catalog.PartImage,
                                Part_Catalog.PartImage2,
                                Part_Catalog.NonInventoryPart,
                                Part_Catalog.CreatedByUser,
                                Part_Catalog.LastModifiedDate                                    
                           FROM Part_Catalog INNER JOIN
                                 Models_OEMs ON Part_Catalog.UsedByOEM = Models_OEMs.ID INNER JOIN
                                 Part_Groups ON Part_Catalog.GroupType = Part_Groups.ID INNER JOIN
                                 Part_ItemTypes ON Part_Catalog.ItemType = Part_ItemTypes.ID                                     
                          WHERE (Part_Catalog.ID = @ID)"
        OnUpdating="SqlDataSource2_Updating"
        DeleteCommand="DELETE FROM [Part_Catalog] WHERE [ID] = @ID"
        InsertCommand=""
        UpdateCommand="dbo.Procedure_UpdatePartCatalog" UpdateCommandType="StoredProcedure">

        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="ID" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>

If I set the Configuration string for that markup based datasource in code in Page_Load with :

        if (GridView1.SelectedIndex != -1)
        {
            DetailsView1.Visible = true;
            DetailsView1.Focus();
            SqlDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings[conString].ConnectionString;
        }

the DetailsView1_OnDataBound event fires and returns a null error when it looks at Label_AssemblyPart.Text.

The DetailsView_OnDataBound event shouldn't fire at all because on the first page_load the DetailsView shouldn't have any data bound to it. There is a Gridview that loads the DetailsView on the GridView_SelectedIndex_Changed event and that only occurs on a PostBack.

It looks like the act of assigning a connect String to a DataSource alone is causing a databind.

1
You're not calling DetailsView1.DataBind() anywhere? - Rick S
No. Part of the issue is that the DetailsView shouldn't do anything on page load. The DetailsView is only activated after a GridView index is selected. I seem to be getting into the detailsview events without ever entering into the Gridview ones. Annother way to look at it is the If Condition above is never even hit on Page_Load unless it is a postback. - user1431356
I'm curious what happens if you put an <emptydatatemplate> in your detailview. - Rick S
Well, that seemed to help a little. Now it's dumping because it can't find a control from the Insert Template. Could not find control 'DDL_GroupType_I' in ControlParameter 'categoryParam'. - user1431356

1 Answers

0
votes

The fix turned out to be setting an Onload event for each EditItemTemplate control that would set the SQLDataSource connection string.

        <EditItemTemplate>
<asp:DropDownList ID="DDL_ItemType_E" runat="server"
DataSourceID="SQL_DS_ItemType_E" DataTextField="Type" DataValueField="ItemTypeID"
Height="25px" Width="150px" AutoPostBack="True"
OnDataBound="DDL_ItemType_E_DataBinding" OnLoad="DDL_ItemType_E_Load" SelectedValue='<%# Bind("ItemType") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SQL_DS_ItemType_E" runat="server"                                        
SelectCommand="SELECT [ID] AS ItemTypeID, [Type] FROM [Part_ItemTypes]"></asp:SqlDataSource>
</EditItemTemplate>



        protected void DDL_ItemType_E_Load(object sender, EventArgs e)
{
    SqlDataSource SQL_DS_ItemType_E = (SqlDataSource)DetailsView1.FindControl("SQL_DS_ItemType_E");
    SQL_DS_ItemType_E.ConnectionString = ConfigurationManager.ConnectionStrings[Master.conString].ConnectionString;
}