隐藏 Page_Load 上的编辑按钮

时间:2021-05-03 07:51:34

标签: c# asp.net gridview

我试图在学生登录时隐藏“编辑”按钮,并在管理员登录时显示它。默认情况下,编辑按钮是可见的。我想隐藏 Page_Load 上的编辑按钮。我试过将它包装在一个 div 中,但由于某种原因它不起作用。有什么解决办法吗??

.ASPX 文件的 GRIDVIEW 代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1224px" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                            <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
                    </ItemTemplate>
                    <EditItemTemplate>  
                    <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                    <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                </EditItemTemplate>  
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("SrNo") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
                <asp:TemplateField HeaderText="Profile">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_Profile" runat="server" Text='<%#Eval("Profile") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_Profile" runat="server" Text='<%#Eval("Profile") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
                <asp:TemplateField HeaderText="CTC">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_CTC" runat="server" Text='<%#Eval("CTC") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_CTC" runat="server" Text='<%#Eval("CTC") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
                <asp:TemplateField HeaderText="InterOrFT">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_InternOrFT" runat="server" Text='<%#Eval("InternOrFT") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_InternOrFT" runat="server" Text='<%#Eval("InternOrFT") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
                <asp:TemplateField HeaderText="Location">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_Location" runat="server" Text='<%#Eval("Location") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_Location" runat="server" Text='<%#Eval("Location") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
            </Columns>
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

.ASPX.CS 文件的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["user"] == null)
        {
            Response.Redirect("~/login.aspx");
        }
        else
        {
            if (Session["user"].ToString() != "admin")
            {
                addForm.Visible = false;

            }
        }
        linkProfile.Text = Session["user"].ToString();
        if (!IsPostBack)
        {
            GVBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(cs);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tblUpcoming values(@name, @profile, @ctc, @internFT, @location)", con);
        cmd.Parameters.AddWithValue("@name", txtName.Text);
        cmd.Parameters.AddWithValue("@profile", txtProfile.Text);
        cmd.Parameters.AddWithValue("@ctc", txtCTC.Text);
        cmd.Parameters.AddWithValue("@internFT", txtInternFT.Text);
        cmd.Parameters.AddWithValue("@location", txtLocation.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GVBind();
        clear();
    }

    void GVBind()
    {
        SqlConnection con = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand("select compID as SrNo, name as Name, profile as Profile, internFT as InternOrFT, ctc as CTC, location as Location from tblUpcoming", con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

1 个答案:

答案 0 :(得分:0)

直接在 aspx 文件中读取会话值

<asp:Button ID="btn_Edit" runat="server" Text="Edit" 
     CommandName="Edit" Visible='<% Session["user"] == "admin" %>' />

另见ASP.NET "special" tags

相关问题