简体   繁体   English

不能设置带有DropDownList的SelectedIndex的ASP.NET Web窗体用户控件

[英]ASP.NET web forms user control with DropDownList's SelectedIndex cannot be set

I have created a web forms user control with a DropDownList. 我用DropDownList创建了一个Web窗体用户控件。 I want to change SelectedIndex property of DropDownList1 to change the selected index. 我想更改DropDownList1 SelectedIndex属性以更改选定的索引。

WebUserControl1.ascx: WebUserControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.ControlUI.WebUserControl1" %>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>

WebUserControl1.ascx.cs: WebUserControl1.ascx.cs:

using System;
namespace WebApplication1.ControlUI
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (IsPostBack) return;
            for (int i = 1; i <= 5; i++) {
                DropDownList1.Items.Add("Test: " + i.ToString());
            }
        }

        public void SetSelectedIndex(int index) {
            DropDownList1.SelectedIndex = index;
        }
    }
}

Now I am using the user control in a page. 现在,我在页面中使用用户控件。

Default.aspx: Default.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<%@ Register Src="~/ControlUI/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<asp:Content ID="HeadContent" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <uc1:WebUserControl1 runat="server" id="WebUserControl1" />
</asp:Content>

Default.aspx.cs: Default.aspx.cs:

using System;
using System.Web.UI;
namespace WebApplication1
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e) {
            WebUserControl1.SetSelectedIndex(3);
        }
    }
}

This does not work. 这是行不通的。 It assigns -1 into SelectedIndex property of DropDownList1 . 它将-1分配给DropDownList1 SelectedIndex属性。 But the user control works if I add items into the DropDownList in the markup ( WebUserControl1.ascx ), rather than in the codebehind file ( WebUserControl1.ascx.cs ): 但是,如果将项目添加到标记中的DropDownList( WebUserControl1.ascx )中,而不是在代码隐藏文件( WebUserControl1.ascx.cs )中,则用户控件会起作用:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.ControlUI.WebUserControl1" %>
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>Test: 1</asp:ListItem>
    <asp:ListItem>Test: 2</asp:ListItem>
    <asp:ListItem>Test: 3</asp:ListItem>
    <asp:ListItem>Test: 4</asp:ListItem>
    <asp:ListItem>Test: 5</asp:ListItem>
</asp:DropDownList>

But I need to add items using the codebehind file, not in the markup file. 但是我需要使用代码隐藏文件(而不是标记文件)添加项目。 Why it is not working? 为什么不起作用? How to solve the problem? 如何解决问题?

The issue is that Page_Load for the page containing the user control (Default) executes before Page_Load for the user control (WebUserControl1). 问题是, Page_Load包含用户控件(默认)的页面之前执行Page_Load用户控件(WebUserControl1)。 Therefore, when SetSelectedIndex is invoked from the page, the drop down does not have any list item in it when the page is first built. 因此,从页面调用SetSelectedIndex时,在首次构建页面时,下拉列表中没有任何列表项。

You can solve the issue very simply by creating the list item for the drop down in the Init stage of the user control life cycle rather than in the Load stage: 您可以通过在用户控件生命周期的Init阶段而不是Load阶段为下拉列表创建列表项来非常简单地解决该问题:

protected void Page_Init(object sender, EventArgs e) {
  if (IsPostBack) return;
  for (int i = 1; i <= 5; i++) {
    DropDownList1.Items.Add("Test: " + i.ToString());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM