GridView 的问题,困扰了几天,各位请帮帮忙啊,谢谢了?

raymond123456 2010-01-14 02:27:03
现在又一个GridView 显示数据库中的一个表(UserInfor),然后我有一个模板列(删除)

我想实现这样的效果,在我打开UserList 页面的时候,同时找出姓名是admin(管理员)的记录,
然后把与此记录相对应的LinkButten(即删除)在GridView 中不显示出来,请问各位怎么写啊?
...全文
175 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
HooverHuang 2010-01-14
  • 打赏
  • 举报
回复
可以在后台定义一个全局变量,然后在前台判断
raymond123456 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 rock870210 的回复:]
使用 <asp:BoundField DataField="Fil_Remark" HeaderText="备注" />时,用e.Row.Cells[1].Text
你这里使用了模板列,就要使用((Lable)e.Row.FindControl("lblName")).Text=="admin"了
[/Quote]

非常感谢您!3Q
Rock870210 2010-01-14
  • 打赏
  • 举报
回复
使用<asp:BoundField DataField="Fil_Remark" HeaderText="备注" />时,用e.Row.Cells[1].Text
你这里使用了模板列,就要使用((Lable)e.Row.FindControl("lblName")).Text=="admin"了
raymond123456 2010-01-14
  • 打赏
  • 举报
回复
我再试试
raymond123456 2010-01-14
  • 打赏
  • 举报
回复

Rock870210 2010-01-14
  • 打赏
  • 举报
回复
由于没有同名的lblName,这里((Lable)e.Row.FindControl("lblName")).Text=="admin"也行
raymond123456 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 rock870210 的回复:]
this.GridView1.DataKeys[e.Row.RowIndex][1].ToString() 换成e.Row.Cells[1].Text,是我搞错了
[/Quote]

大哥还是不行啊 我DEBUG了一下发现e.Row.Cells[1].Text 总是为“”空啊,上面原因呢?
Rock870210 2010-01-14
  • 打赏
  • 举报
回复
你这里要这样用((Lable)e.Row.Cells[?].FindControl("lblName")).Text=="admin"
Rock870210 2010-01-14
  • 打赏
  • 举报
回复
this.GridView1.DataKeys[e.Row.RowIndex][1].ToString() 换成e.Row.Cells[1].Text,是我搞错了
raymond123456 2010-01-14
  • 打赏
  • 举报
回复

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="100%" DataKeyNames="UserId" Height="110px"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="5"
AllowPaging="True" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="工号">
<ItemTemplate>
<asp:Label ID="lblUserId" runat="server" Text='<%# Bind("UserId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="权限">
<ItemTemplate>
<asp:Label ID="lblLimitLevel" runat="server" Text='<%# Bind("LimitLevel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:LinkButton ID="lkbtnEdit" runat="server" PostBackUrl='<%#"~/User_edit.aspx?UserId="+Eval("UserId").ToString() %>'>编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="lkbtnDelete" runat="server" onclientclick="if(!confirm( '确定删除? ')) return false; "
CommandArgument='<%# Eval("UserId").ToString() %>' CommandName="De">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<table style="font-size :12px; margin-left: 0px; height: 15px; margin-bottom: 0px;"
align="right" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="height: 25px">
 </td>
<td style="height: 25px;width:10px">
 </td>
<td style="height: 20px">
总共
<asp:Label ID="Label1" runat="server"
Text="<%#((GridView)Container.NamingContainer).PageCount %>"> </asp:Label>
页  
</td>
<td style="height: 25px">

<asp:Label ID="Label2" runat="server"
Text="<%#((GridView)Container.NamingContainer).PageIndex+1 %>"> </asp:Label>
页  </td>
<td style="height: 25px">
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="1"
CommandName="Page"
Enabled="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">首页</asp:LinkButton>
 </td>
<td style="height: 25px">
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Prev"
CommandName="Page"
Enabled="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">上一页</asp:LinkButton>
 </td>
<td style="height: 25px">
<asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Next"
CommandName="Page"

Enabled="<%#((GridView)Container.NamingContainer).PageIndex!=((GridView)Container.NamingContainer).PageCount-1 %>">下一页
</asp:LinkButton>
 </td>
<td style="height: 25px">
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument="Last"
CommandName="Page"

Enabled="<%#((GridView)Container.NamingContainer).PageIndex!=((GridView)Container.NamingContainer).PageCount-1 %>">尾页</asp:LinkButton>
 </td>
<td style="height: 25px">
<td style="height: 25px">
<asp:TextBox ID="txtNum" runat="server" Width="30px"
Text="<%#((GridView)Container.NamingContainer).PageIndex+1 %>"
ValidationGroup="1">
</asp:TextBox>
<asp:LinkButton ID="LinkButton5" runat="server" CommandArgument="-1"
CommandName="Page" ValidationGroup="1">GO</asp:LinkButton>
</td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="" ValidationExpression="^\d+$" ControlToValidate="txtNum"
ValidationGroup="1">
</asp:RegularExpressionValidator>
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>

Rock870210 2010-01-14
  • 打赏
  • 举报
回复
把你页面Gridview代码贴出来
raymond123456 2010-01-14
  • 打赏
  • 举报
回复
“System.ArgumentOutOfRangeException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理

其他信息: 索引超出范围。必须为非负值并小于集合大小。
raymond123456 2010-01-14
  • 打赏
  • 举报
回复
只有改成0才不报错 改大于0 就错
Rock870210 2010-01-14
  • 打赏
  • 举报
回复
[e.Row.RowIndex][???]你Name是第几列你就相应改一下就可以啦,列下标为0,从0开始计数
raymond123456 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 rock870210 的回复:]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    if (this.GridView1.DataKeys[e.Row.RowIndex][1].ToString() == "管理员")
                    {
                        ((LinkButton)e.Row.FindControl("lbtnDel")).Visible = false;
                    }
                    else
                    {
                        ((LinkButton)e.Row.FindControl("lbtnDel")).Visible = true;
                    }
                }
            }
        }
[/Quote]
但是 【0】我发现不是遍历的Name 而是 userId 所以它报错啊
vip__888 2010-01-14
  • 打赏
  • 举报
回复
这个在查询的时候就可以过滤
userName!='admin' 就行了
或者就用上面 一行一行判断
kingdragonx 2010-01-14
  • 打赏
  • 举报
回复
ajax不用考虑,不安全,网速慢显示不正常
guanpu28 2010-01-14
  • 打赏
  • 举报
回复
1,3楼正解
2楼说AJAX,具体说说看,我想不出来怎么做。。
Rock870210 2010-01-14
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
if (this.GridView1.DataKeys[e.Row.RowIndex][1].ToString() == "管理员")
{
((LinkButton)e.Row.FindControl("lbtnDel")).Visible = false;
}
else
{
((LinkButton)e.Row.FindControl("lbtnDel")).Visible = true;
}
}
}
}
tan124 2010-01-14
  • 打赏
  • 举报
回复
用ajax自动更新
加载更多回复(1)

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧