Binding to Sub-Object does not work

1 Answer 38 Views
Grid
Andreas Decke
Top achievements
Rank 1
Andreas Decke asked on 22 Jun 2021, 05:36 PM | edited on 22 Jun 2021, 05:44 PM

Hello,
to test the input I would like to set default values for the form. I do that in the OnItemCommand. The values for the properties "ShortName" and "LegalForm" are displayed, the other values are not.

 

Codebehind:

 protected void rgCompanyies_OnItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "ViewCustomers")
                {
                    Response.Redirect(EditUrl("Id", e.CommandArgument.ToString(), "ViewCustomer"));
                }

                if (e.CommandName == RadGrid.InitInsertCommandName)
                {
                    if (IsDebugMode)
                    {
                        e.Canceled = true;
                        Hashtable values = new Hashtable();

                        string[] legalForms = { "GmbH", "AG", "Gbr", "OHG", "KG" };

                        Random rnd = new Random();
                        values["ShortName"] = $"A-Test{rnd.Next(10)}";
                        values["LegalForm"] = legalForms[rnd.Next(legalForms.Length)];
                        values["CompanyAddress.Street"] = $"Teststr. {rnd.Next(10)}";
                        values["CompanyAddress.Postalcode"] = $"{rnd.Next(10)}2345";
                        values["CompanyAddress.City"] = "Bonn";
                        values["CompanyPhone.Number"] = $"123456{rnd.Next(10)}";
                        values["CompanyEmail.Address"] = $"a.decke{rnd.Next(10)}@abc.de";

                        e.Item.OwnerTableView.InsertItem(values);
                    }
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

 

Views.ascx:


 <EditFormSettings CaptionFormatString="Bearbeite &lt;strong&gt;{0}&lt;/strong&gt;" EditFormType="Template" InsertCaption="Firma bearbeiten..." CaptionDataField="ShortName">
                        <FormTemplate>
                            <div id="main" class="container">
                                <div class="row mt-4">
                                    <div class="col-12">

                                        <div class="card">
                                            <!--Firmendaten-->
                                            <div class="card-header">
                                                Firmendaten
                                            </div>
                                            <div class="card-body">
                                                <div class="row g-2">
                                                    <!--CompanyTitle & CompanyTyp-->
                                                    <div class="col-md-6">
                                                        <label for="inputTitle" class="form-label-required">Title</label>
                                                        <asp:RequiredFieldValidator runat="server" ID="rfvInputCompanyName" ControlToValidate="tbCompanyName" Display="Dynamic" SetFocusOnError="True" CssClass="form-label-error float-end" ErrorMessage="Ein Firmenname wird erwartet!" ValidationGroup="vgCompany"/>
                                                        <asp:CustomValidator ID="cuvCompanyName" ControlToValidate="tbCompanyName" runat="server" Display="Dynamic" ErrorMessage="Firmenname bereits vorhanden!" CssClass="form-label-error float-end" SetFocusOnError="True" ValidationGroup="vgCompany" OnServerValidate="cuvInputCompanyName_OnServerValidate"/>
                                                        <asp:TextBox autofocus ID="tbCompanyName" Text='<%# Bind("ShortName") %>' runat="server" CssClass="form-control" ValidationGroup="vgCompany" TabIndex="10" required/>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <label for="inputType" class="form-label">Firmentyp</label>
                                                        <asp:TextBox ID="tbCompanyLeagalForm" runat="server" Text='<%# Bind("LegalForm") %>' CssClass="form-control"/>
                                                    </div>
                                                    <!--Postalcode & Location-->
                                                    <div class="col-md-6">
                                                        <label for="inputPostalcode" class="form-label-required">PLZ</label>
                                                        <asp:RequiredFieldValidator runat="server" ID="rfvCompanyPostalcode" ControlToValidate="tbCompanyPostalcode" Display="Dynamic" SetFocusOnError="True" CssClass="form-label-error float-end" ErrorMessage="Eine PLZ wird erwartet!" ValidationGroup="vgCompany"/>
                                                        <asp:TextBox ID="tbCompanyPostalcode" Text='<%# Bind("CompanyAddress.Postalcode") %>' runat="server" CssClass="form-control" ValidationGroup="vgCompany" TabIndex="20" required/>
                                                    </div>

 

the values for ShortName and LegalForm are displayed correctly (show image). The CompanyAddress.Street, CompanyAddress.Postalcode, etc. values are not displayed. Why doesn't the binding work there?

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 25 Jun 2021, 12:38 PM

Hello Andreas,

Can you tell me what were you trying to achieve in the ItemCommand event? If you want to bind data to RadGrid it has to be done in the NeedDataSource event. Binding the Fields to columns is done through the DataField property in case of built-in Columns, and using the Bind() expression in case of Template columns.

Here is the documentation article with the Sub Object binding: Binding to SubObjects

I have tested the following and it seems to be working:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView AutoGenerateColumns="False">
        <Columns>
            <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn DataField="Inner1.TestProp" HeaderText="Inner1.TestProp">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Inner1.Inner2.TestProp"
                HeaderText="Inner1.Inner2.TestProp">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn
                DataField="Inner1.Inner2.Inner1.TestProp" HeaderText="Inner1.Inner2.Inner1.TestProp">
            </telerik:GridBoundColumn>

            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <%# Eval("Inner1.TestProp") %>
                </ItemTemplate>

                <EditItemTemplate>
                    <telerik:RadTextBox ID="RadTextBox1" runat="server" Text='<%# Bind("Inner1.TestProp") %>'></telerik:RadTextBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

 

C# - code

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    ArrayList list = new ArrayList();
    list.Add(new MyObj("1"));
    list.Add(new MyObj("2"));
    list.Add(new MyObj("3"));
    list.Add(new MyObj("4"));
    list.Add(new MyObj("1"));
    RadGrid1.DataSource = list;
}
public class MyObj
{
    public string _innerText = "";
    public MyObj()
    {
    }
    public MyObj(string text)
    {
        _innerText = text;
    }
    public MyObj Inner1
    {
        get
        {
            return new MyObj(this._innerText + " - Inner1");
        }
    }
    public MyObj Inner2
    {
        get
        {
            return new MyObj(this._innerText + " - Inner2");
        }
    }
    public string TestProp
    {
        get
        {
            return this._innerText;
        }
    }
}

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Andreas Decke
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or