本文介绍了如何从更新面板中的动态控件获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我已经在下面给出了添加动态控件的代码,如何从这里获取值 DropDownList ddlAgent = new DropDownList(); ddlAgent.EnableViewState = false ; ddlAgent.CssClass = RegsSelect; ddlAgent.ID = ddlAgent; ddlAgent.ClientIDMode = ClientIDMode.Static; ddlAgent.Attributes.Add( style, margin-left:-4px; height:40px; width:328px;); upText.ContentTemplateContainer.Controls.Add(ddlAgent); // 使用下拉列表绑定数据 CUser objUser = new CUser(); ddlAgent.DataSource = objUser.GetUserByRoleId( 4 ); ddlAgent.DataValueField = Id; ddlAgent.DataTextField = FirstName; ddlAgent.DataBind(); 现在获取控制权 DropDownList ddlAgent =(DropDownList)upText.ContentTemplateContainer.FindControl( ddlAgent); 给出空参考例外 它是从上一个下拉列表的选定索引创建的,它放在updatepanel中解决方案 始终在Page_Init事件中创建动态控件(阅读此内容 [ ^ ]和此 [ ^ ]获取详细信息)。 更改您的CS。试试这个: // 在此创建控件页面事件 受保护 void Page_Init( object sender,EventArgs e) { DropDownList ddlAgent = new DropDownList(); ddlAgent.EnableViewState = false ; ddlAgent.CssClass = RegsSelect; ddlAgent.ID = ddlAgent; ddlAgent.ClientIDMode = ClientIDMode.Static; ddlAgent.Attributes.Add( style, margin-left:-4px; height:40px; width:328px;); upText.ContentTemplateContainer.Controls.Add(ddlAgent); } 现在,找到控件并将其绑定在您的函数或方法中。试试这个: DropDownList ddlAgent =(DropDownList)upText.ContentTemplateContainer.FindControl( ddlAgent); // 现在您将无法获得null referance异常 // 使用下拉列表绑定数据 CUser objUser = new CUser(); ddlAgent.DataSource = objUser.GetUserByRoleId( 4 ); ddlAgent.DataValueField = Id; ddlAgent.DataTextField = FirstName; ddlAgent.DataBind(); - Amit I have given code for adding dynamic Control below , how to get value from this DropDownList ddlAgent = new DropDownList(); ddlAgent.EnableViewState = false; ddlAgent.CssClass = "RegsSelect"; ddlAgent.ID = "ddlAgent"; ddlAgent.ClientIDMode = ClientIDMode.Static; ddlAgent.Attributes.Add("style", "margin-left:-4px;height:40px;width:328px;"); upText.ContentTemplateContainer.Controls.Add(ddlAgent); //Binding data with dropdownlist CUser objUser = new CUser(); ddlAgent.DataSource = objUser.GetUserByRoleId(4); ddlAgent.DataValueField = "Id"; ddlAgent.DataTextField = "FirstName"; ddlAgent.DataBind(); now to get control DropDownList ddlAgent=(DropDownList) upText.ContentTemplateContainer.FindControl("ddlAgent"); which gives Null Reference Exceptionit is created from selected index of previous dropdownlist and it is placed in updatepanel 解决方案 Always create your dynamic control in Page_Init event (Read this[^] and this[^] for a detailed information).Change your CS. Try this://Create the controls in this page eventprotected void Page_Init(object sender, EventArgs e){ DropDownList ddlAgent = new DropDownList(); ddlAgent.EnableViewState = false; ddlAgent.CssClass = "RegsSelect"; ddlAgent.ID = "ddlAgent"; ddlAgent.ClientIDMode = ClientIDMode.Static; ddlAgent.Attributes.Add("style", "margin-left:-4px;height:40px;width:328px;"); upText.ContentTemplateContainer.Controls.Add(ddlAgent);}Now, find the control and bind it in your function or method. Try this:DropDownList ddlAgent=(DropDownList) upText.ContentTemplateContainer.FindControl("ddlAgent"); //Now you won't get a null referance exception//Binding data with dropdownlistCUser objUser = new CUser();ddlAgent.DataSource = objUser.GetUserByRoleId(4);ddlAgent.DataValueField = "Id";ddlAgent.DataTextField = "FirstName";ddlAgent.DataBind();--Amit 这篇关于如何从更新面板中的动态控件获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 23:14