1

私を助けてください。ユーザーコントロールのC#ファイルから選択したインデックスを取得していないため

4

3 に答える 3

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 に答える
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 に答える