本文介绍了GridView的Dropdown在从中选择任何一个时同时触发SelectedIndexChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在通过程序向网格视图添加下拉列表。当我第一次选择drowdown项目时它工作正常(事件仅触发一次)但在下次我选择另一个下拉列表项目然后我的事件(GVDropDownSelectIndexChanged)点火两次(根据之前选择的下拉列表n次)。下面是我的代码。可以任何机构给我解决这个问题。 先进的thanx。 我的代码 public void PopulateGridViewDetails() { int row1 = 0 ; foreach (GridViewRow行 GridView1.Rows) { for ( int cCount = 0 ; cCount < dataTable.Columns.Count; cCount ++) { if (cCount < 3 ) { Label l = new Label(); l.ID = gvlbl + dataTable.Columns [cCount] .ColumnName; l.Text = dataTable.Rows [row1] [cCount] .ToString(); if (l.ID == gvlblTarineeId) { l.Visible = true ; } if (cCount == 0 ) { GridView1.HeaderRow.Cells [cCount] .Visible = false ; rows.Cells [cCount] .Visible = false ; } rows.Cells [cCount] .Controls.Add(l); } else { if (dataTable .Columns [cCount] .ColumnName.ToLower()。包含( status)) { // 将下拉列表添加到网格视图 DropDownList ddlStatus = new DropDownList(); ddlStatus.ID = ddl + dataTable.Columns [cCount] .ColumnName + _ + row1; ddlStatus.DataSource = new StudentExamStatusDAO()。GetExamStatusList(); ddlStatus.DataTextField = 状态; ddlStatus.DataValueField = Id; ddlStatus.DataBind(); ddlStatus.Items.Add( new ListItem( - 选择状态 - , 0) ); ddlStatus.SelectedValue = 0; // 将事件添加到DropdownList ddlStatus.SelectedIndexChanged + = new EventHandler(GVDropDownSelectIndexChanged); ddlStatus.AutoPostBack = true ; rows.Cells [cCount] .Controls.Add(ddlStatus); GridView1.HeaderRow.Cells [cCount] .Text = ; } else { TextBox t = new TextBox(); t.ID = gvtxt + dataTable.Columns [cCount] .ColumnName; rows.Cells [cCount] .Controls.Add(t); 标签lID = 新标签(); lID.ID = gvlbl + dataTable.Columns [cCount] .ColumnName + Id; lID.Text = dataTable.Rows [row1] [cCount] .ToString(); lID.Visible = false ; rows.Cells [cCount] .Controls.Add(lID); } } } row1 ++; } } 我的事件被触发n次(根据之前的下拉列表选择) 受保护 void GVDropDownSelectIndexChanged( object sender,EventArgs e) { // MyStuff } 是否有任何排序问题。 i加载gridview数据来自pageLoad事件而不检查IsPostback属性 protected void Page_Load( object sender,EventArgs e) { 尝试 { if (!IsPostBack) { // 我的东西 } PopulateGridViewDetails(); } catch (例外) { throw ; } } 还有其他任何方式来实现这些东西(防止其他控件事件)? 解决方案 每次填充数据时,它都会注册该事件。为了安全起来 添加取消注册事件处理程序。 //删除事件到DropdownList ddlStatus.SelectedIndexChanged - = new EventHandler(GVDropDownSelectIndexChanged); //将事件添加到DropdownList ddlStatus.SelectedIndexChanged + = new EventHandler(GVDropDownSelectIndexChanged); I am adding dropdownlist to gridview by program. when i select drowdown item first time it working fine(Event fire only once) but in next time i select another dropdownlist item then my event(GVDropDownSelectIndexChanged) fire twice (n times according to previous selected dropdown). below is my code. can any body give me solution to this problem.thanx in advanced. My Codepublic void PopulateGridViewDetails(){int row1 = 0; foreach (GridViewRow rows in GridView1.Rows) { for (int cCount = 0; cCount < dataTable.Columns.Count; cCount++) { if (cCount < 3) { Label l = new Label(); l.ID = "gvlbl" + dataTable.Columns[cCount].ColumnName; l.Text = dataTable.Rows[row1][cCount].ToString(); if (l.ID == "gvlblTarineeId") { l.Visible = true; } if (cCount == 0) { GridView1.HeaderRow.Cells[cCount].Visible = false; rows.Cells[cCount].Visible = false; } rows.Cells[cCount].Controls.Add(l); } else { if (dataTable.Columns[cCount].ColumnName.ToLower().Contains("status")) { //adding dropdownlist to grid view DropDownList ddlStatus = new DropDownList(); ddlStatus.ID = "ddl" + dataTable.Columns[cCount].ColumnName + "_" + row1; ddlStatus.DataSource = new StudentExamStatusDAO().GetExamStatusList(); ddlStatus.DataTextField = "Status"; ddlStatus.DataValueField = "Id"; ddlStatus.DataBind(); ddlStatus.Items.Add(new ListItem("--Select Status--", "0")); ddlStatus.SelectedValue = "0"; //adding Event to DropdownList ddlStatus.SelectedIndexChanged += new EventHandler(GVDropDownSelectIndexChanged); ddlStatus.AutoPostBack = true; rows.Cells[cCount].Controls.Add(ddlStatus); GridView1.HeaderRow.Cells[cCount].Text = ""; } else { TextBox t = new TextBox(); t.ID = "gvtxt" + dataTable.Columns[cCount].ColumnName; rows.Cells[cCount].Controls.Add(t); Label lID = new Label(); lID.ID = "gvlbl" + dataTable.Columns[cCount].ColumnName + "Id"; lID.Text = dataTable.Rows[row1][cCount].ToString(); lID.Visible = false; rows.Cells[cCount].Controls.Add(lID); } } } row1++; }}My Event which is fired n times (according to previous dropdownlist selection)protected void GVDropDownSelectIndexChanged(object sender, EventArgs e){ //MyStuff}Is there any sequencing problem.i Load gridview Data from pageLoad event without checking IsPostback propertyprotected void Page_Load(object sender, EventArgs e) { try { if (!IsPostBack) { //my stuff } PopulateGridViewDetails(); } catch (Exception) { throw; } }and is there any other way to achieve this stuff (prevent other controls event)? 解决方案 Every time the data is populated it registers the event. For safe end Add unregister Event Handler.//removing Event to DropdownListddlStatus.SelectedIndexChanged -= new EventHandler(GVDropDownSelectIndexChanged);//adding Event to DropdownListddlStatus.SelectedIndexChanged += new EventHandler(GVDropDownSelectIndexChanged); 这篇关于GridView的Dropdown在从中选择任何一个时同时触发SelectedIndexChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 00:29