Asp.net 在CS中更改SQL数据连接的配置字符串时出现奇怪的行为

Asp.net 在CS中更改SQL数据连接的配置字符串时出现奇怪的行为,asp.net,c#-2.0,Asp.net,C# 2.0,我有包含标签的DetailsView模板控件 <ItemTemplate> <asp:Label ID="Label_AssemblyPart" runat="server" Text='<%# Bind("AssemblyPart") %>'></asp:Label> </ItemTemplate> DetailsView1_OnDataBound事件在查看Label_AssemblyPart.Text时激发并返回空错误 Detai

我有包含标签的DetailsView模板控件

<ItemTemplate>
<asp:Label ID="Label_AssemblyPart" runat="server" Text='<%# Bind("AssemblyPart") %>'></asp:Label>
</ItemTemplate>
DetailsView1_OnDataBound事件在查看Label_AssemblyPart.Text时激发并返回空错误

DetailsView\u OnDataBound事件不应该触发,因为在加载的第一页上,DetailsView不应该有任何数据绑定到它。有一个Gridview,它在Gridview\u SelectedIndex\u Changed事件上加载Details视图,并且只在回发时发生


看起来,将连接字符串单独分配给数据源的行为会导致数据绑定。

修复结果是为每个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;
}

受保护的无效DDL_ItemType_加载(对象发送方,事件参数)
{
SqlDataSource SQL_DS_ItemType=(SqlDataSource)DetailsView1.FindControl(“SQL_DS_ItemType”);
SQL\u DS\u ItemType\u E.ConnectionString=ConfigurationManager.ConnectionString[Master.conString].ConnectionString;
}

您没有在任何地方调用
DetailsView1.DataBind()
吗?没有。部分问题是DetailsView不应该在页面加载时执行任何操作。DetailsView仅在选择GridView索引后激活。我似乎进入了detailsview事件,而没有进入Gridview事件。另一种看待它的方式是,除非是回发,否则在页面加载时,上面的If条件永远不会被命中。我很好奇如果你在detailview中添加一个
,会发生什么。好吧,这似乎有点帮助。现在它正在转储,因为它无法从插入模板中找到控件。在控制参数“categoryParam”中找不到控件“DDL_GroupType_I”。
        if (GridView1.SelectedIndex != -1)
        {
            DetailsView1.Visible = true;
            DetailsView1.Focus();
            SqlDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings[conString].ConnectionString;
        }
        <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;
}