0

I have created a web forms user control with a DropDownList. I want to change SelectedIndex property of DropDownList1 to change the selected index.

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:

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:

<%@ 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:

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. 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):

<%@ 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?

4
  • you want to add items from code behind or you want to set selectedindex from code behind? Commented Dec 24, 2015 at 13:03
  • @DSA: Both. I want to add items from code behind like DropDownList1.Items.Add("item name") and then change SelectedIndex by WebUserControl1.SetSelectedIndex(number) in a page's code behind like Default.aspx.cs. Commented Dec 24, 2015 at 13:08
  • Here is your solution: dotnetcurry.com/ShowArticle.aspx?ID=155 Commented Dec 24, 2015 at 13:14
  • @DSA: SelectedIndex still cannot be changed by the approach (public DropDownList ControlB_DDL) described in the link. The article in the link is for a different problem. Commented Dec 24, 2015 at 13:39

1 Answer 1

1

The issue is that Page_Load for the page containing the user control (Default) executes before Page_Load for the user control (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.

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:

protected void Page_Init(object sender, EventArgs e) {
  if (IsPostBack) return;
  for (int i = 1; i <= 5; i++) {
    DropDownList1.Items.Add("Test: " + i.ToString());
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.