|
| 1 | +--- |
| 2 | +title: Decorated GridView Is Not Bound to SqlDataSource with ControlParameter |
| 3 | +page_title: Decorated GridView Is Not Bound to SqlDataSource with ControlParameter | RadFormDecorator for ASP.NET AJAX Documentation |
| 4 | +description: Decorated GridView Is Not Bound to SqlDataSource with ControlParameter |
| 5 | +slug: formdecorator/troubleshooting/decorated-gridview-is-not-bound-to-sqldatasource-with-controlparameter |
| 6 | +tags: decorated,gridview,is,not,bound,to,sqldatasource,with,controlparameter |
| 7 | +published: True |
| 8 | +position: 2 |
| 9 | +--- |
| 10 | + |
| 11 | +# Decorated GridView Is Not Bound to SqlDataSource with ControlParameter |
| 12 | + |
| 13 | +This help article offers a solution to an issue where a decorated asp:GridView by RadFormDecorator cannot be bound to the SqlDataSource with a ControlParameter on initial page load. |
| 14 | + |
| 15 | +**Problem:** |
| 16 | + |
| 17 | +The asp:GridView cannot be bound on initial page load when it is decorated by RadFormDecorator and the GridView is bound to the SqlDataSource with a ControlParameter. **Example 1** shows how to reproduce the issue. |
| 18 | + |
| 19 | +>caption **Example 1**: Decorated asp:GridView is not visible on initial page load when it is bound to the SqlDataSource with ControlParameter. |
| 20 | +
|
| 21 | +**ASP.NET** |
| 22 | + |
| 23 | +<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" DecoratedControls="All" /> |
| 24 | + |
| 25 | +<asp:DropDownList ID="Dropdownlist1" runat="server" DataSourceID="DropDownListDataSource" AutoPostBack="true" DataTextField="CompanyName" DataValueField="CustomerID"> |
| 26 | +</asp:DropDownList> |
| 27 | +<asp:SqlDataSource runat="server" ID="DropDownListDataSource" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 28 | +ProviderName="System.Data.SqlClient" SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" |
| 29 | +OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues" /> |
| 30 | + |
| 31 | + |
| 32 | +<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataKeyNames="OrderID" DefaultMode="ReadOnly" |
| 33 | +DataSourceID="GridViewDataSource" Width="300" Style="float: left;"> |
| 34 | +</asp:GridView> |
| 35 | +<asp:SqlDataSource runat="server" ID="GridViewDataSource" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 36 | +ProviderName="System.Data.SqlClient" SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID],[OrderDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)" |
| 37 | +OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues"> |
| 38 | +<SelectParameters> |
| 39 | +<asp:ControlParameter ControlID="Dropdownlist1" Name="CustomerID" PropertyName="SelectedValue" |
| 40 | +Type="String"></asp:ControlParameter> |
| 41 | +</SelectParameters> |
| 42 | +</asp:SqlDataSource> |
| 43 | + |
| 44 | + |
| 45 | +**Cause:** |
| 46 | + |
| 47 | +In order to decorate all of the controls on the page, the RadFormDecorator decorates the children controls of the complex controls as well (i.e., the RadFormDecorator iterates through the controls' collections). |
| 48 | + |
| 49 | +There is, however, a binding issue with the GridView when the SqlDataSource is used with a ControlParameter and at the same time, the GridView's collection is accessed from the code behind. This issue can be easily reproduced on a page with no Telerik UI for ASP.NET AJAX controls and is shown in **Example 2**. The problem also affects the scenario with the RadFormDecorator from **Example 1**. |
| 50 | + |
| 51 | +>caption **Example 2**: asp:GridView cannot be bound to the SqlDataSource with a ControlParameter when the GridView's collection is accessed from the code behind. |
| 52 | +
|
| 53 | +**ASP.NET** |
| 54 | + |
| 55 | +<asp:DropDownList ID="Dropdownlist1" runat="server" DataSourceID="DropDownListDataSource" AutoPostBack="true" DataTextField="CompanyName" DataValueField="CustomerID"> |
| 56 | +</asp:DropDownList> |
| 57 | +<asp:SqlDataSource runat="server" ID="DropDownListDataSource" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 58 | +ProviderName="System.Data.SqlClient" SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" |
| 59 | +OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues" /> |
| 60 | + |
| 61 | + |
| 62 | +<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataKeyNames="OrderID" DefaultMode="ReadOnly" |
| 63 | +DataSourceID="GridViewDataSource" Width="300" Style="float: left;"> |
| 64 | +</asp:GridView> |
| 65 | +<asp:SqlDataSource runat="server" ID="GridViewDataSource" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 66 | +ProviderName="System.Data.SqlClient" SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID],[OrderDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)" |
| 67 | +OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues"> |
| 68 | +<SelectParameters> |
| 69 | +<asp:ControlParameter ControlID="Dropdownlist1" Name="CustomerID" PropertyName="SelectedValue" |
| 70 | +Type="String"></asp:ControlParameter> |
| 71 | +</SelectParameters> |
| 72 | +</asp:SqlDataSource> |
| 73 | + |
| 74 | +**C#** |
| 75 | + |
| 76 | +protected void Page_PreRender(object sender, EventArgs e) |
| 77 | +{ |
| 78 | +var c = GridView1.Controls; |
| 79 | +} |
| 80 | + |
| 81 | +**VB** |
| 82 | + |
| 83 | +Protected Sub Page_PreRender(sender As Object, e As EventArgs) |
| 84 | +Dim c = GridView1.Controls |
| 85 | +End Sub |
| 86 | + |
| 87 | + |
| 88 | +**Solution:** |
| 89 | + |
| 90 | +There are a few options you can choose from in order to handle the scenario described above. |
| 91 | + |
| 92 | +* Bind the DropDownList's data from the code behind instead of declaring the DataSourceID property of the DropDownList. |
| 93 | + |
| 94 | +>caption **Example 3**: Binding DropDownList's data from the code behind instead of declaring its DataSourceID property. |
| 95 | + |
| 96 | +**ASP.NET** |
| 97 | + |
| 98 | +<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" DecoratedControls="All" /> |
| 99 | + |
| 100 | +<asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true" DataTextField="CompanyName" DataValueField="CustomerID"> |
| 101 | +</asp:DropDownList> |
| 102 | +<asp:SqlDataSource runat="server" ID="DropDownListDataSource" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 103 | +ProviderName="System.Data.SqlClient" SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" |
| 104 | +OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues" /> |
| 105 | + |
| 106 | + |
| 107 | +<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataKeyNames="OrderID" DefaultMode="ReadOnly" |
| 108 | +DataSourceID="GridViewDataSource" Width="300" Style="float: left;"> |
| 109 | +</asp:GridView> |
| 110 | +<asp:SqlDataSource runat="server" ID="GridViewDataSource" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 111 | +ProviderName="System.Data.SqlClient" SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID],[OrderDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)" |
| 112 | +OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues"> |
| 113 | +<SelectParameters> |
| 114 | +<asp:ControlParameter ControlID="Dropdownlist1" Name="CustomerID" PropertyName="SelectedValue" |
| 115 | +Type="String"></asp:ControlParameter> |
| 116 | +</SelectParameters> |
| 117 | +</asp:SqlDataSource> |
| 118 | + |
| 119 | +**C#** |
| 120 | + |
| 121 | +protected void Page_Init(object sender, EventArgs e) |
| 122 | +{ |
| 123 | +DataSourceSelectArguments args = new DataSourceSelectArguments(); |
| 124 | +DataView view = (DataView)DropDownListDataSource.Select(args); |
| 125 | +DataTable dt = view.ToTable(); |
| 126 | + |
| 127 | +Dropdownlist1.DataSource = dt; |
| 128 | +Dropdownlist1.DataBind(); |
| 129 | +} |
| 130 | + |
| 131 | +**VB** |
| 132 | + |
| 133 | +Protected Sub Page_Init(sender As Object, e As EventArgs) |
| 134 | +Dim args As New DataSourceSelectArguments() |
| 135 | +Dim view As DataView = DirectCast(DropDownListDataSource.[Select](args), DataView) |
| 136 | +Dim dt As DataTable = view.ToTable() |
| 137 | + |
| 138 | +Dropdownlist1.DataSource = dt |
| 139 | +Dropdownlist1.DataBind() |
| 140 | +End Sub |
| 141 | + |
| 142 | +* Skip the following controls form decoration - GridFormDetailsViews, LoginControls, Textbox and ValidationSummary: |
| 143 | + |
| 144 | +>caption **Example 4**: Skip the GridFormDetailsViews, LoginControls, Textbox and ValidationSummary controls from decoration. |
| 145 | + |
| 146 | +**ASP.NET** |
| 147 | + |
| 148 | +<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" ControlsToSkip="GridFormDetailsViews,LoginControls,Textbox,ValidationSummary" /> |
| 149 | + |
| 150 | +* Set the DataSourceID property of the DropDownList in the Page_Init event. This approach, however, will force the DropDownList to rebind itself, which may lead to performance issues for large data sources. |
| 151 | + |
| 152 | +**C#** |
| 153 | + |
| 154 | +protected void Page_Init(object sender, EventArgs e) |
| 155 | +{ |
| 156 | +Dropdownlist1.DataSourceID = "DropDownListDataSource"; |
| 157 | +Dropdownlist1.DataBind(); |
| 158 | +} |
| 159 | + |
| 160 | +**VB** |
| 161 | + |
| 162 | +Protected Sub Page_Init(sender As Object, e As EventArgs) |
| 163 | +Dropdownlist1.DataSourceID = "DropDownListDataSource" |
| 164 | +Dropdownlist1.DataBind() |
| 165 | +End Sub |
| 166 | + |
| 167 | + |
| 168 | +# See Also |
| 169 | + |
| 170 | + * [RadFormDecorator Input Appearance Is Not Updated When Disabled In Internet Explorer]({%slug formdecorator/troubleshooting/input-appearance-is-not-updated-when-disabled-in-internet-explorer%}) |
| 171 | + |
| 172 | + * [RadFormDecorator Integration With RadControls]({%slug formdecorator/integration-with-radcontrols%}) |
| 173 | + |
| 174 | + * [RadFormDecorator Integration With Standard Controls]({%slug formdecorator/integration-with-standard-controls%}) |
| 175 | + |
| 176 | + * [RadFormDecorator Overview]({%slug formdecorator/overview%}) |
0 commit comments