微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – 如何将下拉列表添加为gridview项

我的表单在gridview中有三列.一个数量(下拉列表){如何添加数量下拉列表?},其他是价格和金额.我想计算gridview内的数量.如果我选择数量“2”,那么它计算数量*价格.如何在gridview或任何其他选项中获取下拉列表的selectedindexchanged属性

如何在gridview项中添加下拉列表?
如何在gridview内的下拉列表中添加值?
如何为asp.net中的gridview内的下拉列表编写selectedindexchanged事件的代码

解决方法

你的问题有三个部分:

>如何在GridView中添加DropDownList?
>如何从GridView中的DropDownList中获取所选项?
>如何计算值并在GridView中显示

这是我们如何做到的.首先在页面添加标记.我为产品名称添加一个列,使其看起来更好:

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField datafield="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:DropDownList ID="ddlQuantity" runat="server" AutopostBack="true" OnSelectedindexChanged="ddlQuantity_SelectedindexChanged"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:Label ID="lblPrice" Text='<%#Eval("Price") %>' runat="server" ></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>                        
                <asp:Label ID="lblAmount" Text="0.00" runat="server" ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

如何在GridView中添加DropDownList

在Page_Load()中填充GridView(我使用了产品列表):

protected void Page_Load(object sender,EventArgs e)
{
    if (!IsPostBack)
    {
        //Test data to populate GridView
        GridView1.DataSource = new List<Product>()
        {
            new Product{ID=1,Name="Paper",Price=7.99M},new Product{ID=2,Name="Pen",Price=14.99M},new Product{ID=3,Name="Pencil",Price=1.99M}
        };

        GridView1.DataBind();
    }
}

这将触发GridView的RowDataBound事件.在event方法中,我们将在每行中绑定DropDownList,如下所示:

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DaTarow)
    {
        var ddl = e.Row.FindControl("ddlQuantity") as DropDownList;
        if (ddl != null)
        {
            ddl.DataSource = new List<string>() { "0","1","2","3","4" };
            ddl.DataBind();
        }
    }
}

如何从GridView中的DropDownList中获取所选项目

如何计算值并在GridView中显示

当您更改DropDownList中的任何选择时,它将触发SelectedindexChange事件.在那个事件方法中,我们可以找到哪个DropDownList被更改,而且它的“NamingContainer” – GridView的行保存它:

protected void ddlQuantity_SelectedindexChanged(object sender,EventArgs e)
{
    GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow ;
    if (gvr != null)
    {
        decimal price = 0.00M;
        int quantity = 0;
        //We can find all the controls in this row and do operations on them
        var ddlQuantity = gvr.FindControl("ddlQuantity") as DropDownList;
        var lblPrice = gvr.FindControl("lblPrice") as Label;
        var lblAmount = gvr.FindControl("lblAmount") as Label;
        if (ddlQuantity != null && lblPrice != null && lblAmount != null)
        {
            int.TryParse(ddlQuantity.SelectedValue,out quantity);
            decimal.TryParse(lblPrice.Text,out price);

            lblAmount.Text = (price * quantity).ToString();
        }    
    }
}

这是结果:

您可以下载测试项目here.

原文地址:https://www.jb51.cc/aspnet/245761.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐