1
votes

I am trying to create a custom component. The component should by dynamically initialized in code behind. The component presents a custom Window containing other components, like datefields, dropdown fields etc. I derived my class from Ext.Net.Window and added simple DateField. The date should than be used by a button click on the server (Date should not be passed over DirectMethod parameter). When I add this component to mark-up it works perfectly. But when I add the window in code behind, the value of the datefield is not set after the server call. I am creating the window in the life cycle in OnInit event by "Controls.Add(mywindow)". It would be great if anybody could give me a hint. Here my window code (onExecuteButtonClick just calls the direct method and hides the window):

public sealed class WindowFilterComponent:Window
{
    private const string Script = "MyProject.JavaScript.src.WindowFilterComponent.js";

    public override string InstanceOf
    {
        get
        {
            return "MyProject.Filter.WindowFilterComponent";
        }
    }

    public override string XType
    {
        get
        {
            return "windowfiltercomponent";
        }
    }

    private Button _btnExecute;

    private Button _btnCancel;

    private DateField _dateField;


    protected override void OnInit(EventArgs e)
    {       
        AutoHeight = true;
        _btnExecute = new Button("Execute Export");
        _btnExecute.Listeners.Click.Handler = string.Format("#{{{0}}}.onExecuteButtonClick()", ID);
        _btnCancel = new Button("Cancel");
        _btnCancel.Listeners.Click.Handler = string.Format("#{{{0}}}.onCancelButtonClick()", ID);
        Buttons.Add(_btnExecute);
        Buttons.Add(_btnCancel);
        _dateField = new DateField();
        Items.Add(_dateField);
        base.OnInit(e);
    }       

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (ExtNet.IsAjaxRequest || Page.IsCallback) return;
            ResourceManager.GetInstance().AddDirectMethodControl(this);
    }

    [DirectMethod(ShowMask = true)]
    public void ExecuteClick()
    {
        var date = _dateField.SelectedValue;            
    }
}

Now the useage in my page in the OnInit event:

protected override void OnInit(EventArgs e)
{    
    var myWindow = new WindowFilterComponent()
            {
                Hidden = false,
                Width = 500             
            };
    myWindow.ID = myWindow.ID + "MyComponent";
    Controls.Add(myWindow);
    base.OnInit(e);
}
1
If you need some code please let me know :) - xandi1987
can u show your class which derived window - sakir
I have added the class to the question - xandi1987
Please also demonstrate how you add the control in code behind. - Daniil Veriga
Sure. I changed again the post in the beginning. I am guessing, that I add this control to late in the life-cycle. But in the OnPreInit event, Controls is not initialized. So maybe I need to register this control somewhere. I have also looked to the request and the viewstate of this control is not send in the request. - xandi1987

1 Answers

1
votes

I think the Window is rendered outside of the Form.

Please replace

Controls.Add(myWindow);

with

Form.Controls.Add(myWindow);

Also I would recommend to set up explicit IDs for the submittable fields (the DateField in your case) to ensure that the id key from POST data will match the control's ID on the server.