Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my gridview i've 2 fields

i want gridview like

-----------------------
STATUS || ACTION
-----------------------
Active || Block

Blocked || UnBlock

that is

if my status field is "Active" then my action link button should be "Block" and its commandname ="Block"

if my status field is "Blocked" then my action link button should be "UnBlock" and its commandname="UnBlock"


Is it possible by using row command or row data bound property??
Posted
Updated 26-Aug-13 20:38pm
v2
Comments
vinay.tatipamula 27-Aug-13 2:41am    
you have to set the commandnaem in the databound event and in rowcommand check the commandname and do according to the command name..
VIP Venkatesan 27-Aug-13 2:58am    
my linkbutton text should be changed based on that status field.. i've no idea abt this
Brij 27-Aug-13 3:38am    
did you try using row data bound? Change the command name at row data bound based on your logic
VIP Venkatesan 27-Aug-13 3:45am    
i've given my code at solution 1. look at that and give me a perfect solution sir..

Here i've given my rowcommand and row databound code..

can u help me by using this?

C#
protected void grdbroker_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName == "Block")
        {
            int i = Convert.ToInt32(e.CommandArgument);
            int recid = Convert.ToInt32(grdbroker.DataKeys[i].Value);
            ViewState["recid"] = recid;
            broker.RecID = Convert.ToInt32(ViewState["recid"]);
            BAL.fnBlockBroker(broker);
            BindBrokerList(); 
            Label lbl=(Label)grdbroker.Rows[i].FindControl("lblrecordstatus");
            if(lbl.Text=="Blocked")          
             {
                Session["lnkblocktxt"]="UnBlock";
             }
        }
        else if (e.CommandName == "UnBlock")
        {
            int i = Convert.ToInt32(e.CommandArgument);
            int recid = Convert.ToInt32(grdbroker.DataKeys[i].Value);
            ViewState["recid"] = recid;
            broker.RecID = Convert.ToInt32(ViewState["recid"]);
            BAL.fnUnBlockBroker(broker);
            BindBrokerList();            
           Label lbl=(Label)grdbroker.Rows[i].FindControl("lblrecordstatus");
           if(lbl.Text=="Active")          
           {
               Session["lnkblocktxt"] = "Block";
           }
        }
    }

    protected void grdbroker_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lnkblock = (LinkButton)e.Row.FindControl("lnkblock");

            if (Session["lnkblocktxt"].ToString() == "UnBlock")
            {
                lnkblock.Text = "UnBlock";
                lnkblock.CommandName = "UnBlock";                
            }
            else if (Session["lnkblocktxt"].ToString() == "Block")
            {
                lnkblock.Text = "Block";
                lnkblock.CommandName = "Block";
            }
        }
        
    }
 
Share this answer
 
v2
protected void grdbroker_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName == "Block")
        {
            int i = Convert.ToInt32(e.CommandArgument);
            int recid = Convert.ToInt32(grdbroker.DataKeys[i].Value);
            ViewState["recid"] = recid;
            broker.RecID = Convert.ToInt32(ViewState["recid"]);
            BAL.fnBlockBroker(broker);
            BindBrokerList();
           
        }
        else if (e.CommandName == "UnBlock")
        {
            int i = Convert.ToInt32(e.CommandArgument);
            int recid = Convert.ToInt32(grdbroker.DataKeys[i].Value);
            ViewState["recid"] = recid;
            broker.RecID = Convert.ToInt32(ViewState["recid"]);
            BAL.fnUnBlockBroker(broker);
            BindBrokerList();    
        }
    }


public void BindBrokerList()
    {
        DataTable dt = new DataTable();
        dt = BAL.fnBindBrokerList();
        if (dt.Rows.Count > 0)
        {
            grdbroker.DataSource = dt;
            grdbroker.DataBind();
            int i;
            for (i = 0; i < grdbroker.Rows.Count; i++)
            {
                Label lbl = (Label)grdbroker.Rows[i].FindControl("lblrecordstatus");
                if (lbl.Text == "Active")
                {
                    LinkButton lnkblock = (LinkButton)grdbroker.Rows[i].FindControl("lnkblock");
                    lnkblock.Text = "Block";
                    lnkblock.CommandName = "Block";
                }
                if (lbl.Text == "Blocked")
                {
                    LinkButton lnkblock = (LinkButton)grdbroker.Rows[i].FindControl("lnkblock");
                    lnkblock.Text = "UnBlock";
                    lnkblock.CommandName = "UnBlock";
                }
            }
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            grdbroker.DataSource = dt;
            grdbroker.DataBind();
            int columncount = grdbroker.Rows[0].Cells.Count;
            grdbroker.Rows[0].Cells.Clear();
            grdbroker.Rows[0].Cells.Add(new TableCell());
            grdbroker.Rows[0].Cells[0].ColumnSpan = columncount;
            grdbroker.Rows[0].Cells[0].Text = "No Records Found";
        }
    }
 
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