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

前端之家收集整理的这篇文章主要介绍了asp.net – 如何将下拉列表添加为gridview项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的表单在gridview中有三列.一个是数量(下拉列表){如何添加数量下拉列表?},其他是价格和金额.我想计算gridview内的数量.如果我选择数量“2”,那么它计算数量*价格.如何在gridview或任何其他选项中获取下拉列表的selectedindexchanged属性

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

解决方法

你的问题有三个部分:

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

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

  1. <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBound">
  2. <Columns>
  3. <asp:BoundField DataField="Name" HeaderText="Name" />
  4. <asp:TemplateField HeaderText="Quantity">
  5. <ItemTemplate>
  6. <asp:DropDownList ID="ddlQuantity" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged"></asp:DropDownList>
  7. </ItemTemplate>
  8. </asp:TemplateField>
  9. <asp:TemplateField HeaderText="Price">
  10. <ItemTemplate>
  11. <asp:Label ID="lblPrice" Text='<%#Eval("Price") %>' runat="server" ></asp:Label>
  12. </ItemTemplate>
  13.  
  14. </asp:TemplateField>
  15. <asp:TemplateField HeaderText="Amount">
  16. <ItemTemplate>
  17. <asp:Label ID="lblAmount" Text="0.00" runat="server" ></asp:Label>
  18. </ItemTemplate>
  19. </asp:TemplateField>
  20. </Columns>
  21. </asp:GridView>

如何在GridView中添加DropDownList

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

  1. protected void Page_Load(object sender,EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. //Test data to populate GridView
  6. GridView1.DataSource = new List<Product>()
  7. {
  8. 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}
  9. };
  10.  
  11. GridView1.DataBind();
  12. }
  13. }

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

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

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

如何计算值并在GridView中显示

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

  1. protected void ddlQuantity_SelectedIndexChanged(object sender,EventArgs e)
  2. {
  3. GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow ;
  4. if (gvr != null)
  5. {
  6. decimal price = 0.00M;
  7. int quantity = 0;
  8. //We can find all the controls in this row and do operations on them
  9. var ddlQuantity = gvr.FindControl("ddlQuantity") as DropDownList;
  10. var lblPrice = gvr.FindControl("lblPrice") as Label;
  11. var lblAmount = gvr.FindControl("lblAmount") as Label;
  12. if (ddlQuantity != null && lblPrice != null && lblAmount != null)
  13. {
  14. int.TryParse(ddlQuantity.SelectedValue,out quantity);
  15. decimal.TryParse(lblPrice.Text,out price);
  16.  
  17. lblAmount.Text = (price * quantity).ToString();
  18. }
  19. }
  20. }

这是结果:

您可以下载测试项目here.

猜你在找的asp.Net相关文章