2017-03-15 135 views
2

我有一個ASPxGridView控件需要一個在條件基礎上可見的按鈕。我已經搜索並閱讀了幾篇文章,但沒有找到解決我的具體情況。該按鈕稱爲btnAllocatePlan,只有在名爲PayPlanFlg的列中的值爲true時才應顯示該按鈕。下面是網格的標記和我在代碼隱藏中嘗試的兩件事情,這是我寧願這樣做的地方。其實這些事件都沒有被解僱。任何幫助是極大的讚賞!DevExpress ASPxGridView在VB.NET中的自定義按鈕的條件顯示

<dx:ASPxGridView ID="grdPayments" runat="server" CssClass="tblLined" SettingsPager-PageSize="50" 
     AutoGenerateColumns="False" KeyFieldName="PaymentKey" Width="100%"> 
     <SettingsBehavior AllowFocusedRow="True" ProcessFocusedRowChangedOnServer="True" /> 
     <Columns> 
      <dx:GridViewDataTextColumn Caption="CaseKey" FieldName="CaseKey" Visible="False" VisibleIndex="0"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Name" FieldName="Name" VisibleIndex="1" Width="10%"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Association" FieldName="AssociationName" VisibleIndex="2" Width="15%"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Address" FieldName="Address1" VisibleIndex="3" Width="15%"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Amount Paid" FieldName="Amount" VisibleIndex="4" Width="5%"> 
       <PropertiesTextEdit DisplayFormatString="c"></PropertiesTextEdit> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Payment Date" FieldName="TransactionTime" VisibleIndex="5" Width="12%"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Payment Type" FieldName="PaymentType" VisibleIndex="6" Width="3%"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Amount Owed" FieldName="Balance" VisibleIndex="7" Width="5%"> 
       <PropertiesTextEdit DisplayFormatString="c"> 
       </PropertiesTextEdit> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn Caption="Amount Available" FieldName="AmountAvailable" VisibleIndex="8" Width="6%"> 
       <PropertiesTextEdit DisplayFormatString="c"> 
       </PropertiesTextEdit> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataHyperLinkColumn Name="AllocationLink" VisibleIndex="9" Caption=" " UnboundType="String" Width="5%"> 
       <DataItemTemplate> 
        <!--<a href="javascript:OpenPaymentAllocation()"><%#"Allocate"%></a>--> 
        <dx:ASPxButton ID="btnAllocate" runat="server" Text="Allocate" OnClick="btnAllocate_Click" /> 
       </DataItemTemplate> 
      </dx:GridViewDataHyperLinkColumn> 
      <dx:GridViewDataHyperLinkColumn Name="PlanLink" VisibleIndex="10" Caption=" " UnboundType="String" Width="5%"> 
       <DataItemTemplate> 
        <!--<a href="javascript:OpenPaymentPlanAllocation()"><%#"Allocate"%></a>--> 
        <dx:ASPxButton ID="btnAllocatePlan" runat="server" Text="Pay Plan" OnClick="btnAllocatePlan_Click" /> 
       </DataItemTemplate> 
      </dx:GridViewDataHyperLinkColumn> 
      <dx:GridViewDataTextColumn Caption="PayPlanFlg" FieldName="PayPlanFlg" Visible="false" VisibleIndex="11"> 
      </dx:GridViewDataTextColumn> 
     </Columns>    
    </dx:ASPxGridView> 

代碼隱藏的嘗試:

Protected Sub grdPayments_CustomButtonInitialize(sender As Object, e As ASPxGridViewCustomButtonEventArgs) 

     If e.Button.ID = "btnAllocatePlan" Then 

      If grdPayments.GetRowValues(e.VisibleIndex, 11, "PayPlanFlg") = 1 Then 

       e.Button.Visibility = GridViewCustomButtonVisibility.Invisible 

      End If 

     End If 

    End Sub 

Protected Sub ASPxGridView1_HtmlDataCellPrepared(sender As Object, e As ASPxGridViewTableDataCellEventArgs) 

     If e.DataColumn.FieldName = "ID" Then 
      Dim textBox As ASPxButton = TryCast(ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "btn"), ASPxButton) 
      If Convert.ToString(e.GetValue("ItemName")).Equals("B") Then 
       textBox.Text = Convert.ToString(e.CellValue) 
       textBox.Visible = True 
      Else 
       textBox.Visible = False 
      End If 
     End If 

    End Sub 

回答

1

最好的辦法是處理按鈕的Init/Load事件。在The general technique of using the Init/Load event handler知識庫文章中描述了此推薦技術 。

檢查下面的一排隱藏的特定按鈕的示例代碼..

ASPX

<dx:GridViewDataHyperLinkColumn Name="AllocationLink" VisibleIndex="9" Caption=" " UnboundType="String" Width="5%"> 
    <DataItemTemplate> 
     <!--<a href="javascript:OpenPaymentAllocation()"><%#"Allocate"%></a>--> 
     <dx:ASPxButton ID="btnAllocate" runat="server" Text="Allocate" OnInit="btnAllocate_Init" /> 
    </DataItemTemplate> 
</dx:GridViewDataHyperLinkColumn> 

的.cs

protected void btnAllocate_Init(object sender, EventArgs e) 
{ 
    DevExpress.Web.ASPxButton btn = (DevExpress.Web.ASPxButton)sender; 
    GridViewDataItemTemplateContainer container = btn.NamingContainer as GridViewDataItemTemplateContainer; 

    // You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference 
    if (container != null && !container.Grid.IsNewRowEditing) 
     btn.Visible = !(bool)DataBinder.Eval(container.DataItem, "PayPlanFlg"); 
} 

參考:
ASPxGridView - Hide the control in the DataItemTemplate conditionally

+0

感謝您的回覆,Niranjan。我嘗試了你的代碼,但是我得到一個DevExpress.Web.ASPxButton沒有定義的錯誤。任何想法是什麼造成的? – Mike

+0

我對那個問題進行了排序。當ASPxButton仍處於ASPxEditors名稱空間中時,我使用的是較舊版本的DevExpress。 – Mike

+0

但是你的功能仍然是一樣的。只需改變命名空間並引用代碼根據你的DevExpress版本進行修改即可。 –