Dynamic DataSourceID without code-behindhttp://forums.asp.net/t/1790827.aspx/1?Dynamic+DataSourceID+without+code+behindMon, 09 Apr 2012 19:07:49 -040017908274923322http://forums.asp.net/p/1790827/4923322.aspx/1?Dynamic+DataSourceID+without+code+behindDynamic DataSourceID without code-behind <p>How can I specify a dynamic DataSourceID to a control such as GridView, using a conditional statement in the ASPX code ??</p> <p>Let's say I have these controls:</p> <p style="padding-left:30px">&lt;asp:CheckBox ID=&quot;CheckBox1&quot; runat=&quot;server&quot; /&gt;</p> <p style="padding-left:30px">&lt;asp:SqlDataSource ID=&quot;SqlDataSource1&quot; runat=&quot;server&quot; /&gt;</p> <p style="padding-left:30px">&lt;asp:SqlDataSource ID=&quot;SqlDataSource2&quot; runat=&quot;server&quot; /&gt;</p> <p style="padding-left:30px">&lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; <br> DataSourceID = &quot;&lt;%# (CheckBox1.Checked == true ? SqlDataSource1.ID : SqlDataSource2.ID) %&gt;&quot; /&gt;</p> <p>The idea here is that the DataSourceID can vary based on the value of the checkbox. &nbsp;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.</p> <p>I have tried several other methods, and nothing seems to work. &nbsp;For example:</p> <p style="padding-left:30px">&lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; <br> DataSourceID = &quot;&lt;% if (CheckBox1.Checked == true) { %&gt;SqlDataSource1&lt;% } else { %&gt;SqlDataSource2&lt;% } %&gt;&quot;<br> &gt;<br> &lt;/asp:GridView&gt;</p> <p>This yields the error:</p> <p style="padding-left:30px">The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. &nbsp;A control with ID '&lt;% if (CheckBox1.Checked == true) { %&gt;SqlDataSource1&lt;% } else { %&gt;SqlDataSource2&lt;% } %&gt;' could not be found.</p> <p>In fact I received the above error several times based on a few different ways of coding it. &nbsp;Why is it processing all my &lt;% %&gt; code as though that code were the DataSourceID specified, instead of first pre-processing the &lt;% %&gt; code and then using the result from that to plug into the GridView1 tag definition?</p> <p>Please keep in mind, I do NOT want to do this with a code-behind. &nbsp;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?) &nbsp;I don't mind using an IF statement or a ternary operator, or some other way.</p> <p>For example, I was toying with different types of inline tags such as &lt;% vs &lt;%# vs &lt;%=. &nbsp;See article at&nbsp;<a href="http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx">http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx</a>&nbsp;for more on this. &nbsp;But none of those options seems to give me the result I want.</p> <p>Am I missing something?</p> <p>Thanks a bunch!</p> <p></p> <h2><i><br> </i></h2> 2012-04-09T15:38:45-04:004923596http://forums.asp.net/p/1790827/4923596.aspx/1?Re+Dynamic+DataSourceID+without+code+behindRe: Dynamic DataSourceID without code-behind <p>I seriously doubt it's a bindable property. You'll have to use code behind.</p> <p></p> <p>Try it with a &lt;&#36;</p> 2012-04-09T19:07:49-04:00