现在又一个GridView  显示数据库中的一个表(UserInfor),然后我有一个模板列(删除) 我想实现这样的效果,在我打开UserList 页面的时候,同时找出姓名是admin(管理员)的记录,
然后把与此记录相对应的LinkButten(即删除)在GridView  中不显示出来,请问各位怎么写啊?

解决方案 »

  1.   

    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;
                        }
                    }
                }
            }
      

  2.   

    1,3楼正解
    2楼说AJAX,具体说说看,我想不出来怎么做。。
      

  3.   

    ajax不用考虑,不安全,网速慢显示不正常
      

  4.   

    这个在查询的时候就可以过滤
    userName!='admin' 就行了
    或者就用上面 一行一行判断
      

  5.   

    但是 【0】我发现不是遍历的Name 而是 userId  所以它报错啊 
      

  6.   

    [e.Row.RowIndex][???]你Name是第几列你就相应改一下就可以啦,列下标为0,从0开始计数
      

  7.   

    “System.ArgumentOutOfRangeException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理其他信息: 索引超出范围。必须为非负值并小于集合大小。
      

  8.   


    <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">
                                                &nbsp;</td>
                                            <td style="height: 25px;width:10px">
                                                &nbsp;</td>
                                            <td style="height: 20px">
                                                总共 
                                                <asp:Label ID="Label1" runat="server" 
                                                Text="<%#((GridView)Container.NamingContainer).PageCount %>"> </asp:Label>
                                                页 &nbsp;
                                            </td>
                                            <td style="height: 25px">
                                                第 
                                                <asp:Label ID="Label2" runat="server" 
                                                Text="<%#((GridView)Container.NamingContainer).PageIndex+1 %>"> </asp:Label>
                                                页 &nbsp;</td>
                                            <td style="height: 25px">
                                                <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="1" 
                                                CommandName="Page" 
                                                Enabled="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">首页</asp:LinkButton>
                                                &nbsp;</td>
                                            <td style="height: 25px">
                                                <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Prev" 
                                                CommandName="Page" 
                                                Enabled="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">上一页</asp:LinkButton>
                                                &nbsp;</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>
                                                &nbsp;</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>
                                                &nbsp;</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>
      

  9.   

    this.GridView1.DataKeys[e.Row.RowIndex][1].ToString() 换成e.Row.Cells[1].Text,是我搞错了
      

  10.   

    你这里要这样用((Lable)e.Row.Cells[?].FindControl("lblName")).Text=="admin"
      

  11.   


    大哥还是不行啊  我DEBUG了一下发现e.Row.Cells[1].Text 总是为“”空啊,上面原因呢?
      

  12.   

    由于没有同名的lblName,这里((Lable)e.Row.FindControl("lblName")).Text=="admin"也行
      

  13.   

    使用<asp:BoundField DataField="Fil_Re" HeaderText="备注" />时,用e.Row.Cells[1].Text 
    你这里使用了模板列,就要使用((Lable)e.Row.FindControl("lblName")).Text=="admin"了