Невозможно получить идентификаторы (и, конечно, значение) элементов управления, добавленных динамически - PullRequest
1 голос
/ 07 февраля 2011

Я просто пытаюсь добавить некоторые динамические элементы управления в SimpleQueryControl (который, конечно, является разновидностью веб-элемента управления и наследует все методы соответственно)Я не знаю, как получить значения дочерних элементов управления, которые я добавил динамически.

class RoomPickerQueryControl : SimpleQueryControl
{
    protected override void OnLoad(EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            EnsureChildControls();
            mColumnList.Visible = false;


        }



    }

    protected override void OnInit(EventArgs e)
    {
        DateTimeControl controlStartDate = new DateTimeControl();
        controlStartDate.LocaleId = 1053;
        controlStartDate.CssClassTextBox = "ms-long";
        controlStartDate.TimeZoneID = 1053;
        controlStartDate.LocaleId = 1053;
        controlStartDate.MinDate = DateTime.Now;
        controlStartDate.ID = "startDateTime";
        controlStartDate.Visible = true;
        controlStartDate.Enabled = true;
        controlStartDate.EnableViewState = true;
        this.Controls.Add(controlStartDate);




        base.OnInit(e);
    }

    protected override void CreateChildControls()
    {

        base.CreateChildControls();


    }

    protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize)
    {

        //i'm unable to get the ids here
        DateTimeControl dt = (DateTimeControl) FindControlRecursive(this, "startDateTime");
 //i'm unable to get the ids here
        DateTimeControl dt3 = (DateTimeControl)FindControlRecursive(this.Page, "startDateTime");
 //i'm unable to get the ids here
        DateTimeControl controlStartDate = (DateTimeControl)this.FindControl("startDateTime");
 //i'm unable to get the ids here
        DateTimeControl controlEndDate = (DateTimeControl)this.FindControl("endDateTime");



        return rowCount;
    }


    public static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;
        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }
        return null;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);


    }
}

Ответы [ 2 ]

0 голосов
/ 07 февраля 2011

Во-первых, я бы порекомендовал поместить любые создаваемые вами элементы управления в CreateChildControls.

Во-вторых, есть несколько способов заставить это работать. Во-первых, используйте FindControl, чтобы получить ссылку на ваш элемент управления ( пример здесь ):

DateTimeControl dt = this.FindControl("startDateTime") as DateTimeControl;

Альтернатива - сделать ваши DateTimeControl закрытыми переменными-членами.

class RoomPickerQueryControl : SimpleQueryControl
{
   DateTimeControl controlStartDate;
   DateTimeControl controlEndDate;
   // more code...

Инициализируйте ваши личные переменные-члены в CreateChildControls:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    // using our private class variable
    controlStartDate = new DateTimeControl();
    controlStartDate.LocaleId = 1053;
    // more code...
}

Тогда вы можете просто сослаться на controlStartDate в вашей функции IssueQuery.

0 голосов
/ 07 февраля 2011

Хранить переменную ссылку на них:

private DateTimeControl _controlStartDate;

Oninit(..)
{
   _controlStartDate = new DateTimeControl();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...