如何通过在Child ReorderList中的代码更改SqlDataSource的ConnectionString属性

本文关键字:SqlDataSource ConnectionString 属性 代码 Child ReorderList 何通过 | 更新日期: 2023-09-27 18:07:10

我有两个ReorderList一个是父,另一个是它的子。我想通过后面的代码动态改变SqlDataSourceConnectionString属性,但我无法改变ReorderListConnectionString属性,即使我尝试了OnItemDataBound,并试图找到控件并改变其属性,但不能。下面是我使用的代码示例:

     <div class="reorderListDemo" style="width: 100%">
    <cc1:ReorderList ID="ReorderList1" runat="server" DataSourceID="SqlDataSource1" DataKeyField="RecordingFilterId" AllowReorder="true"
        SortOrderField="Priority" PostBackOnReorder="False">
        <ItemTemplate>
            <table style="width: 100%" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="6%" style="padding-left:5px;padding-top:3px;">
                        <a href="javascript:switchViews('divRF<%# Eval("RecordingFilterId") %>', 'dragHandle<%# Eval("RecordingFilterId") %>');">
                            <img id="imgdivRF<%# Eval("RecordingFilterId") %>" border="0" src="Images/expand.png" />
                        </a>
                    </td>
                    <td width="34%">
                        <asp:Label ID="Label7" runat="server" Text='<%# Eval("Name") %>' meta:resourcekey="Label7Resource1" /></td>
                </tr>
                    <tr>
                        <td colspan="6" width="100%" style="padding-right:10px;" >
                            <div id="divRF<%# Eval("RecordingFilterId") %>" style="display: none; width: 99%;">
                                <table style="width: 100%;" cellspacing="0" cellpadding="0" border="0" >
                                        <tbody>
                                            <tr>
                                                <td style="color: white; width: 15%; padding-left: 30px;" class="topleft" align="left">
                                                    <div>
                                                        <b>
                                                            <asp:Label ID="Label3" runat="server" Text="Rule" meta:resourcekey="Label3Resource2"></asp:Label></b>
                                                    </div>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                <div class="reorderListDemo" style="margin-left: 0px;width: 97%;">
                                    <cc1:ReorderList ID="ReorderList2" runat="server" PostBackOnReorder="False" CallbackCssStyle="callbackStyle"
                                        AllowReorder="True" DataKeyField="RuleId" SortOrderField="Priority"
                                        >
                                        <ItemTemplate>                                                                
                                                <table style="width: 100%" cellpadding="0" cellspacing="0" border="0">
                                                    <tr>
                                                        <td align="left" style="padding-left: 10px; width: 15%;">
                                                            <asp:Label ID="Label6" runat="server" ToolTip='<%# Eval("RuleName") %>' Text='<%# Eval("RuleName").ToString().Length > 14 ? Eval("RuleName").ToString().Substring(0,12) + ".." : Eval("RuleName").ToString() %>' />
                                                        </td>
                                                    </tr>
                                                </table>                                                              
                                        </ItemTemplate>
                                        <ReorderTemplate>
                                            <asp:Panel ID="Panel2" runat="server" CssClass="reorderCue" meta:resourcekey="Panel2Resource1">
                                            </asp:Panel>
                                        </ReorderTemplate>
                                        <DragHandleTemplate>
                                            <div class="dragHandleChild" id="dragHandle<%# Eval("RecordingFilterId") %>">
                                            </div>
                                        </DragHandleTemplate>
                                    </cc1:ReorderList>
                                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
                                        ProviderName="<%$ ConnectionStrings:MyConnectionString.ProviderName %>"
                                        OnDeleted="OnRuleDeleted" SelectCommand="SELECT RuleId,RecordingFilterId,RuleName,RecordingAction,RecordingCondition,ExtensionValue,Priority,CallType FROM rules  WHERE ([RecordingFilterId] =@RecordingFilterId)  and RuleName &lt;&gt; '' ORDER BY [Priority] asc"
                                        UpdateCommand="UPDATE [Rules] SET [Priority] = @Priority WHERE RuleId = @original_RuleID"
                                        DeleteCommand="exec DeleteRule @original_RuleID" OldValuesParameterFormatString="original_{0}">
                                        <DeleteParameters>
                                            <asp:Parameter Name="original_RuleID" />
                                        </DeleteParameters>
                                        <SelectParameters>
                                            <asp:ControlParameter ControlID="lblCategoryName" Name="RecordingFilterId" PropertyName="Text"
                                                Type="String" />
                                        </SelectParameters>
                                        <UpdateParameters>
                                            <asp:Parameter Name="Priority" Type="Int32" />
                                            <asp:Parameter Name="original_RuleID" Type="String" />
                                        </UpdateParameters>
                                    </asp:SqlDataSource>
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
        </ItemTemplate>
        <ReorderTemplate>
            <asp:Panel ID="Panel2" runat="server" CssClass="reorderCue" >
            </asp:Panel>
        </ReorderTemplate>
        <DragHandleTemplate>
            <div class="dragHandle" id="dragHandle<%# Eval("RecordingFilterId") %>">
            </div>
        </DragHandleTemplate>
    </cc1:ReorderList>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    ProviderName="<%$ ConnectionStrings:MyConnectionString.ProviderName %>"
    SelectCommand="SELECT RecordingFilterId,Name,Description,SystemFilter,Priority FROM recordingfilters WHERE SystemFilter='1' AND STATUS =1  order BY Priority"
    UpdateCommand="UPDATE [recordingfilters] SET [Priority] = @Priority WHERE RecordingFilterId = @original_RecordingFilterId"
    OnDeleted="OnFilterDeleted" DeleteCommand="exec DeleteRecordingFilter @original_RecordingFilterId"
    OldValuesParameterFormatString="original_{0}">
    <DeleteParameters>
        <asp:Parameter Name="original_RecordingFilterId" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="Priority" Type="Int32" />
        <asp:Parameter Name="original_RecordingFilterId" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

我可以通过后面的代码改变SqlDataSource1ConnectionString,它是可访问的,但SqlDataSource2是不可访问的。任何人都给我一个线索如何实现这一点,因为我想从后面的代码分配连接字符串。

如何通过在Child ReorderList中的代码更改SqlDataSource的ConnectionString属性

. NET Expressions机制从配置文件

中检索连接字符串https://msdn.microsoft.com/en-us/library/d5bd1tad.aspx

你应该能够替换

 <%$ ConnectionStrings:MyConnectionString %>

与您自己的自定义表达式提供程序

https://msdn.microsoft.com/en-us/library/system.web.compilation.expressionbuilder.aspx

http://haacked.com/archive/2006/11/29/Express_Yourself_With_Custom_Expression_Builders.aspx/

通过这种方式,您可以完全命令式地控制向检索值的上下文中提供的实际值

 <%$ CustomExpressionProvider:MyConnectionString %>

ItemDataBound方法中,您可以访问SqlDataSource:

sds = (SqlDataSource)(e.Item.FindControl("SqlDataSource2"));

所以你可以改变它的ConnectionString属性。

然后,使用相同的方法找到ReorderList控件(e.Item.FindControl),并将其DataSource属性设置为sds

myReorderList.DataSource = sds;

最后,不要忘记调用RedorderList的DataBind()方法。

myReorderList.DataBind();