从Code Behind填充ASPxTextBox和ASPxListBox

时间:2011-06-13 09:41:03

标签: devexpress aspxgridview

我正在为项目使用Devexpress ASPxGridView,但是在使用数据填充自定义EditForm时遇到了问题。

如下所示

<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>

通常我会写类似

的内容
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";
}

但那不行。有什么建议从哪里开始?该文档并未真正涵盖自定义表单:/

谢谢!

==更新==

尝试使用onhtmleditformcreated =“ASPxGridView1_HtmlEditFormCreated”和方法

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";
    }

}

我使用Value =“&lt;%#Bind('CompanyName')%&gt;”混淆了一些东西。如果我删除了绑定,则填充框将被填充,但之后我无法获取其中的数据。有什么方法吗?

2 个答案:

答案 0 :(得分:1)

您应该使用稍微不同的方法:

使用HtmlEditFormCreated事件设置编辑器属性。要获取编辑器实例,请使用gridView的FindEditFormTemplateControl方法。

答案 1 :(得分:1)

答案是......

填充名为CompanyList的ASPxComboBox

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!

如果你有像我这样的自定义表单

<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>

你无法从代码隐藏中填充它,#bind方法会挡住并覆盖任何其他传入的值。但是,如果您不打算从后面的代码填充它们,那么获取数据是一个巧妙的技巧......

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

}

但是如果你想从代码隐藏中填充一些表单值,请确保EditForm中没有#bind方法

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

按照此文章顶部的描述进行填充,并获取此类值

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...

}

玩得开心