tags:

views:

43

answers:

3

Hi all I have a custom NewItem.aspx that I made by creating a new aspx from the exisiting one

I wanted to put a control in a row inside the XSL Template like this

<asp:DropDownList ID="ddlSectors" AutoPostBack="true" runat="server" __designer:bind="{ddwrt:DataBind('i',ddlSectors,'SelectedValue','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Sector')}">
                    </asp:DropDownList>
                        <!--<SharePoint:FormField runat="server" id="ff7{$Pos}" ControlMode="New" FieldName="Sector" __designer:bind="{ddwrt:DataBind('i',concat('ff7',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Sector')}"/>-->
                        <SharePoint:FieldDescription runat="server" id="ff7description{$Pos}" FieldName="Sector" ControlMode="New"/>

Now I want to reference ddlSectors from my code library but it always throws an object reference not set to an instatnce of an object.

I believe that this is becuase the control is inside the XSL template.

so is there any workaround for this ?

thanks

A: 

Question: Is that dropdownlist rendered on each row?

But you could try referencing parent control and using Control.FindControl method to get that control.

However if you don't know exactly which control could be parent, you can write custom FindControlRecursive method to search for that control no matter where.

using System.Web.UI;

namespace MyNamespace
{
    public static class ControlExtensions
    {
        public static T FindControlRecursive<T>(this Control parentControl, string id) where T : Control
        {
            T ctrl = default(T);

            if ((parentControl is T) && (parentControl.ID == id))
                return (T)parentControl;

            foreach (Control c in parentControl.Controls)
            {
                ctrl = c.FindControlRecursive<T>(id);

                if (ctrl != null)
                    break;
            }
            return ctrl;
        }
}

Then just call FindControlRecursive<DropDownList>("ddlSectors").

Janis Veinbergs
Hithis is not inside a gridview or somethingthis is inside a custom NewItem.aspx formso the dropdownlist is rendered just once in a single html row.I need to reference it from code behind.thanks
Mina Samy
See edited answer.
Janis Veinbergs
A: 

Hello There, i've got the same issue please if you find any work arounds , post :)

Baher
A: 

Thanks Janis

your Idea inspired me.

but here is the function that worked for me

Control FindControl2(ControlCollection col,string desiredID)
    {
        Control found=default(Control);


        if (found != null)
            return found;
            for (int i = 0; i < col.Count; i++)
            {

                Control temp = col[i];
                if (temp != null)
                {
                    if (temp.ID != null)
                    {
                        if (temp.ID == desiredID)
                        {
                            found = temp;
                            break;
                        }
                        else
                        {
                            if (found != null)
                                return found;
                            else
                            found=FindControl2(temp.Controls, desiredID);
                        }
                    }
                    else
                    {
                        if (found != null)
                            return found;
                        else
                        found = FindControl2(temp.Controls, desiredID);
                    }


                }
            }

        return found;
    }

I used this and it worked

thanks

Mina Samy