本文介绍了无法找到在gridview行中动态创建的dropdonlist控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在gridview rowdatabound中创建了动态下拉列表,请参阅下面的代码。 DropDownList ddlResult = new DropDownList(); ddlResult.ID = ddlResult; ddlResult.AutoPostBack = true ; ddlResult.SelectedIndexChanged + = ddlResult_SelectedIndexChanged; ddlResult.Items.Insert( 0 , new ListItem( PASS, 0\" )); ddlResult.Items.Insert( 1 , new ListItem( FAIL, 1\" )); DropDownList drpResult =(DropDownList)row.FindControl( ddlResult); 我在gridview下方有一个按钮,当我点击我想要访问的按钮并获得上述内容时下拉选择的值。如下面的代码里面点击事件 foreach (GridViewRow row in gvMark.Rows) { DropDownList drpResult =(DropDownList)row.FindControl( ddlResult); // 这里drpResult我的空白 结果= drpResult.SelectedValue.ToString(); } 任何建议。在此先感谢.. 我的尝试: DropDownList ddlResult = new DropDownList(); ddlResult.ID = ddlResult; ddlResult.AutoPostBack = true ; ddlResult.SelectedIndexChanged + = ddlResult_SelectedIndexChanged; ddlResult.Items.Insert( 0 , new ListItem( PASS, 0\" )); ddlResult.Items.Insert( 1 , new ListItem( FAIL, 1\" )); DropDownList drpResult =(DropDownList)row.FindControl( ddlResult); foreach (GridViewRow row in gvMark.Rows) { DropDownList drpResult =(DropDownList)row.FindControl( ddlResult) ; // 这里drpResult我的空白 结果= drpResult.SelectedValue.ToString(); } 解决方案 您刚刚创建了一个DropDownList对象,但实际上并没有将它添加到GridView单元格中。所以你真的没办法在回发中访问它。此外,您可能需要在RowCreated事件中创建控件。这是一个工作代码供您参考: ASPX < asp:content id = Content2 contentplaceholderid = MainContent runat = server xmlns:asp = #unknown > < asp:gridview id = GridView1 runat = server onrowcreated = GridView1_RowCreated > < 列 > < asp:templatefield > < itemtemplate > & lt; asp:占位符 id = PlaceHolder1 runat = server > < / asp:占位符 > < / itemtemplate > < / asp:templatefield > < / columns > < / asp:gridview > < asp:button id = Button1 runat = server onclick = Button1_Click text = 按钮 / > < / asp:content > 背后的代码: 使用系统; 使用 System.Web.UI.WebControls; 使用 System.Data; 命名空间 WebFormDemo { public partial class DynamicControlInGridView:System.Web.UI.Page { protected void Page_Load( object sender,EventArgs e){ if (!IsPostBack) BindGridView(); } protected void GridView1_RowCreated( object sender,GridViewRowEventArgs e){ if (e.Row.RowType == DataControlRowType.DataRow){ DropDownList ddlResult = new DropDownList(); ddlResult.ID = ddlResult; ddlResult.Items.Insert( 0 , new ListItem( PASS, 0\" )); ddlResult.Items.Insert( 1 , new ListItem( FAIL, 1\" )); PlaceHolder p =(PlaceHolder)e.Row.FindControl( PlaceHolder1); p.Controls.Add(ddlResult); // 您还可以将其添加到单元格集合中,如 TextBox tbox = new TextBox(); tbox.ID = TextBox1; e.Row.Cells [ 0 ]。Controls.Add(tbox); } } private void BindGridView(){ GridView1.DataSource = CreateDataSource(); GridView1.DataBind(); } public DataTable CreateDataSource(){ DataTable dt = 新 DataTable(); DataRow博士; dt.Columns.Add( new DataColumn( ID, typeof ( string ))); dt.Columns.Add( new DataColumn( 名称, typeof ( string ))); dt.Columns.Add( new DataColumn( 姓氏, typeof ( string ))); dr = dt.NewRow(); // 为每列添加值 dr [ ID] = 1 ; dr [ 名称] = Vincent; dr [ LastName] = Durano; dt.Rows.Add(dr); return dt; } 受保护 void Button1_Click( object sender,EventArgs e){ foreach (GridViewRow row in GridView1.Rows){ DropDownList dd1 =(DropDownList)row.FindControl( ddlResult); if (dd1!= null ){ Response.Write( 找到DropDown!); } TextBox tbox = row.FindControl( TextBox1) as TextBox; if (tbox!= null ){ Response.Write( 找到TextBox!); } } } } } I've created dynamic dropdownlist in gridview rowdatabound pls refer below code.DropDownList ddlResult = new DropDownList(); ddlResult.ID = "ddlResult"; ddlResult.AutoPostBack = true; ddlResult.SelectedIndexChanged += ddlResult_SelectedIndexChanged; ddlResult.Items.Insert(0, new ListItem("PASS", "0")); ddlResult.Items.Insert(1, new ListItem("FAIL", "1"));DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");I've a button below the gridview, when i click the button i wants to access and get the above dropdown selected value. like below code inside click eventforeach (GridViewRow row in gvMark.Rows) {DropDownList drpResult = (DropDownList)row.FindControl("ddlResult"); // Here "drpResult" i'm getting nullResult = drpResult.SelectedValue.ToString();}Any suggestion . thanks in advance..What I have tried:DropDownList ddlResult = new DropDownList(); ddlResult.ID = "ddlResult"; ddlResult.AutoPostBack = true; ddlResult.SelectedIndexChanged += ddlResult_SelectedIndexChanged; ddlResult.Items.Insert(0, new ListItem("PASS", "0")); ddlResult.Items.Insert(1, new ListItem("FAIL", "1"));DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");foreach (GridViewRow row in gvMark.Rows) {DropDownList drpResult = (DropDownList)row.FindControl("ddlResult"); // Here "drpResult" i'm getting nullResult = drpResult.SelectedValue.ToString();} 解决方案 You just created a DropDownList object but you never add it to the GridView cells actually. So there's really no way for you to access it on postbacks. Also you may need to create the control at RowCreated event instead. Here's a working code for your reference:ASPX<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" xmlns:asp="#unknown"> <asp:gridview id="GridView1" runat="server" onrowcreated="GridView1_RowCreated"> <columns> <asp:templatefield> <itemtemplate> <asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" /></asp:content>CODE BEHIND:using System;using System.Web.UI.WebControls;using System.Data;namespace WebFormDemo{ public partial class DynamicControlInGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) BindGridView(); } protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddlResult = new DropDownList(); ddlResult.ID = "ddlResult"; ddlResult.Items.Insert(0, new ListItem("PASS", "0")); ddlResult.Items.Insert(1, new ListItem("FAIL", "1")); PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1"); p.Controls.Add(ddlResult); //you could also add it to the cells collection like TextBox tbox = new TextBox(); tbox.ID = "TextBox1"; e.Row.Cells[0].Controls.Add(tbox); } } private void BindGridView() { GridView1.DataSource = CreateDataSource(); GridView1.DataBind(); } public DataTable CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("ID", typeof(string))); dt.Columns.Add(new DataColumn("Name", typeof(string))); dt.Columns.Add(new DataColumn("Lastname", typeof(string))); dr = dt.NewRow(); //add values to each columns dr["ID"] = 1; dr["Name"] = "Vincent"; dr["LastName"] = "Durano"; dt.Rows.Add(dr); return dt; } protected void Button1_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { DropDownList dd1 = (DropDownList)row.FindControl("ddlResult"); if (dd1 != null) { Response.Write("Found DropDown!"); } TextBox tbox = row.FindControl("TextBox1") as TextBox; if (tbox != null) { Response.Write("Found TextBox!"); } } } }} 这篇关于无法找到在gridview行中动态创建的dropdonlist控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 17:40