我有一个ASPxGridView控件,需要一个在条件基础上可见的按钮。我搜索并阅读了几篇文章,但没有找到适合我的具体情况。该按钮称为btnAllocatePlan,只有在名为PayPlanFlg的列中的值为true时才会显示该按钮。下面是网格的标记和我在代码隐藏中尝试的两件事,这是我宁愿这样做的地方。实际上这些事件都没有被解雇。非常感谢任何帮助!
<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>
Codebehind尝试:
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
答案 0 :(得分:1)
最好的方法是处理Button的Init / Load事件。这个 The general technique of using the Init/Load event handler知识库文章中介绍了推荐的技术。
检查以下示例代码,以隐藏行中的特定按钮..
<强> ASPX 强>
ns
<强>的.cs 强>
<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>
参考:
ASPxGridView - Hide the control in the DataItemTemplate conditionally