如何动态地在gridview中添加和捕获linkbutton的事件

时间:2014-08-27 11:03:55

标签: c# asp.net gridview

<asp:GridView ID="grid_planningStaff" runat="server" 
                        AutoGenerateColumns="false" 
                        OnRowCommand="yourgridview_RowCommand"
                        CssClass="table_style01 table100" 
                        BorderWidth="3px" 
                        ForeColor="Black" 
                        OnRowDataBound="grid_planning_RowdataBound">

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.CommandName = "Edit";


                    //lkBtn.OnClientClick = "Edit";
                    //lkBtn.Click += ViewDetails;
                    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 yourgridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        this.FindControl("grdwv" + e.CommandArgument).Visible = false;
        //i am assuming other gridview is on your .aspx page
    }
}

1 个答案:

答案 0 :(得分:0)

假设您想要在单击时绑定到事件,只需将方法绑定到lkBtn.Click事件:

LinkButton lkBtn = new LinkButton();
lkBtn.ForeColor = Color.Black;
lkBtn.Font.Underline = false;
lkBtn.ID = "link_button" + i;
lkBtn.Text = clientids;
lkBtn.CommandName = "Edit";
lkBtn.Click +=button_Click;

处理事件的方法:

void button_Click(object sender, EventArgs e)
        {
            //add code here for the button 
        }