Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
XML
<asp:GridView ID="grid_planningStaff" runat="server" AutoGenerateColumns="false"
                CssClass="table_style01 table100" BorderWidth="3px" ForeColor="Black" OnRowCommand="grid_planning_OnRowCommand" OnRowDataBound="grid_planning_RowdataBound">
                <Columns>
                </Columns>
            </asp:GridView>





C#
protected void grid_planning_RowdataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType != DataControlRowType.Header)
            {

                DataSet ds = new DataSet();
                planningStaffCommon.StaffID = sid;
                ds = objDALPlanningModule.GetStaffMeetingData(planningStaffCommon);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int J = 0; J < ds.Tables[0].Rows.Count; J++)
                    {
                        dateofcell = Convert.ToString(ds.Tables[0].Rows[J]["FromDate"]);
                        from = Convert.ToInt16(ds.Tables[0].Rows[J]["datea"]);
                        string clientids = Convert.ToString(ds.Tables[0].Rows[J]["MeetingType"]);
                        colorvalue = Convert.ToInt32(ds.Tables[0].Rows[J]["ColorValues"]);
                        //set Color
                        if (colorvalue == 1)
                        {
                            //DataSet dscolor = new DataSet();
                            //objDALPlanningModule._dtLOVDescription=clientids;
                            //dscolor=objDALPlanningModule.GetParaMeterDetailsDAL();
                            //if (dscolor.Tables[0] != null)
                            //{
                            //    setcolor = Convert.ToString(dscolor.Tables[0].Rows[0]["Value"]);
                            //}
                            //else
                            //{ 
                            //break
                            //}

                            if (clientids == "Test Value1")
                            {
                                setcolor = "GREEN";
                            }
                            else
                                if (clientids == "Testy Value2")
                                {
                                    setcolor = "ORANGE";
                                }
                                else
                                {
                                    setcolor = "RED";
                                }
                        }
                        else
                        {
                            setcolor = "Aqua";
                        }


                        //color and fill clients 
                        for (int i = from; i <= from; i++)
                        {
                            LinkButton lkBtn = new LinkButton();
                            lkBtn.ForeColor = Color.Black;
                            lkBtn.Font.Underline = false;
                            lkBtn.ID = "link_button" + i;
                            lkBtn.Text = clientids;
                            lkBtn.CommandArgument = "link_button"+i;
                            lkBtn.CommandName = "Edit";
                            lkBtn. önClientClick = "Edit";
                            HiddenField hdndate = new HiddenField();
                            hdndate.ID = "hdn" + i;
                            hdndate.Value = dateofcell;
                            e.Row.Cells[i].Controls.Add(hdndate);
                            e.Row.Cells[i].BackColor = Color.FromName(setcolor);
                            e.Row.Cells[i].Controls.Add(lkBtn);
                        }
                    }

                }
                //hide other extra created rows
                for (int i = 1; i < grid_planningStaff.Rows.Count; i++)
                {
                    grid_planningStaff.Rows[0].Visible = true;
                    grid_planningStaff.Rows[i].Visible = false;

                }
            }
        }
        catch (Exception)
        {

            throw;
        }


    }
    

    protected void grid_planning_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            for (int i = 1; i < 32; i++)
            {
                HiddenField hdn = (HiddenField)grid_planningStaff.FindControl("hdn" + i);
                //LinkButton lnkdate = (LinkButton)grid_planningStaff.FindControl("hdn"+i);
                txt_fromDate.Text = hdn.Value;
            }
        }
    }
Posted

1 solution

The solution is to add the following under lkBtn. onClientClick = "Edit"; line:

lnk.Click += new EventHandler(lnk_Click);


Also, somewhere within the class you need the function named lnk_Click with handler signature like this

protected void lnk_Click(object sender, System.EventArgs e) {
// handle your click event

LinkButton lnk = (LinkButton)sender;

// here goes your code
DoSomething(lnk.CommandArgument);
}

I'm VB.Net guy so maybe there will be minor syntax errors, but this is the general idea.
 
Share this answer
 
Comments
ganew 27-Aug-14 6:38am    
Thank you sinisa hajnal for ans my question but its not working
link button cant catch any event,
i also use the debugging but click on link button page will be redirect on it
Sinisa Hajnal 27-Aug-14 7:25am    
What does OnClientClick function Edit do? Maybe you have some redirect there? If you want to use server side event, client side should only do validation or confirmation, not editing or postbacks.

Also, put a breakpoint on the line and make sure the code that adds the event executes.
Sinisa Hajnal 28-Aug-14 9:47am    
Did you resolve this?
ganew 31-Aug-14 2:28am    
yes
using hyperlink ,send the id to that page
if i want to use link button or other i want use ITemplate interface class in my code
because event cant catch dynamically

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