DevExpress ASPxTreeList - 内联编辑

时间:2015-09-24 16:04:32

标签: c# webforms devexpress asp.net-4.5

我正在尝试使用ASP.NET 4.5 webforms应用程序上的DevExpress ASPxTreeList控件完成内联编辑 - 类似这样(取自DevExpress演示站点):

enter image description here

我想做的是两件事:

  1. 我希望能够将内联编辑限制为几列 - 例如 Budget列应该是可编辑的。我希望 Budget单元格变为文本框以允许数据输入 - 而所选行中的所有其他列应保持按原样 - 作为标签基本上(现在,当我启用内联编辑时,所有单元格变成可编辑的控件 - 它们会变成一个文本框,建议可以进行编辑)

  2. 我希望能够单击(或双击)到该单元格以启用内联编辑 - 现在,我需要使用单独的命令按钮来“打开”编辑。我怎么能这样做(如果有的话)?

1 个答案:

答案 0 :(得分:2)

  1. 如果您想要标签而不是可编辑的控件,那么您只需使用带有标签控件的EditCellTemplate
  2. 以下是示例:

    <dx:TreeListDataColumn FieldName="ContactName">
        <EditCellTemplate>
            <dx:ASPxLabel ID="ASPxLabel1" runat="server" Value='<%# Eval("ContactName") %>' />
        </EditCellTemplate>
    </dx:TreeListDataColumn>
    
    1. DevExpress未实现ASPxTreeList的此功能。他们在支持中心建议实施“ASPxTreeList - Implement BatchEdit mode (like in ASPxGridView)”。 Here您可以在ASPxGridView。{/ li>中了解它的工作原理。{/ 3}

      所以,这是基于this示例的解决方法 主要目标是将DataCellTemplate用于具有ASPxTextBox控件的列作为值编辑器。通过将特定样式应用于每个复合编辑器元素,可以完全自定义ASPxTextBox。这允许模拟期望的行为。您可以隐藏ASPxTextBox的边框和背景,仅在ASPxTextBox聚焦时显示边框 这是一个例子:

      <script>
          function GotFocus(s, e) {
              s.inputElement.style.cursor = "text";
          }
          function LostFocus(s, e) {
              s.inputElement.style.cursor = "default";
          }
      </script>
      
      <dx:ASPxTreeList ID="ASPxTreeList1" ClientInstanceName="tree" runat="server" Width="100%"
          DataSourceID="AccessDataSource1" AutoGenerateColumns="False" KeyFieldName="CategoryID"
          OnCustomCallback="ASPxTreeList1_CustomCallback">            
              <Columns>
                  <dx:TreeListTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0">
                  </dx:TreeListTextColumn>
                  <dx:TreeListTextColumn FieldName="CategoryName" VisibleIndex="1">
                      <DataCellTemplate>
                          <dx:ASPxTextBox
                              ID="txtBox"
                              Width="100%"
                              runat="server"
                              Value='<%# Eval("CategoryName") %>'
                              Cursor="default"
                              BackColor="Transparent"
                              Border-BorderColor="Transparent"
                              FocusedStyle-Border-BorderColor="ActiveBorder"
                              ClientSideEvents-GotFocus="GotFocus"
                              ClientSideEvents-LostFocus="LostFocus" />
                      </DataCellTemplate>
                  </dx:TreeListTextColumn>
                  <dx:TreeListTextColumn FieldName="Description" VisibleIndex="2">
                      <DataCellTemplate>                    
                          <dx:ASPxTextBox 
                              ID="txtBox" 
                              Width="100%" 
                              runat="server" 
                              Value='<%# Eval("Description")%>' 
                              Cursor="default"
                              BackColor="Transparent"
                              Border-BorderColor="Transparent"                        
                              FocusedStyle-Border-BorderColor="ActiveBorder"
                              ClientSideEvents-GotFocus="GotFocus"
                              ClientSideEvents-LostFocus="LostFocus"/>
                      </DataCellTemplate>                                   
                  </dx:TreeListTextColumn>
              </Columns>            
              <Border BorderWidth="0px" />
          </dx:ASPxTreeList>
          <dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Post Modifications"
              Width="217px">
              <ClientSideEvents Click="
      function(s, e) {
          tree.PerformCallback('post');
      }" />
          </dx:ASPxButton>
      
      <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
          SelectCommand="SELECT * FROM [Categories]" 
          UpdateCommand="UPDATE [Categories] SET [CategoryName] = ?, [Description] = ? WHERE [CategoryID] = ?">
          <UpdateParameters>
              <asp:Parameter Name="CategoryName" Type="String" />
              <asp:Parameter Name="Description" Type="String" />
              <asp:Parameter Name="CategoryID" Type="Int32" />
          </UpdateParameters>
      </asp:AccessDataSource>
      
      private List<Record> list = new List<Record>();
      
      protected void Page_Load(object sender, EventArgs e)
      {
          var column1 = ASPxTreeList1.DataColumns["CategoryName"];
          var column2 = ASPxTreeList1.DataColumns["Description"];
      
          var nodes = ASPxTreeList1.GetVisibleNodes();
      
          foreach (var node in nodes)
          {
              var txtBox1 = (ASPxTextBox)ASPxTreeList1.FindDataCellTemplateControl(node.Key, column1, "txtBox");
              var txtBox2 = (ASPxTextBox)ASPxTreeList1.FindDataCellTemplateControl(node.Key, column2, "txtBox");
      
              if (txtBox1 == null || txtBox2 == null)
                  continue;
      
              int id = Convert.ToInt32(node.Key);
              list.Add(new Record(id, txtBox1.Text, txtBox2.Text));
          }
      }
      
      protected void ASPxTreeList1_CustomCallback(object sender, TreeListCustomCallbackEventArgs e)
      {
          if (e.Argument == "post")
          {
              for (int i = 0; i < list.Count; i++)
              {
                  AccessDataSource1.UpdateParameters["CategoryName"].DefaultValue = list[i].CategoryName;
                  AccessDataSource1.UpdateParameters["Description"].DefaultValue = list[i].Description;
                  AccessDataSource1.UpdateParameters["CategoryID"].DefaultValue = list[i].Id.ToString();
                  //                AccessDataSource1.Update();  << Uncomment this line to update data!
              }
      
              ASPxTreeList1.DataBind();
          }
      }