none
在外部获取Repeater 内 DropDownList1 的选择值,一直找不到, RRS feed

  • 问题

  • 在外部获取Repeater 内 DropDownList1 的选择值,一直找不到;怎么弄的:帮帮忙,谢了,很急用!。。
    小吴
      protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    
        {
    
            DropDownList dr = e.Item.FindControl("touxiang") as DropDownList;
    
            dr.SelectedIndexChanged += new EventHandler(dr_SelectedIndexChanged);
    
    }
    
    
    
       public   void dr_SelectedIndexChanged(object sender, EventArgs e)
    
        {  
    
           
    
            Session["u_image"] = touxiang.SelectedItem.Value;
    
            throw new Exception("The method or operation is not 
    
    implemented.");
    
    
    
    //这里找不到 touxiang.SelectedItem.Value;的值
    
        }
    
    
         <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>  <asp:DropDownList runat="server" ID="touxiang" AutoPostBack="True" Width="79px" OnSelectedIndexChanged="dr_SelectedIndexChanged" > <asp:ListItem Text="头像1" Value="../touxiang/1.gif"></asp:ListItem> <asp:ListItem Text="头像2" Value="../touxiang/2.gif"></asp:ListItem> <asp:ListItem Text="头像3" Value="../touxiang/3.gif"></asp:ListItem> <asp:ListItem Text="头像4" Value="../touxiang/4.gif"></asp:ListItem> </asp:DropDownList >
          </ItemTemplate>
            </asp:Repeater>

    2009年5月23日 3:09

答案

  • 小吴你好,我写了一个示例通过两种方法解决你的问题,你可以参考一下

    ----------------------------------------------------------------------------------------------------------------
    .aspx
    ----------------------------------------------------------------------------------------------------------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200905231324.aspx.cs" Inherits="Repeaters_X200905231324" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Repeater ID="fRepeater" runat="server" onitemcommand="fRepeater_ItemCommand">
        <ItemTemplate>  
            Key:<asp:Label ID="fEmployeeId" Text='<%# Eval("EmployeeKey") %>' runat="server" ></asp:Label><br />
            Name:<asp:Label ID="fEmployeeName" Text='<%# Eval("Name") %>' runat="server" ></asp:Label><br />
            ModifyDate:<asp:Label ID="fEmployeeModifyDate" Text='<%# Eval("ModifyDate") %>' runat="server" ></asp:Label><br />
            Picture:<asp:DropDownList ID="fDropDownList" runat="server" Width="79px" AutoPostBack="true" OnSelectedIndexChanged="SelectedIndexChanged" >
                <asp:ListItem Text="头像1" Value="../touxiang/1.gif"></asp:ListItem>
                <asp:ListItem Text="头像2" Value="../touxiang/2.gif"></asp:ListItem>
                <asp:ListItem Text="头像3" Value="../touxiang/3.gif"></asp:ListItem>
                <asp:ListItem Text="头像4" Value="../touxiang/4.gif"></asp:ListItem>
            </asp:DropDownList >
            <br />
            <asp:Button  CommandName="Command1" Text="Command1" runat="server"/>
            <br />
            -----------------------------------------------------------------
            <br />
        </ItemTemplate>  
        </asp:Repeater>
        </div>
        </form>
    </body>
    </html>
    
    
    
    ----------------------------------------------------------------------------------------------------------------
    .aspx.cs
    ----------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class Repeaters_X200905231324 : System.Web.UI.Page
    {
        #region " DataSource "
    
        public DataTable Employee
        {
            get
            {
                if (Session["Employee"] == null)
                {
                    DataTable fEmployee = new DataTable();
    
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(Int32)));
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(String)));
                    fEmployee.Columns.Add(new DataColumn("ModifyDate", typeof(DateTime)));
    
                    fEmployee.Rows.Add(new object[] { 1, "John", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 2, "Tomi", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 3, "X.XY", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 4, "Sam", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 5, "Mary", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 6, "Chirs", DateTime.Now });
    
                    Session["Employee"] = fEmployee;
                }
    
                return (DataTable)Session["Employee"];
            }
        }
    
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                this.BindRepeater();
        }
    
        private void BindRepeater()
        {
            this.fRepeater.DataSource = this.Employee;
            this.fRepeater.DataBind();
        }
    
        protected void fRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Command1":
                    Control fControl = e.CommandSource as Control;
                    RepeaterItem fCurItem = fControl.NamingContainer as RepeaterItem;
                    this.WriteMsg(fCurItem);
                    break;
            }
        }
    
        private void WriteMsg(RepeaterItem fItem)
        {
            string fKey = ((Label)fItem.FindControl("fEmployeeId")).Text;
            string fName = ((Label)fItem.FindControl("fEmployeeName")).Text;
            string fModifyDate = ((Label)fItem.FindControl("fEmployeeModifyDate")).Text;
            string fSelectText = ((DropDownList)fItem.FindControl("fDropDownList")).SelectedItem.Text;
            this.WriteMsg(fKey, fName, fModifyDate, fSelectText);
        }
    
        private void WriteMsg(string fEmployeeKey, string fName, string fModifyDate, string fSelectedText)
        {
            object[] fArray = new object[] { fEmployeeKey, fName, fModifyDate, fSelectedText };
            string fMsg = string.Format("Key: {0} <br /> Name: {1} <br /> ModifyDate: {2} <br /> SelectedText: {3} <br />", fArray);
            Response.Write("=================================================");
            Response.Write("<br />");
            Response.Write(fMsg);
            Response.Write("=================================================");
            Response.Write("<br />");
        }
    
        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList fDropDownList = (DropDownList)sender;
            RepeaterItem fCurItem = (RepeaterItem)fDropDownList.Parent;
            this.WriteMsg(fCurItem);
        }
    }
    
    
    


    知识改变命运,奋斗成就人生!
    2009年5月23日 6:14
    版主
  • public   void dr_SelectedIndexChanged(object sender, EventArgs e)
    {  
    	DropDownList ddl = (DropDownList)sender;       
    	Session["u_image"] = ddl.SelectedItem.Value;
    }


    知识改变命运,奋斗成就人生!
    2009年5月23日 3:29
    版主

全部回复

  • public   void dr_SelectedIndexChanged(object sender, EventArgs e)
    {  
    	DropDownList ddl = (DropDownList)sender;       
    	Session["u_image"] = ddl.SelectedItem.Value;
    }


    知识改变命运,奋斗成就人生!
    2009年5月23日 3:29
    版主
  • 谢谢X.X.Y !   呵呵.......
    小吴
    2009年5月23日 3:44
  • 还有点问题.......
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">        <ItemTemplate>  
       <asp:DropDownList runat="server" ID="touxiang"   AutoPostBack="True" Width="79px"   OnSelectedIndexChanged="dr_SelectedIndexChanged" >
    
                          <asp:ListItem Text="头像1" Value="../touxiang/1.gif"></asp:ListItem>
    
                          <asp:ListItem Text="头像2" Value="../touxiang/2.gif"></asp:ListItem>
    
                          <asp:ListItem Text="头像3" Value="../touxiang/3.gif"></asp:ListItem>
    
                          <asp:ListItem Text="头像4" Value="../touxiang/4.gif"></asp:ListItem>
    
     </asp:DropDownList >
    
    <asp:lable  id="www" runat="server"  text="<%#Eval("bh")%>"></lable>
          </ItemTemplate>  
          </asp:Repeater>
    
    
    
    public   void dr_SelectedIndexChanged(object sender, EventArgs e)
    {  
    	DropDownList ddl = (DropDownList)sender;       
    	Session["u_image"] = ddl.SelectedItem.Value;
      我想在这里引用  <asp:lable    id="www"  的值,可以吗。怎么弄呢?再麻烦一次啦!
    }
    
    
    









    知识改变命运,奋斗成就人生!


    小吴
    2009年5月23日 4:00
  • 还有点问题.......
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">        <ItemTemplate>  
    
       <asp:DropDownList runat="server" ID="touxiang"   AutoPostBack="True" Width="79px"   OnSelectedIndexChanged="dr_SelectedIndexChanged" >
    
    
    
                          <asp:ListItem Text="头像1" Value="../touxiang/1.gif"></asp:ListItem>
    
    
    
                          <asp:ListItem Text="头像2" Value="../touxiang/2.gif"></asp:ListItem>
    
    
    
                          <asp:ListItem Text="头像3" Value="../touxiang/3.gif"></asp:ListItem>
    
    
    
                          <asp:ListItem Text="头像4" Value="../touxiang/4.gif"></asp:ListItem>
    
    
    
     </asp:DropDownList >
    
    
    
    <asp:lable  id="www" runat="server"  text="<%#Eval("bh")%>"></lable>
    
          </ItemTemplate>  
    
          </asp:Repeater>
    
    
    
    
    
    
    
    public   void dr_SelectedIndexChanged(object sender, EventArgs e)
    
    {  
    
    	DropDownList ddl = (DropDownList)sender;       
    
    	Session["u_image"] = ddl.SelectedItem.Value;
    
      我想在这里引用  <asp:lable    id="www"  的值,可以吗。怎么弄呢?再麻烦一次啦!
    
    }
    
    
    
    
    
    









    知识改变命运,奋斗成就人生!


    小吴

    快来帮帮我呀,怎么啦:
    小吴
    2009年5月23日 5:00
  • 小吴你好,我写了一个示例通过两种方法解决你的问题,你可以参考一下

    ----------------------------------------------------------------------------------------------------------------
    .aspx
    ----------------------------------------------------------------------------------------------------------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200905231324.aspx.cs" Inherits="Repeaters_X200905231324" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Repeater ID="fRepeater" runat="server" onitemcommand="fRepeater_ItemCommand">
        <ItemTemplate>  
            Key:<asp:Label ID="fEmployeeId" Text='<%# Eval("EmployeeKey") %>' runat="server" ></asp:Label><br />
            Name:<asp:Label ID="fEmployeeName" Text='<%# Eval("Name") %>' runat="server" ></asp:Label><br />
            ModifyDate:<asp:Label ID="fEmployeeModifyDate" Text='<%# Eval("ModifyDate") %>' runat="server" ></asp:Label><br />
            Picture:<asp:DropDownList ID="fDropDownList" runat="server" Width="79px" AutoPostBack="true" OnSelectedIndexChanged="SelectedIndexChanged" >
                <asp:ListItem Text="头像1" Value="../touxiang/1.gif"></asp:ListItem>
                <asp:ListItem Text="头像2" Value="../touxiang/2.gif"></asp:ListItem>
                <asp:ListItem Text="头像3" Value="../touxiang/3.gif"></asp:ListItem>
                <asp:ListItem Text="头像4" Value="../touxiang/4.gif"></asp:ListItem>
            </asp:DropDownList >
            <br />
            <asp:Button  CommandName="Command1" Text="Command1" runat="server"/>
            <br />
            -----------------------------------------------------------------
            <br />
        </ItemTemplate>  
        </asp:Repeater>
        </div>
        </form>
    </body>
    </html>
    
    
    
    ----------------------------------------------------------------------------------------------------------------
    .aspx.cs
    ----------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class Repeaters_X200905231324 : System.Web.UI.Page
    {
        #region " DataSource "
    
        public DataTable Employee
        {
            get
            {
                if (Session["Employee"] == null)
                {
                    DataTable fEmployee = new DataTable();
    
                    fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(Int32)));
                    fEmployee.Columns.Add(new DataColumn("Name", typeof(String)));
                    fEmployee.Columns.Add(new DataColumn("ModifyDate", typeof(DateTime)));
    
                    fEmployee.Rows.Add(new object[] { 1, "John", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 2, "Tomi", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 3, "X.XY", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 4, "Sam", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 5, "Mary", DateTime.Now });
                    fEmployee.Rows.Add(new object[] { 6, "Chirs", DateTime.Now });
    
                    Session["Employee"] = fEmployee;
                }
    
                return (DataTable)Session["Employee"];
            }
        }
    
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                this.BindRepeater();
        }
    
        private void BindRepeater()
        {
            this.fRepeater.DataSource = this.Employee;
            this.fRepeater.DataBind();
        }
    
        protected void fRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Command1":
                    Control fControl = e.CommandSource as Control;
                    RepeaterItem fCurItem = fControl.NamingContainer as RepeaterItem;
                    this.WriteMsg(fCurItem);
                    break;
            }
        }
    
        private void WriteMsg(RepeaterItem fItem)
        {
            string fKey = ((Label)fItem.FindControl("fEmployeeId")).Text;
            string fName = ((Label)fItem.FindControl("fEmployeeName")).Text;
            string fModifyDate = ((Label)fItem.FindControl("fEmployeeModifyDate")).Text;
            string fSelectText = ((DropDownList)fItem.FindControl("fDropDownList")).SelectedItem.Text;
            this.WriteMsg(fKey, fName, fModifyDate, fSelectText);
        }
    
        private void WriteMsg(string fEmployeeKey, string fName, string fModifyDate, string fSelectedText)
        {
            object[] fArray = new object[] { fEmployeeKey, fName, fModifyDate, fSelectedText };
            string fMsg = string.Format("Key: {0} <br /> Name: {1} <br /> ModifyDate: {2} <br /> SelectedText: {3} <br />", fArray);
            Response.Write("=================================================");
            Response.Write("<br />");
            Response.Write(fMsg);
            Response.Write("=================================================");
            Response.Write("<br />");
        }
    
        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList fDropDownList = (DropDownList)sender;
            RepeaterItem fCurItem = (RepeaterItem)fDropDownList.Parent;
            this.WriteMsg(fCurItem);
        }
    }
    
    
    


    知识改变命运,奋斗成就人生!
    2009年5月23日 6:14
    版主
  • 你好,X.X.Y ,谢谢你,呵呵!


    小吴
    2009年5月23日 13:46