codefly codefly - 2 months ago 12
ASP.NET (C#) Question

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. 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. 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. Anyone kindly give me a clue how to achieve this as I want to assign Connection String from code behind.

Answer

In your ItemDataBound method, you can access the SqlDataSource :

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

So you can change its ConnectionString attribute.

Then, find your ReorderList control using the same way (e.Item.FindControl), and set its DataSource attribute to sds.

myReorderList.DataSource = sds;

Last, don't forget to call the DataBind() method of your RedorderList.

myReorderList.DataBind();