简体   繁体   English

如何添加if-else语句以更改GridView的BoundFields

[英]How to add if-else statement to change BoundFields of GridView

I am trying to do something like the code below: 我正在尝试做类似下面的代码:

<%= if(ddlChoice.SelectedItem.Value ==1) { %>
  <asp:BoundField DataField="FirstName" HeaderText="First Name">
      <HeaderStyle HorizontalAlign="Left" /></asp:BoundField>
  <asp:BoundField DataField="LastName"  HeaderText="Last Name">
      <HeaderStyle HorizontalAlign="Left" /></asp:BoundField>
<%= } else {  %>>
  <asp:BoundField DataField="Name" HeaderText="Name">
      <HeaderStyle HorizontalAlign="Left" /></asp:BoundField>
<%= } %>

I have a GridView , now I want to add if-else condition to change available BoundField s according to selected item in DropDownList ... Please Guide me !!! 我有一个GridView ,现在我想添加if-else条件,以根据DropDownList选定项更改可用的BoundField ……请指导我!

In short: You cannot place your if statement between BoundField s as you are trying to do. 简而言之:您不能像尝试那样在BoundField之间放置if语句。

As an alternative solution, you could change the Visible property of each BoundField either from code behind or by setting a boolean value to that attribute in your .aspx file. 作为一种替代解决方案,可以从代码后面或通过为.aspx文件中的该属性设置布尔值来更改每个BoundFieldVisible属性。

Another alternative solution is to have more than one GridView and change their visibility upon user selection. 另一种替代解决方案是拥有多个GridView并根据用户选择更改其可见性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM