Link to home
Avatar of Codingitup
Codingitup

asked on

How to access selected index of a DropDownList within a repeater asp/net c#

I all,

I've got the following code from a pervious example: -

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)           

        {
            int SelectedIndex = 0;
            var controls = Repeater1.Items.Cast<RepeaterItem>()
            .Where(t => t.ItemType == ListItemType.Item || t.ItemType == ListItemType.AlternatingItem)
            .Select(t => t.FindControl("cmbReturnStage"));

Open in new window


there is a control called cmbReturnStage that I need to set the selected index based on a variable ie: -

cmbReturnsStage.Selectedindex = varSelectedIndex;

How do I gain control over the DropDownList of the individual row in the repeater to set it's selectedIndex please?

Many Thanks
Lee
Avatar of psreloaded
psreloaded

I haven't tested your code but it should work. Alternatively, you can use something like

DropDownList cmbReturnStage = (DropDownList)e.Item.FindControl("cmbReturnStage");

Open in new window


to get hold of the control. Once you get it... you can then set the selected index.
Avatar of Codingitup

ASKER

Hi,

I've put that code in but I get the following error: -

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Best Regards
Lee
ASKER CERTIFIED SOLUTION
Avatar of psreloaded
psreloaded

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial