开发者

How do you DataBind a SqlDataSource to a DetailView in a UserControl?

I have a user control I have created that contains a details-view that I plan to use as a template to develop many other pages using. For example, I want to use this control on another .aspx page I have. This aspx page will use a SqlDataSource to databind to. I am very new to this stuff so my understanding is limited, so be patient with me (thanks).

This is the basi开发者_如何学Pythoncs of the user control:

<asp:DetailsView ID="DV1" runat="server" AutoGenerateRows="False" 
DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="16px" Width="100%" 
Font-Size="14pt" FooterText=" " ForeColor="#333333" GridLines="None" 
HeaderText=" ">

In the back code, I have made this ID accessible:

public DetailsView DetailView
    {
        get { return DV1; }
        set { DV1 = value; }
    }

In my aspx page I have:

<%@ Register Src="StdDetails_View.ascx" TagName="UserList" TagPrefix="uc" %>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ucsConnectionString %>" 
    SelectCommand="usp_UserInfo_GetBy_ID" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:Parameter DefaultValue="5afadb8a-3127-4a67-85dd-bde4de6d03c4" Name="ID" 
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

The only way I can get this to work is if I add BoundFields in the userControl to the specific stored-procedure I use. Outside of this, I do not know what to do! How do I go about binding this datasource using this detailview?

All your help is appreciated!


Try setting AutoGenerateRows = true

<asp:DetailsView ID="DV1" runat="server" AutoGenerateRows="true" 
DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="16px" Width="100%" 
Font-Size="14pt" FooterText=" " ForeColor="#333333" GridLines="None" 
HeaderText=" ">


The DataSourceID will only work within the same context (i.e. the usercontrol). You will need to do something like this within the code-behind of your web form:

UserList.DetailView.DataSource = SqlDataSource1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜