Asp.net 使用没有sqldatasource的detailsview
我有一个Detailsview对象,当从gridview中单击用户时,它会加载用户数据。问题是,我使用的是sqldatasource,我更愿意使用我的预存在用户类,它具有用于此目的的所有内置功能。我没能把这件事做好。如果没有sqldatasource,我无法使用用户数据填充detailsview,当我尝试插入或更新对象时,它会运行插入/更新代码,然后失败,因为我没有InsertCommand,等我的SQLDataSource…底线…有人能帮我在不需要SQLDataSource的情况下工作吗 这是我的密码Asp.net 使用没有sqldatasource的detailsview,asp.net,detailsview,Asp.net,Detailsview,我有一个Detailsview对象,当从gridview中单击用户时,它会加载用户数据。问题是,我使用的是sqldatasource,我更愿意使用我的预存在用户类,它具有用于此目的的所有内置功能。我没能把这件事做好。如果没有sqldatasource,我无法使用用户数据填充detailsview,当我尝试插入或更新对象时,它会运行插入/更新代码,然后失败,因为我没有InsertCommand,等我的SQLDataSource…底线…有人能帮我在不需要SQLDataSource的情况下工作吗 这是
<asp:GridView runat="server" ID="gvUsers" DataKeyNames="UserID" BackColor="#eeeeee" Width="85%"
HorizontalAlign="Center"
Font-Bold="True" Font-Names="Verdana"
Font-Size="10pt" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound"
OnRowDeleting="GridView1_RowDeleting" >
<HeaderStyle BackColor="Black" ForeColor="White"
Font-Bold="True" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="yellow" ForeColor="blue" />
<AlternatingRowStyle BackColor="#ffffff" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
CommandArgument='<%# Eval("UserID") %>'
CommandName="Select" runat="server">
Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("UserID") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><br /><br />
<asp:DetailsView runat="server" ID="dvUser" DataSourceID="SqlDataSource3" AutoGenerateRows="False" Width="85%"
HorizontalAlign="Center" DataKeyNames="UserID">
<Fields>
<asp:BoundField DataField="UserID" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Password" HeaderText="Password" />
<asp:BoundField DataField="Birthdate" HeaderText="Birthdate" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Apt" HeaderText="Apt" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="ynAdminUser" HeaderText="ynAdminUser" />
<asp:CommandField ShowDeleteButton="False" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:ConnectionString%>" ID="SqlDataSource3"
runat="server" SelectCommand="sp_GetUser" SelectCommandType="StoredProcedure" OnInserting="OnInserting" >
<SelectParameters>
<asp:ControlParameter ControlID="gvUsers" Name="UserID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
挑选
删除
不确定CodeBehind是否必要,我只想用它来调用我的数据对象代码以进行更新、插入等,为什么不使用。此控件的工作方式与SqlDataSource非常相似,但是您可以指定自定义业务对象的方法来执行数据访问,而不是指定SQL查询或存储过程
一个示例,演示了如何使用带有详细视图的ObjectDataSource。谢谢,这看起来是我的答案,但是我的用户类“Load”方法选择用户数据并将其加载到成员变量…这是否意味着我必须创建另一个方法来返回结果,或者我是否可以使用加载的成员变量来显示我在cUser.Load方法中加载的数据?不,您应该能够使用现有的Load()方法。在ObjectDataSource的SelectMethod属性中指定Load()方法。在dvUser Details视图中,将BoundField数据字段属性的值设置为自定义对象的相应属性。my load方法需要传入UserId…我看不到selectmethod的任何参数选项…load()所需的参数方法可以定义为ObjectDataSource的SelectParameters中的ControlParameter,以传递UserID,就像使用SqlDataSource控件一样。