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
<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
相关文章推荐
- [转载] 理解 epoll 的事件触发机制
- 【转载】Delphi7从子线程中发送消息到主线程触发事件执行
- 在C#中使用代理的方式触发事件(转载)
- 修改protect属性, 可能会触发rowfocuschanged事件
- 父窗口jquery触发iframe按钮事件(转载)
- Z-Stack中OSAL定时器事件触发流程分析---转载
- c#中在一个窗体中触发另一个窗体的事件 (转载)
- MYSQL的基本使用【表的创建和修改、视图、函数、存储过程、触发器和事件调度器】
- 如何在修改checkbox状态,不触发事件
- 如何在修改控件属性值,而不触发事件
- 修改select标签为可输入内容的input标签,同时在首次点击第一个下拉框时也会触发onchange事件
- [转载]Qt 软键盘[模拟]之键盘触发事件
- MYSQL的基本使用【表的创建和修改、视图、函数、存储过程、触发器和事件调度器】
- asp.net 页面回车触发button按钮事件-(转载)
- cxgrid控件修改记录时AfterScroll事件触发N次的问题
- yii2 随笔(六)利用事件触发修改请求参
- Dev GridControl修改单元格无法触发CellValueChanged事件的解决方法
- Select中使用js动态修改了Option时如何触发onchange事件
- jquery 6 动态创建的元素的事件触发(转载)
- 点击repeater的一个修改事件触发全部repeater每一行的修改事件