有条件地显示隐藏asp.net Gridview列 [英] conditionally show hide asp.net Gridview column

查看:724
本文介绍了有条件地显示隐藏asp.net Gridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我如何导航到 myPage.aspx

  < a href ='〜/ myPage.aspx?show =<%#Eval(id)%>'id =showEachrunat =server>显示每个< / a> 

< a href =〜/ myPage.aspx?show = allid =showAllrunat =server>全部显示< / a>

我在 myPage.aspx

 < asp:GridView ID =GridView1runat =server> 
<列>
< asp:BoundField HeaderText =ColumnOneVisible =true/>
< asp:BoundField HeaderText =ColumnTwoVisible =true/>
< /列>
< / asp:GridView>

我想要做的是,如果Query String等于 all (~/myPage.aspx?show=all),我想将GridView1的 Column2 的visible设置为true,否则设置false为false。

怎样才能做到这一点?

解决方案

您可以使用gridview列索引来隐藏特定列



代码可以是

  if(Request.QueryString.Get( show)==all)
GridView1.Columns [1] .Visible = true;
else
GridView1.Columns [1] .Visible = false;

更多详情

GridView按代码隐藏列



编辑1



我认为是

 < asp:BoundField HeaderText = ColumnTwo
Visible ='<%if(Request.QueryString.Get(all)==all)trueelsefalse%>'/>

您必须检查syntex



编辑2



试试这个

  Visible ='<%Request.QueryString.Get(all)==all? true:false%>'



编辑3



不能直接完成。



<%=%>直接输出到响应流,而asp标记不是响应流。假设<%=%>操作符正在对asp标记执行任何预处理,这是一个错误。



更多解释

为什么会<%=%>表达式作为服务器控件上的属性值导致编译错误?


This is how I navigate to myPage.aspx ,

<a href='~/myPage.aspx?show=<%#Eval("id")%>' id="showEach" runat="server">Show Each</a>

<a href="~/myPage.aspx?show=all" id="showAll" runat="server">Show All</a>

And I have a gridview in myPage.aspx

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField  HeaderText="ColumnOne"  Visible="true"/>
<asp:BoundField  HeaderText="ColumnTwo"  Visible="true"/>
</Columns>
</asp:GridView>

What I want to do is , if Query String is equal to all(~/myPage.aspx?show=all) , I want to set GridView1's Column2's visible to true , else , set visible to false .
How can I do it ?

解决方案

you can use gridview column index to hide the particular column

Code could be

 if(Request.QueryString.Get("show")=="all")
    GridView1.Columns[1].Visible=true;
 else
    GridView1.Columns[1].Visible=false;

More detail

GridView Hide Column by code

Edit 1

I think yes

 <asp:BoundField HeaderText="ColumnTwo" 
      Visible='<% if (Request.QueryString.Get("all") == "all" ) "true" else "false" %>'/>

You will have to check for the syntex

Edit 2

Try this

 Visible='<% Request.QueryString.Get("all") == "all"? "true": "false"%>'

Edit 3

Can not be done directly.

<%= %> outputs directly to the response stream, and the asp markup is not part of the response stream. Its a mistake to assume the <%= %> operators are performing any kind of preprocessing on the asp markup.

More explanation

Why will <%= %> expressions as property values on a server-controls lead to a compile errors?

这篇关于有条件地显示隐藏asp.net Gridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆