1

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

4

3 回答 3

1

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

在用户控制代码隐藏文件中

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

或者

在用户控件使用的 aspx 页面的代码隐藏文件中

int index = (usercontrolid.FindControl("dropdownid") as DropDownList).SelectedIndex;
于 2012-10-10T07:21:28.453 回答
1

怎么样

MyUserControl.ItsDropdown.SelectedIndex
于 2012-10-10T07:22:38.093 回答
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);
    }



}
于 2012-10-10T07:30:21.263 回答