GridView

Hey guys, i have a grive view and i am populating from database using c# coding,

in my gridview i am using Item Template and have a coloum called option which i have two image button, ie edit and delete.

i want to deavated the delete button if the user type is "user".

how to i do it? couse on the page load i cant access the gridview items?

i will post my code here

 
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False"
                CssClass="mGrid" GridLines="None" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField SortExpression="name">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnName" runat="server" CommandArgument="name" CommandName="Sort">Driver Name</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderName" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblNname" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField SortExpression="surname">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnSurname" runat="server" CommandArgument="surname" CommandName="Sort">Driver Surname</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholdeSurname" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblSurname" runat="server" Text='<%# Bind("surname") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField SortExpression="indentity_number">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnIndentityNumber" runat="server" CommandArgument="indentity_number"
                                CommandName="Sort">Driver ID Number</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderIndentityNumber" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblIndentityNumber" runat="server" Text='<%# Bind("indentity_number") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            &nbsp;<asp:ImageButton ID="imgEdit" runat="server" CommandArgument='<%#Eval("indentity_number") %>'
                                ImageAlign="AbsMiddle" ImageUrl="~/login/images/icons/icon_edit.png" OnCommand="imgEdit_Command"
                                ToolTip="Edit" />
                            &nbsp;<asp:ImageButton ID="imgDelete" runat="server" CommandArgument='<%#Eval("indentity_number") %>'
                                ImageAlign="AbsMiddle" ImageUrl="~/login/images/icons/icon_delete.png" OnCommand="imgDelete_Command"
                                ToolTip="Delete" />
                        </ItemTemplate>
                        <ItemStyle Width="70%" />
                    </asp:TemplateField>
                </Columns>
                <AlternatingRowStyle />
            </asp:GridView>
            <table id="PagerFooter" runat="server" cellpadding="0" cellspacing="0" class="pgr_table">
                <tr>
                    <td>
                        &nbsp;&nbsp;<asp:TextBox ID="txtSearch" runat="server" CssClass="textbox"></asp:TextBox>
                        &nbsp;&nbsp;
                        <asp:Button ID="imgbtnSearch" runat="server" CssClass="btnblue" OnClick="imgbtnSearch_Click"
                            Text="Search" />
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td style="text-align: right">
                        <asp:LinkButton ID="lnkbtnPrevious" runat="server" CssClass="lnkbtn" OnClick="lnkbtnPrevious_Click">Previous</asp:LinkButton>
                        <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" OnItemDataBound="dlPaging_ItemDataBound"
                            RepeatDirection="Horizontal" RepeatLayout="Flow">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                    CommandName="lnkbtnPaging" CssClass="pager_link_button" Text='<%# Eval("PageText") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:DataList>
                        <asp:LinkButton ID="lnkbtnNext" runat="server" CssClass="lnkbtn" OnClick="lnkbtnNext_Click">Next</asp:LinkButton>
                    </td>
                </tr>
            </table>

Open in new window


 
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False"
                CssClass="mGrid" GridLines="None" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField SortExpression="name">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnName" runat="server" CommandArgument="name" CommandName="Sort">Driver Name</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderName" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblNname" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField SortExpression="surname">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnSurname" runat="server" CommandArgument="surname" CommandName="Sort">Driver Surname</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholdeSurname" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblSurname" runat="server" Text='<%# Bind("surname") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField SortExpression="indentity_number">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnIndentityNumber" runat="server" CommandArgument="indentity_number"
                                CommandName="Sort">Driver ID Number</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderIndentityNumber" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblIndentityNumber" runat="server" Text='<%# Bind("indentity_number") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            &nbsp;<asp:ImageButton ID="imgEdit" runat="server" CommandArgument='<%#Eval("indentity_number") %>'
                                ImageAlign="AbsMiddle" ImageUrl="~/login/images/icons/icon_edit.png" OnCommand="imgEdit_Command"
                                ToolTip="Edit" />
                            &nbsp;<asp:ImageButton ID="imgDelete" runat="server" CommandArgument='<%#Eval("indentity_number") %>'
                                ImageAlign="AbsMiddle" ImageUrl="~/login/images/icons/icon_delete.png" OnCommand="imgDelete_Command"
                                ToolTip="Delete" />
                        </ItemTemplate>
                        <ItemStyle Width="70%" />
                    </asp:TemplateField>
                </Columns>
                <AlternatingRowStyle />
            </asp:GridView>
            <table id="PagerFooter" runat="server" cellpadding="0" cellspacing="0" class="pgr_table">
                <tr>
                    <td>
                        &nbsp;&nbsp;<asp:TextBox ID="txtSearch" runat="server" CssClass="textbox"></asp:TextBox>
                        &nbsp;&nbsp;
                        <asp:Button ID="imgbtnSearch" runat="server" CssClass="btnblue" OnClick="imgbtnSearch_Click"
                            Text="Search" />
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td style="text-align: right">
                        <asp:LinkButton ID="lnkbtnPrevious" runat="server" CssClass="lnkbtn" OnClick="lnkbtnPrevious_Click">Previous</asp:LinkButton>
                        <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" OnItemDataBound="dlPaging_ItemDataBound"
                            RepeatDirection="Horizontal" RepeatLayout="Flow">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                    CommandName="lnkbtnPaging" CssClass="pager_link_button" Text='<%# Eval("PageText") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:DataList>
                        <asp:LinkButton ID="lnkbtnNext" runat="server" CssClass="lnkbtn" OnClick="lnkbtnNext_Click">Next</asp:LinkButton>
                    </td>
                </tr>
            </table>

Open in new window

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jawad.Web.WebControls;
using intercity_business_layer;
using System.Data.SqlClient;

public partial class driver_view : System.Web.UI.Page
{
    driver D = new driver();
    PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindGrid(CurrentSql);
        }

        if (Cache["UserType"].ToString() == "")
        {
        }
    }

    private void BindGrid(string sql_query)
    {
        DataTable dt = new DataTable();
        D.Sql = sql_query;
        dt = D.SQL_QUERY();
        DataView dv = dt.DefaultView;

        if (this.ViewState["SortExp"] != null)
        {
            dv.Sort = this.ViewState["SortExp"].ToString() + " " + this.ViewState["SortOrder"].ToString();
        }

        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.PageSize = 20;
        pds.CurrentPageIndex = CurrentPage;
        lnkbtnNext.Enabled = !pds.IsLastPage;
        lnkbtnPrevious.Enabled = !pds.IsFirstPage;

        GridView1.DataSource = pds;
        GridView1.DataBind();

        doPaging();
    }

    public int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }

    public string CurrentSql
    {
        get
        {
            if (this.ViewState["CurrentSql"] == null)
                return "SELECT * from driver";
            else
                return Convert.ToString(this.ViewState["CurrentSql"].ToString());
        }
        set
        {
            this.ViewState["CurrentSql"] = value;
        }
    }

    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        for (int i = 0; i < pds.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        dlPaging.DataSource = dt;
        dlPaging.DataBind();
    }

    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            BindGrid(CurrentSql);
        }
    }

    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
        }
    }
    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindGrid(CurrentSql);
    }

    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindGrid(CurrentSql);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Sort"))
        {
            if (this.ViewState["SortExp"] == null)
            {
                this.ViewState["SortExp"] = e.CommandArgument.ToString();
                this.ViewState["SortOrder"] = "ASC";
            }
            else
            {
                if (this.ViewState["SortExp"].ToString() == e.CommandArgument.ToString())
                {
                    if (this.ViewState["SortOrder"].ToString() == "ASC")
                        this.ViewState["SortOrder"] = "DESC";
                    else
                        this.ViewState["SortOrder"] = "ASC";
                }
                else
                {
                    this.ViewState["SortOrder"] = "ASC";
                    this.ViewState["SortExp"] = e.CommandArgument.ToString();
                }
            }

            BindGrid(CurrentSql);
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header && this.ViewState["SortExp"] != null)
        {
            Image ImgSort = new Image();
            if (this.ViewState["SortOrder"].ToString() == "ASC")
                ImgSort.ImageUrl = "/login/images/Master/down.gif";
            else
                ImgSort.ImageUrl = "/login/images/Master/up.gif";

            switch (this.ViewState["SortExp"].ToString())
            {
                case "name":
                    PlaceHolder placeholderName = (PlaceHolder)e.Row.FindControl("placeholderName");
                    placeholderName.Controls.Add(ImgSort);
                    break;

                case "surname":
                    PlaceHolder placeholdeSurname = (PlaceHolder)e.Row.FindControl("placeholdeSurname");
                    placeholdeSurname.Controls.Add(ImgSort);
                    break;

                case "indentity_number":
                    PlaceHolder placeholderIndentityNumber = (PlaceHolder)e.Row.FindControl("placeholderIndentityNumber");
                    placeholderIndentityNumber.Controls.Add(ImgSort);
                    break;

            }
        }
    }

    protected void imgEdit_Command(object sender, CommandEventArgs e)
    {
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });

        string indentity_number = commandArgs[0];

        Response.Redirect("/login/driver/edit.aspx?indentity_number=" + Server.UrlEncode(indentity_number));
    }

    protected void imgDelete_Command(object sender, CommandEventArgs e)
    {
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });

        string indentity_number = commandArgs[0];

        Response.Redirect("/login/driver/delete.aspx?indentity_number=" + Server.UrlEncode(indentity_number));
    }

    protected void imgbtnSearch_Click(object sender, EventArgs e)
    {
        BindGrid("SELECT * from  driver where (lower(name)) like lower('%" + txtSearch.Text + "%')");
    }




}

Open in new window

JCWEBHOSTAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
Well, you would disable the button as part of the grids RowDataBound event. There is no sign of "User Type" in the code you have posted, so where does that come from?
0
 
JCWEBHOSTAuthor Commented:
master page
0
 
JCWEBHOSTAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.