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

时间:2013-12-27 23:21:38

标签: c# asp.net gridview

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

代码很直接,我很难过这里发生了什么。我很确定这很简单,但此时我只是在转动我的谚语。

这是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>

这是背后的代码......

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

    }
}

1 个答案:

答案 0 :(得分:2)

在选定的索引事件处理程序上你试过这个吗?

if (ddl.ClientID == ddl1.ClientID)

你的if子句正在比较两个相同的值