我有包含Label的DetailsView Template控件。
<ItemTemplate>
<asp:Label ID="Label_AssemblyPart" runat="server" Text='<%# Bind("AssemblyPart") %>'></asp:Label>
</ItemTemplate>
如果我将SQLDataSource绑定到ASP标记中的DetailsView,则DetailsView1_OnDataBound例程不会触发Page_Load,这是正确的行为。
<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>
如果我在Page_Load中的代码中为基于标记的数据源设置配置字符串:
if (GridView1.SelectedIndex != -1)
{
DetailsView1.Visible = true;
DetailsView1.Focus();
SqlDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings[conString].ConnectionString;
}
DetailsView1_OnDataBound事件在查看Label_AssemblyPart.Text时会触发并返回null错误。
DetailsView_OnDataBound事件根本不应该触发,因为在第一个page_load上,DetailsView不应该绑定任何数据。有一个Gridview在GridView_SelectedIndex_Changed事件上加载DetailsView,并且只在PostBack上发生。
看起来将连接字符串分配给DataSource的行为导致了数据绑定。
答案 0 :(得分:0)
修复程序结果是为每个EditItemTemplate控件设置一个Onload事件,该控件将设置SQLDataSource连接字符串。
<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;
}