jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
asp2.0中formview+dropdown控件小DEMO,演示了如何对northwind数据库中的product表进行更改,其中可以对某个产品的所属分类用dropdownlist进行更改,可以注意下formview和dropdownlist的用法
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [CategoryID] FROM [Products]"
            UpdateCommand="UPDATE  [Products]  SET [ProductName]=@productname,[CategoryID] =@categoryid  WHERE [ProductID] = @productid">
                           <UpdateParameters> 
    <asp:Parameter Name="ProductName" Type="String" />
                       <asp:Parameter Name="CategoryID" Type="Int32" />
                               <asp:Parameter Name="ProductID" Type="Int32" />
                           </UpdateParameters>
        </asp:SqlDataSource>
   
    </div>
        <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="ProductID"
            DataSourceID="SqlDataSource1">
            <EditItemTemplate>
                ProductID:
                <asp:Label ID="ProductIDLabel1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
                ProductName:
                <asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>'>
                </asp:TextBox><br />
                CategoryID:
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
                    DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
                </asp:DropDownList><br />
                 (<asp:Label ID="CategoryIDLabel"                         runat="server"                         Text='<%# Eval("CategoryID") %>' />)<br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update">
                </asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                ProductName:
                <asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>'>
                </asp:TextBox><br />
                CategoryID:
                <asp:TextBox ID="CategoryIDTextBox" runat="server" Text='<%# Bind("CategoryID") %>'>
                </asp:TextBox><br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
                ProductID:
                <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
                ProductName:
                <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label><br />
                CategoryID:
               <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Sqldatasource2"                        DataTextField="CategoryName"                         DataValueField="CategoryID"   SelectedValue='<%# Eval("CategoryID") %>'   Enabled="False" />
                                  (<asp:Label ID="CategoryIDLabel" runat="server"  Text='<%# Eval("CategoryID") %>'/>)
      <asp:LinkButton ID="EditButton" runat="server"   CausesValidation="False"    CommandName="Edit"    Text="Edit" />
            </ItemTemplate>
          
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource>
    </form>
</body>
posted on 2006-02-10 08:23  jackyrong的世界  阅读(2127)  评论(2编辑  收藏  举报