简体   繁体   English

如何通过子 ReorderList 中的代码隐藏更改 SqlDataSource 的 ConnectionString 属性?

[英]How to change ConnectionString property of SqlDataSource by Code Behind which is in Child ReorderList?

I have two ReorderList One is a parent and other is its child.我有两个ReorderList一个是父级,另一个是它的子级。 I want to change dynamically the ConnectionString property of SqlDataSource through code behind but I am unable to change ConnectionString property of Child ReorderList even I tried OnItemDataBound and tried to find the control and change its property but could not.我想通过后面的代码动态更改SqlDataSourceConnectionString属性,但我无法更改 Child ReorderList ConnectionString属性,即使我尝试了OnItemDataBound并试图找到控件并更改其属性但无法更改。 Here is a sample of code which I am using:这是我正在使用的代码示例:

     <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>

I can change the ConnectionString of SqlDataSource1 through code behind and its accessible but SqlDataSource2 is not accessible.我可以通过后面的代码更改SqlDataSource1ConnectionString及其可访问但SqlDataSource2不可访问。 Anyone kindly give me a clue how to achieve this as I want to assign Connection String from code behind.任何人都请给我一个如何实现这一点的线索,因为我想从后面的代码中分配连接字符串。

Since your control uses the ASP.NET Expressions mechanism to retrieve the connection string from the config file由于您的控件使用 ASP.NET 表达式机制从配置文件中检索连接字符串

https://msdn.microsoft.com/en-us/library/d5bd1tad.aspx https://msdn.microsoft.com/en-us/library/d5bd1tad.aspx

you should be able to replace你应该可以更换

 <%$ ConnectionStrings:MyConnectionString %>

with your own custom expression provider使用您自己的自定义表达式提供程序

https://msdn.microsoft.com/en-us/library/system.web.compilation.expressionbuilder.aspx 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/ http://haacked.com/archive/2006/11/29/Express_Yourself_With_Custom_Expression_Builders.aspx/

This way you could have total imperative control over what actual value is provided to the context where the value is retrieved通过这种方式,您可以对提供给检索值的上下文的实际值进行完全命令式控制

 <%$ CustomExpressionProvider:MyConnectionString %>

In your ItemDataBound method, you can access the SqlDataSource :在您的ItemDataBound方法中,您可以访问 SqlDataSource :

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

So you can change its ConnectionString attribute.所以你可以改变它的ConnectionString属性。

Then, find your ReorderList control using the same way ( e.Item.FindControl ), and set its DataSource attribute to sds .然后,使用相同的方式( e.Item.FindControl )找到您的 ReorderList 控件,并将其DataSource属性设置为sds

myReorderList.DataSource = sds;

Last, don't forget to call the DataBind() method of your RedorderList.最后,不要忘记调用 RedorderList 的DataBind()方法。

myReorderList.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM