如何在gridview中单击更新时显示下拉菜单?

时间:2014-02-04 11:06:32

标签: asp.net gridview

我在gridview中有一个名为status的字段我想要它,这样当用户点击gridview在textfield的位置更新时,会有一个带已发布和未发布选项的下拉列表。这是代码

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource3">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:BoundField DataField="info" HeaderText="info" SortExpression="info" />
                <asp:BoundField DataField="productcode" HeaderText="productcode" SortExpression="productcode" />
                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
                <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" DeleteCommand="DELETE FROM [product_tbl] WHERE [Id] = @Id" InsertCommand="INSERT INTO [product_tbl] ([Id], [subcat], [name], [info], [brandid], [gender]) VALUES (@Id, @subcat, @name, @info, @brandid, @gender)" SelectCommand="SELECT * FROM [product_tbl]" UpdateCommand="UPDATE [product_tbl] SET [subcat] = @subcat, [name] = @name, [info] = @info, [brandid] = @brandid, [productcode] = @productcode, [gender] = @gender WHERE [Id] = @Id">
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Id" Type="Int32" />
                <asp:Parameter Name="subcat" Type="Int32" />
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="info" Type="String" />
                <asp:Parameter Name="brandid" Type="Int32" />
                <asp:Parameter Name="gender" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="subcat" Type="Int32" />
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="info" Type="String" />
                <asp:Parameter Name="brandid" Type="Int32" />
                <asp:Parameter Name="productcode" Type="Int32" />
                <asp:Parameter Name="gender" Type="Int32" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

对于下拉列表,您需要放置templatefield

             <asp:TemplateField HeaderText="Status">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlStautus" runat="server"> 
                           <asp:ListItem>published</asp:ListItem>
                           <asp:ListItem>unpublished</asp:ListItem>
                        </asp:DropDowList>
               </asp:TempLateFeild>

并将您的gridview绑定在

背后的代码上

更多参考用途This link

                              OR

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" DataKeyNames="UserID"
        ForeColor="#333333" GridLines="None"             
            AutoGenerateColumns="False" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating1" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowdatabound="GridView1_RowDataBound1" 
        onrowdeleting="GridView1_RowDeleting">
           <Columns>
           <asp:TemplateField HeaderText="id" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtName" runat="server" Width="75px" 
                            Text='<%# Bind("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server" 
                            Text='<%# Bind("Name") %>'></asp:Label>
                    </ItemTemplate>
                     </asp:TemplateField>
                      <asp:TemplateField HeaderText="Info">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtInfo" runat="server" Width="75px"
                            Text='<%# Bind("Info") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblInfo" runat="server" 
                            Text='<%# Bind("Info") %>'></asp:Label>
                    </ItemTemplate>
                     </asp:TemplateField>
            <asp:TemplateField HeaderText="productcode">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtproductcode" runat="server" Width="75px"
                            Text='<%# Bind("DomainID") %>'></asp:TextBox>
                         </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblproductcode" runat="server" 
                            Text='<%# Bind("productcode") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText = "status">
                    <ItemTemplate>
                    <asp:Label ID="lblRole" runat="server" Text='<%# Bind("status") %>' Visible="false"/>
                    <asp:DropDownList ID="ddlRole" runat="server">
                    </asp:DropDownList>
                    </ItemTemplate>
                    </asp:TemplateField>                       
                <asp:CommandField ShowEditButton="True"/>
                <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the record?')">
                            <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                        </span>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>                
        </asp:GridView>

和使用代码背后的代码在链接

中给出