简体   繁体   English

在进行下拉列表选择时,SelectedIndexChanged不会触发

[英]SelectedIndexChanged not firing when dropdownlist selection made

I've got a test gridview where I'm attempting to place a numerical value into a textbox when the spelled out number is selected as a dropdown selection. 我有一个测试网格视图,当我选择拼写出的数字作为下拉选择时,我试图将数值放入文本框中。 The first row works fine but when it comes to rows 2 & 3 the event doesn't fire at all when any of the selections from the dropdownmenu are selected. 第一行工作正常,但是当涉及第2行和第3行时,当选择了dropdownmenu中的任何选项时,事件根本不会触发。

The code is pretty straight forward and I'm stumped as to what's going on here. 代码很直接,我很难过这里发生了什么。 I'm pretty sure It's something simple but at this point I'm just spinning my proverbial wheels. 我很确定这很简单但是在这一点上我只是旋转我的谚语。

Here's the ASP code (non essential code stripped out) 这是ASP代码(非基本代码被删除)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" />
            <asp:BoundField DataField="CustomerName" HeaderText="Customer Name" />
            <asp:TemplateField HeaderText="Number Value">
                <ItemTemplate>
                    <asp:DropDownList Width="50" runat="server" ID="ddlTest" AutoPostBack="true" ViewStateMode="Enabled" EnableViewState="true" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
<asp:TemplateField HeaderText="Result">
    <ItemTemplate>
        <asp:TextBox ID="txtTest" runat="server">

        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>


        </Columns>

   </asp:GridView>

And here's the code behind.... 这是代码背后的代码....

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;
        }

    }
}

On the selected index event handler have you tried this? 在选定的索引事件处理程序上你试过这个吗?

if (ddl.ClientID == ddl1.ClientID)

Your if clause is comparing two values that are the same 您的if子句正在比较两个相同的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下拉列表 selectedindexchanged 事件未触发 - Dropdownlist selectedindexchanged event is not firing Dropdownlist SelectedIndexChanged在每次回发时触发 - Dropdownlist SelectedIndexChanged firing on every postback DropDownList SelectedIndexChanged不触发(AutoPostBack =“ true”) - DropDownList SelectedIndexChanged not firing (AutoPostBack=“true”) 停止DropDownList SelectedIndexChanged事件在FormView命令上触发 - Stop DropDownList SelectedIndexChanged Event firing on FormView command 嵌套的ASP.NET DropDownList SelectedIndexChanged未触发 - Nested ASP.NET DropDownList SelectedIndexChanged Not Firing dropDownList的SelectedIndexChanged事件没有触发c# - SelectedIndexChanged event of dropDownList not firing c# dropDownList的SelectedIndexChanged事件未在更新面板中触发 - SelectedIndexChanged event of dropDownList not firing in Update Panel 当我单击C#中的清单框下拉列表时,为什么总是selectedindexchanged在触发? - Why always selectedindexchanged is firing when i click on checklistbox dropdownlist in c#? 会话结束和重新启动会阻止DropDownList_SelectedIndexChanged触发 - Session ending and restarting is preventing DropDownList_SelectedIndexChanged from firing ASP.NET MVC C#-DropDownList SelectedIndexChanged不触发 - Asp.net MVC C# - DropDownList SelectedIndexChanged not firing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM