Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectedIndexChanged在进行dropdownlist选择时不触发_C#_Asp.net_Gridview - Fatal编程技术网

C# SelectedIndexChanged在进行dropdownlist选择时不触发

C# SelectedIndexChanged在进行dropdownlist选择时不触发,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个测试gridview,当拼写的数字被选择为下拉选择时,我试图将一个数值放入文本框中。第一行工作正常,但当涉及到第2行和第3行时,当从下拉菜单中选择任何选项时,事件根本不会触发 代码非常直截了当,我对这里发生的事情感到困惑。我很确定这是件很简单的事情,但在这一点上,我只是在旋转我众所周知的轮子 下面是ASP代码(去掉非必要代码) 这是背后的代码 using System; using System.Configuration; using System.Collections; usi

我有一个测试gridview,当拼写的数字被选择为下拉选择时,我试图将一个数值放入文本框中。第一行工作正常,但当涉及到第2行和第3行时,当从下拉菜单中选择任何选项时,事件根本不会触发

代码非常直截了当,我对这里发生的事情感到困惑。我很确定这是件很简单的事情,但在这一点上,我只是在旋转我众所周知的轮子

下面是ASP代码(去掉非必要代码)


这是背后的代码

using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

namespace Web2008
{

    public partial class GridViewDropDown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                List<Customer> lst = new List<Customer>();
                Customer cust1 = new Customer();
                cust1.CustomerID = 1;
                cust1.CustomerName = "Customer1";
                Customer cust2 = new Customer();
                cust2.CustomerID = 2;
                cust2.CustomerName = "Customer2";
                Customer cust3 = new Customer();
                cust3.CustomerID = 3;
                cust3.CustomerName = "Customer3";

                lst.Add(cust1);
                lst.Add(cust2);
                lst.Add(cust3);
                GridView1.DataSource = lst;
                GridView1.DataBind();
            }
        }
        protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropDownList ddl = sender as DropDownList;
            foreach (GridViewRow row in GridView1.Rows)
            {

                Control ctrl = row.FindControl("ddlTest") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;

                    if (ddl.ClientID == ddl.ClientID)
                    {
                        TextBox txt = row.FindControl("txtTest") as TextBox;
                        txt.Text = ddl1.SelectedValue;
                        break;

                    }
                }
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Response.Write(e.CommandName);
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Control ctrl = e.Row.FindControl("ddlTest");
                if (ctrl != null)
                {
                    DropDownList dd = ctrl as DropDownList;

                    List<DropDownData> lst = new List<DropDownData>();
                    DropDownData cust1 = new DropDownData(1, "One");
                    DropDownData cust2 = new DropDownData(2, "Two");
                    DropDownData cust3 = new DropDownData(3, "Thres");

                    lst.Add(cust1);
                    lst.Add(cust2);
                    lst.Add(cust3);

                    dd.DataTextField = "Text";
                    dd.DataValueField = "ID";
                    dd.DataSource = lst;
                    dd.DataBind();
                }
            }
        }

        public class DropDownData
        {
            public DropDownData(int id, string displaytext)
            {
                iD = id;
                Text = displaytext;
            }
            int iD;
            public int ID
            {
                get { return iD; }
                set { iD = value; }
            }
            string text;
            public string Text
            {
                get { return text; }
                set { text = value; }
            }
        }

    public class Customer
        {
        public int CustomerID
            {
                get;
                set;
            }

            public string CustomerName { get; set; }
        }

        public string CustomerName
        {
            get;
            set;
        }

    }
}
使用系统;
使用系统配置;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用系统数据;
使用System.Data.Sql;
使用System.Data.SqlClient;
使用System.Web.UI;
使用System.Web.Security;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.WebControl;
名称空间Web2008
{
公共部分类GridView下拉列表:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
List lst=新列表();
客户cust1=新客户();
cust1.CustomerID=1;
cust1.CustomerName=“Customer1”;
客户cust2=新客户();
cust2.CustomerID=2;
cust2.CustomerName=“Customer2”;
客户cust3=新客户();
cust3.CustomerID=3;
cust3.CustomerName=“Customer3”;
第一次增补(客户1);
第一次增补(客户2);
第一次增补(客户3);
GridView1.DataSource=lst;
GridView1.DataBind();
}
}
受保护的无效DDLSetest\u SelectedIndexChanged(对象发送方,事件参数e)
{
DropDownList ddl=发送方作为DropDownList;
foreach(GridView1.Rows中的GridViewRow行)
{
Control ctrl=row.FindControl(“ddlTest”)作为DropDownList;
如果(ctrl!=null)
{
DropDownList ddl1=(DropDownList)ctrl;
if(ddl.ClientID==ddl.ClientID)
{
TextBox txt=row.FindControl(“txtest”)作为TextBox;
txt.Text=ddl1.SelectedValue;
打破
}
}
}
}
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e)
{
响应。写入(如CommandName);
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
Control ctrl=e.Row.FindControl(“ddlTest”);
如果(ctrl!=null)
{
DropDownList dd=ctrl作为DropDownList;
List lst=新列表();
DropDownData cust1=新的DropDownData(1,“一”);
DropDownData cust2=新的DropDownData(2,“两”);
DropDownData cust3=新的DropDownData(3,“Thres”);
第一次增补(客户1);
第一次增补(客户2);
第一次增补(客户3);
dd.DataTextField=“Text”;
dd.DataValueField=“ID”;
dd.DataSource=lst;
dd.DataBind();
}
}
}
公共类下拉数据
{
公共下拉数据(int-id,string-displaytext)
{
iD=iD;
文本=显示文本;
}
int-iD;
公共整数ID
{
获取{return iD;}
设置{iD=value;}
}
字符串文本;
公共字符串文本
{
获取{返回文本;}
设置{text=value;}
}
}
公共类客户
{
公共int客户ID
{
得到;
设置
}
公共字符串CustomerName{get;set;}
}
公共字符串客户名称
{
得到;
设置
}
}
}

在选定的索引事件处理程序上,您是否尝试过此操作

if (ddl.ClientID == ddl1.ClientID)
您的if子句正在比较两个相同的值(反复砰砰地一声),即:叹息:谢谢上帝!
if (ddl.ClientID == ddl1.ClientID)