Monday, February 20, 2012

Passing SqlDataSource Properties to Class

Hi,
I have a UserControl called DataPage with the following public property:
privateSqlDataSource sqlDataSource;
publicSqlDataSource SqlDataSource
{
get {returnthis.sqlDataSource; }
set {this.sqlDataSource =value; }
}
In a web form i create an instance of the DataPage UserControl and assign the SqlDataSource properties:
<%@.PageMasterPageFile="~/MasterPages/Page.master"Inherits="IMS.Pages.ModelType"Title="Model Types" %>
<asp:ContentID="Content"ContentPlaceHolderID="ContentPlaceHolder"runat="Server">
<ims:DataPageID="DataPage"runat="server">
<SqlDataSourceID="SqlDataSource"runat="server"
ConnectionString="IMSConnectionString"
DeleteCommand="DeleteModelType"DeleteCommandType="StoredProcedure"
InsertCommand="InsertModelType"InsertCommandType="StoredProcedure"
SelectCommand="SelectModelType"SelectCommandType="StoredProcedure"
UpdateCommand="UpdateModelType"UpdateCommandType="StoredProcedure">
<DeleteParameters>
<asp:ParameterName="ModelTypeID"Type="Int32"/>
</DeleteParameters>
<UpdateParameters>
<asp:ParameterName="ModelTypeID"Type="Int32"/>
<asp:ControlParameterControlID="UpdateModelTypeTextBox"Name="ModelType"PropertyName="Text"Type="String"/>
</UpdateParameters>
<SelectParameters>
<asp:ParameterName="ModelTypeID"Type="Int32"/>
</SelectParameters>
<InsertParameters>
<asp:ControlParameterControlID="InsertModelTypeTextBox"Name="ModelType"PropertyName="Text"Type="String"/>
</InsertParameters>
</SqlDataSource>
</ims:DataPage>
</asp:Content>


Then in the UserControl I try to call the SqlDataSource.Insert() method, and I get the following error:
"The SqlDataSource control 'SqlDataSource' does not have a naming container. Ensure that the control is added to the page before calling DataBind."

Now, based on the error message it sounds like that SqlDataSource is not contained in either the web form or the usercontrol. Any ideas, thanks very much for any help people.
Grant

It doesn't look like you're actually adding the SqlDataSource to the page. It's being created, and some properties are assigned to it, but no one is adding it to the control tree. What you're doing here is a bit beyond what UserControls are meant to do. You should typically never have a control be a settable property on another control. Why not just have a DataSourceID property on the UserControl and just declare the SqlDataSource on the content page? It seems that you aren't gaining much anyway with your syntax since you're placing so many details on the content page.
Thanks,
Eilon

|||Eilon,
Many thanks for your response. I have managed to get this to work by basically doing as you suggested. Sometimes the obvious can get a little twisted!! The problem that I face now is that any ControlParameters that I setup on the SqlDataSource are visible as they are inside the DataPage naming container. Maybe you can help with this??
What I am trying to achieve is a templated control that takes care of all my 'simple' data pages. The Usercontrol has a MultiView with the following views: a paged repeater, a delete view (allowing confirm / cancel), a deleted view (report showing success of delete), an insert view (allowing insert information, insert / cancel), an inserted view (report showing success of insert), an update view (allowing updating of information, update / cancel), an updated view (report showing success of update), and an error view (report detailing error).
I have exposed an ITemplate property for each view (repeater view obviously has HeaderTemplate, Footer, Item, AlternatingItem) in the UserControl with a content place holder in each view in the neccessary postion. These templates are then provided on the WebForm. Is there anyway to get the WebForm to recognise these templates at design time? At present I have to work in code view. Cannot switch Error... Like you said previously I think I am trying to gain a little too much from the UserControl (however, the templates etc all work beautifully, just can't use deisgn view).
What I am trying to achieve is the ability to create all my 'simple' data webforms without needing any code behind, i.e. it can all be achieved through markup. All neccessary code is encapsulated in the DataPage UserControl. This would mean that about 80% of my pages are dealt with for every project!!
Any light that you can shed on this would be much appreciated.
Many thanks for you efforts (Eilon and anyone else that takes the time to read this),
Grant.

No comments:

Post a Comment