帮助ASP.NET ObjectDataSource

时间:2009-11-16 02:58:18

标签: asp.net gridview objectdatasource detailsview

我有一个带有Load方法的User对象,该方法接受UserId参数并将用户数据加载到对象成员变量。

现在我要做的是使用DetailsView将此数据加载到ObjectDataSource控件,但我对如何操作感到难过。

首先,我不确定我是否已正确设置代码以将参数(UserID)传递给SelectMethod (cUser.Load)。其次,我不知道如何将这些数据加载到DetailsView,因为我实际上并没有从Load方法返回结果,我只是用数据加载对象。那是我的代码..

<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"
                    OnSelectedIndexChanged="IndexChanged" >
            <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="ObjectDataSource1" 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:ObjectDataSource ID="ObjectDataSource1"
                    runat="server" SelectMethod="Load" TypeName="cUser">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="gvUsers" PropertyName="SelectedValue" Name="iUserID" Type="int32" />
                    </SelectParameters>
                </asp:ObjectDataSource>

有没有办法做我想要实现的目标?如果是的话,你能举例解释一下吗?

非常感谢。感谢

1 个答案:

答案 0 :(得分:0)

您需要查看本教程以获取详细帮助....

Master/Detail Using a Selectable Master GridView with a Details DetailView