Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a gridview and I want to add a button 'VIEW' which redirects to the (Customers.aspx?CustomerID=) page which links to a report. Every customer has a ID and each ID is associated with a report.

I don't want the ID to show up in my gridview and I want it to be hidden. Also, I want this button to show up only if the USER is an "Admin" or a "Salesperson" category. Is there a way i can bind my CustomerID to a UserCategory to generate reports for each ID separately.

I tried looking at some other posts as well but my button doesn't redirect for some reason. Can someone please help me figure out what i am doing wrong here or if i am missing something. Also, is there a simpler way of doing this Any help will be greatly appreciated. Thank you.

What I have tried:

<asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="btnView" runat="server" CausesValidation="false" CommandName="Select" Text="VIEW"  />
             </ItemTemplate>
             <ControlStyle CssClass="button" />
          </asp:TemplateField>



protected void btnView(object sender, EventArgs e)
   {
       txtHFUserCategory.Value = Session["UserCategoryPC"].ToString();


       if (txtHFUserCategory.Value == "A" || txtHFUserCategory.Value == "B" )

       {

           Button btn = (Button)sender;
           GridViewRow row = (GridViewRow)btn.NamingContainer;

           Label lblCustomerID = (Label)row.FindControl("lblCustomerID");
           String strCustomerID = lblCustomerID.Text;
           Session["UserID"] = txtHFUserID.Value;
           Response.Redirect("Customer.aspx?CustomerID=" );

       }


   }
Posted
Updated 12-Jul-17 8:09am
Comments
j snooze 11-Jul-17 17:36pm    
shouldn't your redirect be response.redirect("Customer.aspx?customerid=" + strCustomerID); or am I missing something?

Hi,
How you are using a gridview, to hide the ID, use the DataKeyNames property of the component. You do not see the id.
ASP.NET
<asp:GridView ID="grd_result" DataKeyNames="id" runat="server" OnRowCommand="grd_result_RowCommand">

To activate the button, do not use a button component in the TemplateField, use a ButtonField component, it is easier. This button is executed by the OnRowCommand event.
You can have many ButtonFields in your grid, to differentiate them use CommandName property.
ASP.NET
<asp:ButtonField ButtonType="Link" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" CommandName="view" HeaderText="" Text="View" ControlStyle-BorderColor="Black" />

Here is an example to help you.
HTML CODE
ASP.NET
<div>
        <asp:GridView ID="grd_result" DataKeyNames="id" runat="server" EmptyDataText=":: No Data ::" AutoGenerateColumns="False" BorderStyle="Solid" OnRowCommand="grd_result_RowCommand" FooterStyle-CssClass="GridFooter" AllowPaging="false"  ShowFooter="True">
            <Columns>
                <asp:ButtonField ButtonType="Link" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" CommandName="view" HeaderText="" Text="View" ControlStyle-BorderColor="Black" />
                <asp:BoundField HeaderText="Name"  datafield="name" HeaderStyle-HorizontalAlign="Left" />
            </Columns>
        </asp:GridView>    
    </div>
    <br />
    <asp:Label ID="lblmsg" runat="server" Text="-"></asp:Label>

CODE-BEHIND
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Fill the gridview with data.
                DataTable dt = new DataTable();
                dt.Columns.Add("id");dt.Columns.Add("name");

                DataRow dr = dt.NewRow();
                dr["id"] = "1";dr["name"] = "John";dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["id"] = "2";dr["name"] = "Mary";dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["id"] = "3"; dr["name"] = "Sarah";dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["id"] = "4";dr["name"] = "Peter";dt.Rows.Add(dr);

                this.grd_result.DataSource = dt;
                this.grd_result.DataBind();
            }
        }

        private bool user_permission(string user)
        {
            //Here, the code that verify if the user is admin or Salesperson
            //This information is normally record in the database.
            return true;
        }

        protected void grd_result_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                //this.Page.User.Identity.Name. This code is used if you authenticate the system with an user and password.
                if (this.user_permission(this.Page.User.Identity.Name))
                {
                    //find the name button.
                    string current_command = e.CommandName;
                    //find the index of line that i clicked.
                    int current_row_index = Int32.Parse(e.CommandArgument.ToString());
                    //find the id the line.
                    string id = this.grd_result.DataKeys[current_row_index].Value.ToString();

                    switch (current_command)
                    {
                        case "view":
                            {
                                this.lblmsg.Text = "My id is " + id + " and my name is " + this.grd_result.Rows[current_row_index].Cells[1].Text;
                                break;
                            }
                    }
                }
                else{this.lblmsg.Text = "You don't have permission to execute this task.";}
            }
            catch (Exception ex)
            {
                this.lblmsg.Text = "Error: " + ex.Message; 
            }
        }
 
Share this answer
 
v2
Hide the column
int columnToHide =4;
if(role=="Admin"){
GridView1.Columns[columnToHide].Visible = false;
}


You shall use Hidden field instead of Label to hide and store the data
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900