The idea here is that the DataSourceID can vary based on the value of the checkbox. The above code will actually run without error, but if I set breakpoints in the Page_Load, Page_Init and Page_PreRender events, I can see that GridView1.DataSourceID is
a blank string.
I have tried several other methods, and nothing seems to work. For example:
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID '<% if (CheckBox1.Checked == true) { %>SqlDataSource1<% } else { %>SqlDataSource2<% } %>' could not be found.
In fact I received the above error several times based on a few different ways of coding it. Why is it processing all my <% %> code as though that code were the DataSourceID specified, instead of first pre-processing the <% %> code and then using the result
from that to plug into the GridView1 tag definition?
Please keep in mind, I do NOT want to do this with a code-behind. I know it can be done that way, but I want to know how to do it without code-behind (if there is indeed a way?) I don't mind using an IF statement or a ternary operator, or some other way.
jordantx
Member
10 Points
9 Posts
Dynamic DataSourceID without code-behind
Apr 09, 2012 03:38 PM|LINK
How can I specify a dynamic DataSourceID to a control such as GridView, using a conditional statement in the ASPX code ??
Let's say I have these controls:
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" />
<asp:GridView ID="GridView1" runat="server"
DataSourceID = "<%# (CheckBox1.Checked == true ? SqlDataSource1.ID : SqlDataSource2.ID) %>" />
The idea here is that the DataSourceID can vary based on the value of the checkbox. The above code will actually run without error, but if I set breakpoints in the Page_Load, Page_Init and Page_PreRender events, I can see that GridView1.DataSourceID is a blank string.
I have tried several other methods, and nothing seems to work. For example:
<asp:GridView ID="GridView1" runat="server"
DataSourceID = "<% if (CheckBox1.Checked == true) { %>SqlDataSource1<% } else { %>SqlDataSource2<% } %>"
>
</asp:GridView>
This yields the error:
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID '<% if (CheckBox1.Checked == true) { %>SqlDataSource1<% } else { %>SqlDataSource2<% } %>' could not be found.
In fact I received the above error several times based on a few different ways of coding it. Why is it processing all my <% %> code as though that code were the DataSourceID specified, instead of first pre-processing the <% %> code and then using the result from that to plug into the GridView1 tag definition?
Please keep in mind, I do NOT want to do this with a code-behind. I know it can be done that way, but I want to know how to do it without code-behind (if there is indeed a way?) I don't mind using an IF statement or a ternary operator, or some other way.
For example, I was toying with different types of inline tags such as <% vs <%# vs <%=. See article at http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx for more on this. But none of those options seems to give me the result I want.
Am I missing something?
Thanks a bunch!
nick5454
Member
587 Points
486 Posts
Re: Dynamic DataSourceID without code-behind
Apr 09, 2012 07:07 PM|LINK
I seriously doubt it's a bindable property. You'll have to use code behind.
Try it with a <$