0

我有一个 Detailsview 对象,当从 gridview 中单击用户时,该对象会加载用户数据。问题是,我使用的是 sqldatasource,我宁愿使用我预先存在的用户类,它具有所有内置功能。我无法正确完成这项工作。如果没有 sqldatasource,我无法使用用户数据填充详细信息视图,当我尝试使用我的对象插入或更新时,它会运行插入/更新代码,然后因为我没有 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 是否必要,我只想用它来调用我的数据对象代码以进行更新、插入等

4

1 回答 1

2

为什么不使用ObjectDataSource。此控件的工作方式与 SqlDataSource 非常相似,但您无需指定 SQL 查询或存储过程,而是指定自定义业务对象的方法来执行数据访问。

<asp:ObjectDataSource ID="ObjectDataSource" Drunat="server" DeleteMethod="Delete" InsertMethod="Insert" SelectMethod="Select" UpdateMethod="Update" TypeName="YourType">

这是一个示例,说明如何将 ObjectDataSource 与 DetailsView 一起使用。

于 2009-11-14T18:22:39.000 回答