gridview中dropdownlist绑定,更新问题
我在网页Gridview控件中,有一列普通列是用textbox绑定的,在点击编辑后,textbox在编辑列变成dropdownlist,可供用户选择,之后点击更新,保存d...
我在网页Gridview控件中,有一列普通列是用textbox绑定的,在点击编辑后,textbox在编辑列变成dropdownlist,可供用户选择,之后 点击更新,保存dropdownlist选择的数据,该如何实现。我原先是在gridview编辑事件中,给dropdownlist赋值,并选中默认值,可在选中数据保存事件里,取不倒值,并报错。各位高手,帮我说的详细点,最好有源码例子。不好意思,没分了。
展开
2013-05-22
展开全部
把一个DropDownList放到Template中去,这样写代码(片段):<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> </script> <html xmlns=" http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="productsDataSource" Runat="server" SelectCommand="SELECT dbo.Products.ProductID, dbo.Products.ProductName, dbo.Products.CategoryID, dbo.Categories.CategoryName FROM dbo.Products INNER JOIN dbo.Categories ON dbo.Products.CategoryID = dbo.Categories.CategoryID" UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID WHERE [ProductID] = @original_ProductID" ConnectionString="<%$ ConnectionStrings:NWConnectionString %>"> <UpdateParameters> <asp:Parameter Type="String" Name="ProductName"></asp:Parameter> <asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter> <asp:Parameter Name="original_ProductID"></asp:Parameter> </UpdateParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="categoryDataSource" Runat="server" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY [CategoryName]" ConnectionString="<%$ ConnectionStrings:NWConnectionString %>"> </asp:SqlDataSource> <asp:GridView ID="GridView1" Runat="server" DataSourceID="productsDataSource" DataKeyNames="ProductID" AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC"> <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle> <PagerStyle ForeColor="#000066" HorizontalAlign="Left" BackColor="White"></PagerStyle> <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#006699"></HeaderStyle> <Columns> <asp:CommandField ShowEditButton="True"></asp:CommandField> <asp:BoundField ReadOnly="True" HeaderText="ProductID" InsertVisible="False" DataField="ProductID" SortExpression="ProductID"> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName"></asp:BoundField> <asp:TemplateField SortExpression="CategoryName" HeaderText="CategoryName"><EditItemTemplate> <asp:DropDownList ID="DropDownList1" Runat="server" DataSourceID="categoryDataSource" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label Runat="server" Text='<%# Bind("CategoryName") %>' ID="Label1"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#669999"></SelectedRowStyle> <RowStyle ForeColor="#000066"></RowStyle> </asp:GridView> </div> </form></body></html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询