无法访问在gridview行中动态创建的dropdonlist [英] Cant able to access the dropdonlist which dynamically created in gridview row

查看:75
本文介绍了无法访问在gridview行中动态创建的dropdonlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridview中创建了动态下拉列表rowdatabound请参考下面的代码。



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"));







我在gridview下方有一个按钮,我单击我想要访问的按钮,并获得上面的下拉选择值。如下面的代码里面点击事件






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 event

foreach (GridViewRow row in gvMark.Rows)
                {
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");

// Here "drpResult" i'm getting null

Result = drpResult.SelectedValue.ToString();





我尝试过:





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"));













foreach (GridViewRow row in gvMark.Rows)
                {
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");

// Here "drpResult" i'm getting null

Result = drpResult.SelectedValue.ToString();

推荐答案

据我所见,你永远不会添加DropDownList控制在第一位的行。尝试将下拉列表添加到 GridView.RowCreated事件(System.Web.UI.WebControls) [ ^ ]。



换句话说

As far as I can see you never add the DropDownList control in the row in the first place. Try adding the drop down list into the controls collection of the cell in GridView.RowCreated Event (System.Web.UI.WebControls)[^].

In other words
e.Row.Cells[colnumber].Controls.Add(ddlResult);


这篇关于无法访问在gridview行中动态创建的dropdonlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆