Eric Herlitz
Eric Herlitz

Reputation: 26267

Populate ASPxTextBox and ASPxListBox from Code Behind

im using the Devexpress ASPxGridView for a project but are having problems to populate a custom EditForm with data.

It looks as follows

<Templates>
    <EditForm>
        Company Name:
        <dx:ASPxTextBox ID="CompanyName" runat="server" Value="<% #Bind('CompanyName') %>" />
        Parent:
        <dx:ASPxListBox ID="ParentGuid" runat="server" Value="<% #Bind('ParentGuid') %>" />
    </EditForm>
</Templates>

Normally I'd write something like

protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
{
    var CompanyList = db.companies.OrderBy(x => x.CompanyName).ToList();
    ASPxListBox ParentGuid = (ASPxListBox)ASPxGridView1.FindControl("ParentGuid");
    ParentGuid.DataSource = CompanyList;
    ParentGuid.DataBind();

    ASPxTextBox CompanyName = (ASPxTextBox)ASPxGridView1.FindControl("CompanyName");
    CompanyName.Text = "Some company name";
}

But that wont work. Any advice where to start? The documentation doesn't really cover custom forms that well :/

Thanks!

== UPDATE ==

Tried using the onhtmleditformcreated="ASPxGridView1_HtmlEditFormCreated" with the method

protected void ASPxGridView1_HtmlEditFormCreated(object sender, ASPxGridViewEditFormEventArgs e)
{
    Control CompanyName = ASPxGridView1.FindEditFormTemplateControl("CompanyName");
    if (CompanyName != null)
    {
        ASPxTextBox CompanyNameEdit = CompanyName as ASPxTextBox;
        CompanyName.Text = "Some Co";
    }

}

The fact that I use Value="<% #Bind('CompanyName') %>" messes stuff up a bit. If i remove the Bind the boxes gets populated but then I have no way of fetching the data in them. Any way around this?

Upvotes: 0

Views: 7405

Answers (2)

Eric Herlitz
Eric Herlitz

Reputation: 26267

The answer is...

To populate a ASPxComboBox called CompanyList

protected void ASPxGridView1_HtmlEditFormCreated(object sender, ASPxGridViewEditFormEventArgs e)
{
    Control ParentGuidControl = ASPxGridView1.FindEditFormTemplateControl("ParentGuid");
    if (ParentGuidControl != null)
    {
        ASPxComboBox ParentGuid = (ASPxComboBox)ParentGuidControl;

        var CompanyList = db.companies.OrderBy(x => x.CompanyName);
        ParentGuid.TextField = "CompanyName";
        ParentGuid.ValueField = "CompanyGuid";
        ParentGuid.DataSource = CompanyList;
        ParentGuid.DataBind();
    }
}

BUT!

If you have a custom form like i had

<Templates>
    <EditForm>
        Company Name:
        <dx:ASPxTextBox ID="CompanyName" runat="server" Value="<% #Bind('CompanyName') %>" />
        Parent:
        <dx:ASPxComboBox ID="ParentGuid" runat="server" Value="<% #Bind('ParentGuid') %>" />
    </EditForm>
</Templates>

you wont be able to populate it from code-behind, the #bind method is in the way and overwrite any other incoming value. However if you are not planning to populate them from code behind here is a neat trick to fetch the data...

protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
    IDictionaryEnumerator enumerator = e.NewValues.GetEnumerator();

    string CompanyName = string.Empty;
    Guid ParentGuid = Guid.Empty;

    enumerator.Reset();
    while (enumerator.MoveNext())
        if (enumerator.Key.ToString() == "CompanyName")
            CompanyName = enumerator.Value.ToString();
        else if (enumerator.Key.ToString() == "ParentGuid")
            ParentGuid = new Guid(enumerator.Value.ToString());

    // Do insert trick here

}

But if you want to fill some of the form values from code-behind ensure there are no #bind methods in the EditForm

<Templates>
    <EditForm>
        Company Name:
        <dx:ASPxTextBox ID="CompanyName" runat="server" />
        Parent:
        <dx:ASPxComboBox ID="ParentGuid" runat="server" />
    </EditForm>
</Templates>

Populate as described in the top of this post and fetch the values like this

protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
    string CompanyName = string.Empty;
    Guid ParentGuid = Guid.Empty;

    // This method is a bit more secure
    Control CompanyNameControl = ASPxGridView1.FindEditFormTemplateControl("CompanyName");
    if (CompanyNameControl != null)
    {
        ASPxTextBox CompanyNameTb = (ASPxTextBox)CompanyNameControl;
        CompanyName = CompanyNameTb.Text.ToString();
    }

    // A bit less secure, but lesser code
    ASPxComboBox ParentGuidControl = (ASPxComboBox)ASPxGridView1.FindEditFormTemplateControl("ParentGuid");
    ParentGuid = new Guid(ParentGuidControl.SelectedItem.Value.ToString());

    // Do insert...

}

Have fun

Upvotes: 1

DevExpress Team
DevExpress Team

Reputation: 11376

You should use a slightly different approach:

Use the HtmlEditFormCreated event to set editors properties. To obtain editor instances, use the gridView's FindEditFormTemplateControl method.

Upvotes: 1

Related Questions