0

我正在使用 Ajax 请求使用一个服务填充组合框存储值,以使用以下代码自动显示匹配的文本(过滤)

  <ext:ComboBox ID="cbManager" runat="server" DisplayField="FirstName" 
                        LabelAlign="Right"  ValueField="EmployeeID"  AllowBlank="false" 
                                                HideTrigger="true"   MinChars="1" FieldLabel="* Manager" >
                <ext:Store ID="storeManager" runat="server" AutoLoad="false">
                  <Proxy>
                   <ext:AjaxProxy Url="~/Modules/eHRMS/FilterEmployeeNames.ashx?empType=1">
                    <ActionMethods Read="GET" />
                       <Reader>
                     <ext:JsonReader Root="employees" TotalProperty="total" />
                       </Reader>
                    </ext:AjaxProxy>
                   </Proxy>
                  <Model>
                  <ext:Model ID="Model4" runat="server">
                   <Fields>
                    <ext:ModelField Name="EmployeeID" />
                    <ext:ModelField Name="FirstName" />
                   </Fields>
                  </ext:Model>
                  </Model>
                </ext:Store>
              </Store>
      </ext:ComboBox>

当我从组合框中选择任何名称时,它给出了正确的值但是当我尝试使用代码填充组合框值时,它只显示 EmployeeID(Value) 而不显示 EmplyeeName

我在 CodeBehind 中使用了以下代码

员工 emp = GetProject(EmployeeID); cbManager.SetValue(project.ManagerID);

I have also tried the following one also
cbManager.Select(project.ManagerID.ToString());

但它只显示 int 值而不是字符串(即 EmployeeName)

这种行为的原因可能是什么,请建议我正确的方法。

谢谢

4

1 回答 1

1

您应该添加与该值对应的记录,然后设置该值:

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            Store store = this.ComboBox1.GetStore();
            store.Data = new object[] 
            { 
                new 
                { 
                    value = "2", 
                    text = "Item 2" 
                } 
            };
        }
    }

    protected void Store_OnReadData(object sender, StoreReadDataEventArgs e)
    {
        Store store = sender as Store;
        store.DataSource = new object[] 
        { 
            new { value = "1", text = "Item 1" },
            new { value = "2", text = "Item 2" },
            new { value = "3", text = "Item 3" }
        };
        store.DataBind();
    }

    protected void ButtonClick(object sender, DirectEventArgs e)
    {
        ComboBox1.GetStore().Add(new object[] 
        { 
            new 
            { 
                value = "4", 
                text = "Item 4" 
            } 
        });
        ComboBox1.SetValue("4");
    }
</script>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Ext.NET v2 Example</title>
</head>
<body>
    <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:ComboBox
            ID="ComboBox1"
            runat="server"
            DisplayField="text"
            ValueField="value">
            <Store>
                <ext:Store runat="server" OnReadData="Store_OnReadData">
                    <Model>
                        <ext:Model runat="server">
                            <Fields>
                                <ext:ModelField Name="value" />
                                <ext:ModelField Name="text" />
                            </Fields>
                        </ext:Model>
                    </Model>
                    <Proxy>
                        <ext:PageProxy>
                            <Reader>
                                <ext:JsonReader />
                            </Reader>
                        </ext:PageProxy>
                    </Proxy>
                </ext:Store>
            </Store>
            <SelectedItems>
                <ext:ListItem Index="0" />
            </SelectedItems>
        </ext:ComboBox>

        <ext:Button runat="server" Text="Set some value">
            <DirectEvents>
                <Click OnEvent="ButtonClick"></Click>
            </DirectEvents>
        </ext:Button>
    </form>
</body>
</html>
于 2013-07-04T00:39:21.040 回答