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

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

本文介绍了如何动态地在gridview中添加和捕获linkbutton的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>
















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;
            }
        }
    }

推荐答案


这篇关于如何动态地在gridview中添加和捕获linkbutton的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 00:57