Should I *really* be calling DataBind()?

Cyrodox

New Member
I have a CustomControl that, when used on a page, needs to take as a property a value from a server-side value. I tried this ...\[code\]<MyControl runat="server" ID="MyControl1" ContainerId="<%= this.ClientID %>" />\[/code\]Now, when run (for the sake of this explanation, lets say that I know the value of \[code\]this.ClientID\[/code\] was "ctl00_MyControl1") if I test the value, client-side, of \[code\]ContainerId\[/code\] it comes back as "<%= this.ClientID %>".Without really understanding why, I tried this instead...\[code\]<MyControl runat="server" ID="MyControl1" ContainerId="<%# this.ClientID %>" />\[/code\]But testing the value of \[code\]ContainerId\[/code\] showed that the value was empty!Some more reading enabled me to see that the <%# %> mechanism is for data binding but, clearly, my control wasn't doing that (assume that my CustomControl inherits from a TextBox).So, I added a call to \[code\]DataBind()\[/code\] to the \[code\]OnLoad\[/code\] event of the UserControl that my CustomControl is contained in. Yay! It worked. However, when processing certain events on the Page, the call to \[code\]DataBind()\[/code\] generates an exception. The exception is in another control, contained in the same UserControl container as \[code\]MyControl1\[/code\].The message is\[code\]Selection out of rangeParameter name: value\[/code\]and the stack trace finishes like this ...\[code\] at Telerik.Web.UI.RadComboBox.PerformDataBinding(IEnumerable dataSource) at Telerik.Web.UI.RadComboBox.OnDataSourceViewSelectCallback(IEnumerable data) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at Telerik.Web.UI.RadComboBox.OnDataBinding(EventArgs e) at Telerik.Web.UI.RadComboBox.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at Telerik.Web.UI.RadComboBox.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind()\[/code\]Now, I'm not looking for a solution to this exception; I've included it to demonstrate that I believe that my calling \[code\]DataBind()\[/code\] is not necessarily the right thing to do.So, Questions:[*]Why doesn't <%= %> give the value at runtime I expect?[*]Is calling \[code\]DataBind()\[/code\] whenever the UserControl's \[code\]OnLoad\[/code\] event fires the right thing to do to get my value at runtime and, if it is,[*]Are there conditions I should be checking for before calling \[code\]DataBind()\[/code\]
 
Back
Top