DataGrid中DropDownList触发SelectedIndexChanged事件并修改DataGrid行值(转载)

今天遇到了问题,就是在 DataGrid中DropDownList触发SelectedIndexChanged事件 ,并通过修改DropDownList的值,把其它对应的值也读取绑定DataGrid相应的行,前台模版列,AutoPostBack="True" 不要忘了 本文来自: IT知道网(http://www.itwis.com) 详细出处参考:

<asp:TemplateColumn HeaderText="上级提供商">
<ItemStyle HorizontalAlign="Center" Width="50px"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="DDL_UP" runat=server AutoPostBack="True"
OnSelectedIndexChanged="DDL_UP_SelectedIndexChanged" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>后台
   protected void DDL_UP_SelectedIndexChanged(object sender, EventArgs e)
    ...{
        System.Web.UI.WebControls.DataGridItem item = (DataGridItem)((Control)sender).Parent.Parent;
//获取当前Item,是两个Parent,一个会出错的
        DropDownList list;
        list = (DropDownList)item.FindControl("DDL_UP");
        string str = "select  *  from  aa  where Product_TypeName='" + item.Cells[15].Text.Trim() + "'";
//已经省略了sql语句
        SqlDataReader dr= Sg.SQLServerDAL.DbHelperSQL.ExecuteReader(str);
      if (dr.Read())
      ...{
          ((TextBox)item.Cells[4].Controls[0]).Text = dr["Product_Name"].ToString();//读取修改相应列的值
          ((TextBox)item.Cells[4].Controls[0]).Enabled = false;
          if (dr["Product_Price"].ToString() != "")
          ...{
              ((TextBox)item.Cells[8].Controls[0]).Text = Convert.ToDecimal(dr["Product_Price"]).ToString("0");
//把值赋给要修改列的文本框
          }
          if (dr["Product_Cost"].ToString() != "")
          ...{

              ((TextBox)item.Cells[10].Controls[0]).Text = Convert.ToDecimal(dr["Product_Cost"]).ToString("0");
          }
          else
          ...{
              ((TextBox)item.Cells[10].Controls[0]).Text = "0";
          }
      }
      dr.Close();
      
    }
原先参考以下方法没有成功:
DataGrid中DropDownList OnSelectedIndexChanged事件触发- -
                                      

 

手头有个程序,其中设计到用DataGrid对数据进行多种操作处理;for example:Insert,Edit,Del,Cancel and so on.考虑到用按钮列的话需要太多按钮,因此想用DropDownList来实现按钮的各项功能.这样就要用到DropDownList的OnSelectedIndexChanged事件的触发.

开始我在html里的DropDownList添加了OnSelectedIndexChanged="changed",然后在后台写changed()的代码,但是这样的话将无法获取当前的Item信息.在客户端向服务器端传送操作的参数,然后服务器对此进行处理,这样显然不太现实.后来想到了ItemDataBound事件;在msdn里面,是这么说的:

事件数据事件处理程序接收一个 DataGridItemEventArgs 类型的参数,它包含与此事件相关的数据。下列 DataGridItemEventArgs 属性提供特定于此事件的信息。

属性 说明
Item 获取引发该事件时 DataGrid 控件中的被引用项。

备注当项被数据绑定到 DataGrid 控件后,将引发 ItemDataBound 事件。此事件为您提供了在客户端显示数据项之前访问该数据项的最后机会。当引发此事件后,该数据项将被设为空,并且不再可用。

也就是说,在DataGrid中添加的服务器控件,需要写触发事件的话,可以在DataGrid.ItemDataBound中为事件添加委托.

DataGrid中DropDownList模板列的OnSelectedIndexChanged事件触发实现如下:

private void testDg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  ...{
   if(e.Item.FindControl("MyDropDownList")!=null)
    ((DropDownList)e.Item.FindControl("MyDropDownList")).SelectedIndexChanged+=new System.EventHandler(this.Changed);
  }


测试dropdownlist,selectedindexchanged#region 测试dropdownlist,selectedindexchanged
  protected void Changed(object sender, System.EventArgs e)
  ...{
   System.Web.UI.WebControls.DataGridItem item=(DataGridItem)((Control)sender).Parent.Parent;//获取当前Item
  
   DropDownList list;
   list=(DropDownList)item.FindControl("MyDropDownList");
   Response.Write(list.SelectedItem.Text);
  }
  #endregion

本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20080219/943.html

转载于:https://www.cnblogs.com/chaobaojun/archive/2009/09/10/1564192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值