DataGrid中DropDownList的动态绑定以及触发DropDownList事件
时间:2010-09-18 来源:无限循环
今天在写DataGrid中DropDownList的绑定,客户需要根据值去改变价格信息,发现忘记如何写的了。虽然网上很多了,但自己还是做个笔记,以后查询方便
1.ASPX页面
1 <asp:TemplateColumn HeaderText="退还价格类型">2 <ItemTemplate>
3 <asp:DropDownList ID="ddlReturnPriceType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlReturnPriceType_SelectedIndexChanged">
4 </asp:DropDownList>
5 </ItemTemplate>
6 <ItemStyle HorizontalAlign="Center" />
7 </asp:TemplateColumn>
AutoPostBack="true"一定不能忘记了!
2.1 CS页面DropDownList动态绑定
1 foreach (DataGridItem item in myGrid.Items)2 {
3 DropDownList ddlReturnPriceType = (DropDownList)item.FindControl("ddlReturnPriceType");
4 ddlReturnPriceType.Items.Clear();
5 ddlReturnPriceType.DataSource = AppEnum.GetReturnPriceType();
6 ddlReturnPriceType.DataValueField = "key";
7 ddlReturnPriceType.DataTextField = "value";
8 ddlReturnPriceType.DataBind();
9 }
2.2 CS页面DropDownList事件
1 DataGridItem item = (DataGridItem)((Control)sender).Parent.Parent;2 TextBox txtPrice = (TextBox)item.FindControl("txtPrice");
3 DropDownList ddlReturnPriceType = (DropDownList)item.FindControl("ddlReturnPriceType");
4 switch (int.Parse(ddlReturnPriceType.SelectedValue))
5 {
6 case (int)AppEnum.ReturnPriceType.TenPercentsOff://原价90%
7 txtPrice.Text = (decimal.Parse(item.Cells[4].Text)*0.9m).ToString(AppConst.DecimalFormat);
8 break;
9 case (int)AppEnum.ReturnPriceType.OriginPrice://原价100%
10 txtPrice.Text = (decimal.Parse(item.Cells[4].Text)).ToString(AppConst.DecimalFormat);
11 break;
12 case (int)AppEnum.ReturnPriceType.InputPrice://自定义价格
13 break;
14 }
相关阅读 更多 +