DataGrid中DropDownList触发SelectedIndexChanged事件并修改DataGrid行值
2007-08-24 15:28
351 查看
今天遇到了问题,就是在 DataGrid中DropDownList触发SelectedIndexChanged事件 ,并通过修改DropDownList的值,把其它对应的值也读取绑定DataGrid相应的行,
前台模版列,AutoPostBack="True" 不要忘了
<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
前台模版列,AutoPostBack="True" 不要忘了
<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
相关文章推荐
- DataGrid中DropDownList的动态绑定和触发DropDownList事件(*转)
- DataGrid中DropDownList的动态绑定和触发DropDownList事件
- DataGrid模版中嵌入了dropdownlist,如何触发这个dropdownlist的事件
- DataGrid中DropDownList的动态绑定和触发DropDownList事件
- DataGrid中dropdownlist事件的触发/ImageButton的事件触发
- 通过代码触发dropdownlist_SelectedIndexChanged事件。
- 修改select标签为可输入内容的input标签,同时在首次点击第一个下拉框时也会触发onchange事件
- 如何在修改checkbox状态,不触发事件
- 鼠标在DataGrid上移动时触发事件
- DropDownList 控件不能触发SelectedIndexChanged 事件的另一个原因
- 点击repeater的一个修改事件触发全部repeater每一行的修改事件
- (摘抄)为什么设置了DropDownList的AutoPostBack="True"还是不能触发SelectedIndexChanged事件?
- (论坛答疑点滴)为什么设置了DropDownList的AutoPostBack="True"还是不能触发SelectedIndexChanged事件?
- 处理在DataGrid中的DropDownList的事件
- ASP.NET基础教程-DataGrid表格控件-处理在DataGrid中的DropDownList的事件
- dropdownlist设置 AutoPostBack=True 时不能触发事件的问题
- Select中使用js动态修改了Option时如何触发onchange事件
- yii2 随笔(六)利用事件触发修改请求参
- 修改jquery.datatable 搜索框触发事件
- Asp.net中DropDownlist中无法触发后台事件解决方案