您的位置:首页 > 运维架构

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

2009-09-10 17:04 274 查看
今天遇到了问题,就是在 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: