如何从C#文件中获取下拉列表的用户控件中的选定索引

时间:2012-10-10 07:19:11

标签: c# asp.net

请帮帮我。因为我没有从用户控件中的c#文件中获取所选索引

3 个答案:

答案 0 :(得分:1)

您需要将其作为属性从用户控件公开,或者您需要使用find控件来获取作为用户控件一部分的下拉列表的选定索引。

在usercontrol代码隐藏文件

public int drpSlctedIndex
{
  get 
  {
    return dropdownid.SelectedIndex;
  }
}

在aspx页面的codebehind文件中,其中usercontrol使用

int index = (usercontrolid.FindControl("dropdownid") as DropDownList).SelectedIndex;

答案 1 :(得分:1)

怎么样

MyUserControl.ItsDropdown.SelectedIndex

答案 2 :(得分:1)

//ASPX page
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //accessing the SelectedIndex
      int dl= ((DropDownList)this.userControlName.FindControl("DropDownList1")).SelectedIndex;
    }
}

或您想要访问索引的任何地方。

这是TimerUserControl和Aspx页面的代码。我尽力向你解释我的理解。希望这会有所帮助。

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TimeUserControl.ascx.cs" Inherits="TimeUserControl" %>
hour:<asp:DropDownList ID="hour" runat="server">
    <asp:ListItem>0</asp:ListItem>
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
</asp:DropDownList>

<p>
    &nbsp;</p>
Minute:<asp:DropDownList ID="Minute" runat="server">
    <asp:ListItem>0</asp:ListItem>
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
</asp:DropDownList>

<p>
    &nbsp;</p>
Seconds:<asp:DropDownList ID="Seconds" runat="server">
    <asp:ListItem>0</asp:ListItem>
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
</asp:DropDownList>

TimerUserControl背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TimeUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string SelectedTime
    {
        get
        {
            string _time = this.hour.SelectedItem.Text + ":" + this.Minute.SelectedItem.Text + " " + this.Seconds.SelectedItem.Text;
            return _time;
        }
    }

}

Aspx页面背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//ASPX page
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     // Accessing on Page Load with AutoPostBack Property to True for DropDownLists.
    //Every time you select any value from DropDownList the Page will Post back and Selected 
   // value will be in Response.
        string selectedtime = this.TimerUserControl.SelectedTime;
     Response.Write("Time----->" + selectedtime);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //accessing the selected time property if the SelectedTime(Public) property is in User control 
        // Here you wont require to set the AutoPostBack Property  to true for DropDownLists.

        string selectedtime = this.TimerUserControl.SelectedTime;

        Response.Write("Time----->" + selectedtime);
    }



}