有条件地显示隐藏asp.net Gridview列
这是我导航到myPage.aspx
,
<a href='https://stackoverflow.com/questions/17481250/conditionally-show-hide-asp-net-gridview-column/~/myPage.aspx?show=' id="showEach" runat="server">Show Each Show All
我在myPage.aspx
有一个gridview
我想要做的是,如果Query String等于all
(〜/ myPage.aspx?show = all),我想将GridView1的Column2
设置为true,否则设置visible为false。
我该怎么做 ?
您可以使用gridview列索引来隐藏特定列
代码可能是
if(Request.QueryString.Get("show")=="all") GridView1.Columns[1].Visible=true; else GridView1.Columns[1].Visible=false;
更多详情
GridView按代码隐藏列
编辑1
我想是的
你必须检查syntex
编辑2
试试这个
Visible='<% Request.QueryString.Get("all") == "all"? "true": "false"%>'
编辑3
不能直接完成。
<%=%>直接输出到响应流,并且asp标记不是响应流的一部分。 假设<%=%>运算符在asp标记上执行任何类型的预处理是错误的。
更多解释
为什么<%=%>表达式作为服务器控件上的属性值会导致编译错误?
您可以使用gridview预渲染方法来设置此…
protected void GridView_PreRender(object sender, EventArgs e) { if(Reqest.QueryString["Id"]=="all"&& Reqest.QueryString["Id"]!=null) { GridViewId.Columns[1].Visible = true; } else GridViewId.Columns[1].Visible = false; }
亲爱的尝试使用Grid View的RowDataBound事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //here apply your condition if(Request.QueryString["name"] == "all") e.Row.Cells[].Visible = true; else e.Row.Cells[ ].Visible = false; } }
尝试类似的东西。
希望对你有效。