如何将 SqlDataSource 数据绑定到 UserControl 中的 DetailView?
我创建了一个用户控件,其中包含一个详细信息视图,我计划将其用作模板来开发许多其他页面。例如,我想在我拥有的另一个 .aspx 页面上使用此控件。此 aspx 页面将使用 SqlDataSource 进行数据绑定。我对这些东西很陌生,所以我的理解是有限的,所以请耐心等待(谢谢)。
这是用户控件的基础知识:
<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=" ">
在后面的代码中,我已经使这个 ID 可访问:
public DetailsView DetailView
{
get { return DV1; }
set { DV1 = value; }
}
在我的 aspx 页面中,我有:
<%@ 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>
让它工作的唯一方法是,如果我将 userControl 中的 BoundFields 添加到特定的存储 -我使用的程序。除此之外,我不知道该怎么办!如何使用此详细视图绑定此数据源?
感谢您的所有帮助!
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 basics 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试设置 AutoGenerateRows = true
Try setting AutoGenerateRows = true
DataSourceID 只能在相同的上下文(即用户控件)中工作。您需要在 Web 表单的代码隐藏中执行类似的操作:
UserList.DetailView.DataSource = SqlDataSource1
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