使用没有sqldatasource的detailsview

时间:2009-11-14 18:14:19

标签: asp.net detailsview

我有一个Detailsview对象,当从gridview中单击User时,该对象加载了用户数据。问题是,我正在使用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是否必要,我只是想用它来调用我的数据对象代码进行更新,插入等

1 个答案:

答案 0 :(得分:2)

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

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

Here's一个示例,说明如何将ObjectDataSource与DetailsView一起使用。