ASP.NET / C#:DropDownList SelectedIndexChanged事件未触发
发布时间:2020-12-16 06:52:00 所属栏目:asp.Net 来源:网络整理
导读:当我在下拉列表中选择值时,我的selectedindexchanged事件未触发.这些下拉列表在以下代码中动态实现.我尝试更改autopostback和enableviewstate设置无济于事.我正在使用静态面板.有谁看到我怎么能导致selectedindexchanged事件被触发? using System;using Sys
当我在下拉列表中选择值时,我的selectedindexchanged事件未触发.这些下拉列表在以下代码中动态实现.我尝试更改autopostback和enableviewstate设置无济于事.我正在使用静态面板.有谁看到我怎么能导致selectedindexchanged事件被触发?
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ADONET_namespace; namespace AddFileToSQL { public partial class DataMatch : _Default { protected System.Web.UI.WebControls.PlaceHolder phTextBoxes; protected System.Web.UI.WebControls.PlaceHolder phDropDownLists; protected System.Web.UI.WebControls.Button btnAnotherRequest; protected System.Web.UI.WebControls.Panel pnlCreateData; protected System.Web.UI.WebControls.Literal lTextData; protected System.Web.UI.WebControls.Panel pnlDisplayData; //Panel pnlDropDownList; protected static string inputfile2; static string[] headers = null; static string[] data = null; static string[] data2 = null; static DataTable myInputFile = new DataTable("MyInputFile"); static string[] myUserSelections; // a Property that manages a counter stored in ViewState protected int NumberOfControls { get { return (int)ViewState["NumControls"]; } set { ViewState["NumControls"] = value; } } public void EditRecord(object recordID) { SelectedRecordID = recordID; // Load record from database and show in control } protected object SelectedRecordID { get { return ViewState["SelectedRecordID"]; } set { ViewState["SelectedRecordID"] = value; } } // Page Load private void Page_Load(object sender,System.EventArgs e) { if (!Page.IsPostBack) { this.NumberOfControls = 0; } } // Add DropDownList Control to Placeholder private void CreateDropDownLists() { for (int counter = 0; counter < NumberOfControls; counter++) { DropDownList ddl = new DropDownList(); SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable); ddl.ID = "DropDownListID" + (counter + 1).ToString(); ddl.DataTextField = "COLUMN_NAME"; ddl.DataValueField = "COLUMN_NAME"; ddl.DataSource = dr; ddl.DataBind(); //myUserSelections[counter] = ""; ddl.AutoPostBack = true; ddl.EnableViewState = true; //Preserves View State info on Postbacks ddl.Style["position"] = "absolute"; ddl.Style["top"] = 100 * counter + 80 + "px"; ddl.Style["left"] = 250 + "px"; ddl.SelectedIndexChanged += new EventHandler(SelectedIndexChanged); pnlDisplayData.Controls.Add(ddl); pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>")); pnlDisplayData.Visible = true; // pnlDropDownList.FindControl(ddl.ID); dr.Close(); } } protected void SelectedIndexChanged(object sender,EventArgs e) { DropDownList ddl = (DropDownList)sender; string ID = ddl.ID; } // Add TextBoxes Control to Placeholder private void RecreateDropDownLists() { for (int counter = 0; counter < NumberOfControls; counter++) { DropDownList ddl = new DropDownList(); SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable); ddl.ID = "DropDownListID" + (counter + 1).ToString(); ddl.DataTextField = "COLUMN_NAME"; ddl.DataValueField = "COLUMN_NAME"; ddl.DataSource = dr; ddl.DataBind(); myUserSelections[counter] = ""; dr.Close(); ddl.AutoPostBack = true; ddl.EnableViewState = false; //Preserves View State info on Postbacks ddl.Style["position"] = "absolute"; ddl.Style["top"] = 100 * counter + 80 + "px"; ddl.Style["left"] = 250 + "px"; pnlDisplayData.Controls.Add(ddl); pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>")); } } // Create TextBoxes and DropDownList data here on postback. protected override void CreateChildControls() { // create the child controls if the server control does not contains child controls this.EnsureChildControls(); // Creates a new ControlCollection. this.CreateControlCollection(); // Here we are recreating controls to persist the ViewState on every post back if (Page.IsPostBack) { RecreateDropDownLists(); RecreateLabels(); } // Create these conrols when asp.net page is created else { PopulateFileInputTable(); CreateDropDownLists(); CreateLabels(); } // Prevent child controls from being created again. this.ChildControlsCreated = true; } // Read all the data from TextBoxes and DropDownLists protected void btnSubmit_Click(object sender,System.EventArgs e) { int cnt = FindOccurence("DropDownListID"); EditRecord("DropDownListID" + Convert.ToString(cnt + 1)); AppendRecords(); pnlDisplayData.Visible = false; } private int FindOccurence(string substr) { string reqstr = Request.Form.ToString(); return ((reqstr.Length - reqstr.Replace(substr,"").Length) / substr.Length); } } } 解决方法
你没有在RecreateDropDownLists()方法中分配一个事件处理程序:)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net中的.ashx文件?
- asp.net-mvc – 将我的网站重定向到mvc中的默认控制器
- 将webforms混合到ASP.NET MVC应用程序中的问题
- asp.net-mvc – Kendo UI Grid – 显示行号
- 从ASP.NET Core Web API将多GB文件流式传输到AWS S3
- asp.net-mvc – StackExchange站点如何关联用户帐户和OpenI
- asp.net 操作FTP作为数据中转-Helper.cs
- asp.net-core – 如何获取ASP.NET Core中的中间件列表
- 在剃刀mvc 4 rc清空第一行
- asp.net-mvc – Azure和MVC2 Web角色模板
推荐文章
站长推荐
- 为什么我不能将ASP.NET Framework版本显示为3.5?
- asp.net-mvc-3 – MVC3 / Razor:cshtml.Execute
- Asp.net System.Web.HttpContext.Current.Sessio
- asp.net-mvc – MVC模式DDD模式
- asp.net – TransferRequestHandler和BeginReque
- asp.net-mvc – 什么首先启动Application_Start或
- asp.net – 从MVC视图中的模型访问displayName属
- 如何在ASP.net MVC中正确执行异步方法?
- ASP.NET捆绑和缩小 – CSS3属性失败
- asp.net-mvc – Asp.Net Mvc – Html.TextBox –
热点阅读