如何火点击LinkBut​​ton的事件在GridView和显示在asp.net中弹出窗口 [英] How to fire click event of the LinkButton in gridview and Show PopUp window in asp.net

查看:144
本文介绍了如何火点击LinkBut​​ton的事件在GridView和显示在asp.net中弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立有一个与动态列基于数据源的使用asp.net模板字段通过code的后面。

I have tried to build one gridview with dynamic columns based the data source using the template fields in asp.net through code behind.

有关,为了实现我们开发一个类 DynamicTemplate 它实现了了Itemplate 接口。在模板字段我已经在每个单元插入的LinkBut​​ton ,当我点击该单元格链接按钮我需要显示一个弹出与选定单元格的值。

For that, to implement we have developed one class DynamicTemplate which implements the ITemplate interface. In that template fields i have inserted the LinkButton in each cell and when i click that cell link button i need to show the one Popup with selected cell value.

<一个href=\"https://skydrive.live.com/redir?resid=C55C4D7B56FE99BB!231&authkey=!AOi6B54Fkj-YYmE&ithint=file,.rar\"相对=nofollow>有关详细信息,请详细从这个链接下载

有关,我已创建了一个页面Default.asxp并写了下面。

For that I have created one Default.asxp page and wrote the following.

  public partial class Default : System.Web.UI.Page
    {
        DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                GenateGridView();

        }

        private void GenateGridView()
        {
            TemplateField tempField;
            DynamicTemplate dynTempItem;
            LinkButton lnkButton;
            Label label;

            GridView gvDynamicArticle = new GridView();

            gvDynamicArticle.Width = Unit.Pixel(500);
            gvDynamicArticle.BorderWidth = Unit.Pixel(0);
            gvDynamicArticle.Caption = "<div>Default Grid</div>";
            gvDynamicArticle.AutoGenerateColumns = false;

            DataTable data = getBindingData();

            for (int i = 0; i < data.Columns.Count; i++)
            {
                tempField = new TemplateField();
                dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);

                lnkButton = new LinkButton();

                lnkButton.ID = string.Format("lnkButton{0}", i);
                lnkButton.Visible = true;

                string ColumnValue = data.Columns[i].ColumnName;
                tempField.HeaderText = ColumnValue;

                if (ColumnValue == "EmpName")
                {
                    label = new Label();

                    label.ID = string.Format("Label{0}", i);
                    dynTempItem.AddControl(label, "Text", ColumnValue);
                    label.Width = 100;
                }
                else
                {
                    dynTempItem.AddControl(lnkButton, "Text", ColumnValue);
                    lnkButton.Click += lnkButton_Click;
                }
                tempField.ItemTemplate = dynTempItem;
                gvDynamicArticle.Columns.Add(tempField);
                //////grdUserPivotDateTwo.Columns.Add(tempField);
            }

            gvDynamicArticle.DataSource = data;
            gvDynamicArticle.DataBind();

            divContainer.Controls.Add(gvDynamicArticle);

        }

        void lnkButton_Click(object sender, EventArgs e)
        {
            // showing cell values in popUp here.. 
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked')");
        }


        private DataTable getBindingData()
        {
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("EmpName"));
            dt.Columns.Add(new DataColumn("Monday"));
            dt.Columns.Add(new DataColumn("TuesDay"));
            dt.Columns.Add(new DataColumn("WednesDay"));
            dt.Columns.Add(new DataColumn("ThursDay"));

            dt.Rows.Add("EmpOne", "p", "p", "p", "a");
            dt.Rows.Add("EmpTwo", "p", "a", "p", "p");
            dt.Rows.Add("EmpThree", "p", "p", "p", "a");
            dt.Rows.Add("EmpFour", "p", "a", "p", "p");
            dt.Rows.Add("EmpFive", "p", "p", "p", "a");
            dt.Rows.Add("EmpSix", "a", "p", "p", "p");

            return dt;

        }



    }

和相应的 DynamicTemplate

public class DynamicTemplate : System.Web.UI.ITemplate
{
    System.Web.UI.WebControls.ListItemType templateType;
    System.Collections.Hashtable htControls = new System.Collections.Hashtable();
    System.Collections.Hashtable htBindPropertiesNames = new System.Collections.Hashtable();
    System.Collections.Hashtable htBindExpression = new System.Collections.Hashtable();

    public DynamicTemplate(System.Web.UI.WebControls.ListItemType type)
    {
        templateType = type;
    }

    public void AddControl(WebControl wbControl, String BindPropertyName, String BindExpression)
    {
        htControls.Add(htControls.Count, wbControl);
        htBindPropertiesNames.Add(htBindPropertiesNames.Count, BindPropertyName);
        htBindExpression.Add(htBindExpression.Count, BindExpression);

    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        PlaceHolder ph = new PlaceHolder();

        for (int i = 0; i < htControls.Count; i++)
        {

            //clone control 
            Control cntrl = CloneControl((Control)htControls[i]);

            switch (templateType)
            {
                case ListItemType.Header:
                    break;
                case ListItemType.Item:
                    ph.Controls.Add(cntrl);
                    break;
                case ListItemType.AlternatingItem:
                    ph.Controls.Add(cntrl);
                    ph.DataBinding += new EventHandler(Item_DataBinding);
                    break;
                case ListItemType.Footer:
                    break;
            }
        }
        ph.DataBinding += new EventHandler(Item_DataBinding);

        container.Controls.Add(ph);

    }
    public void Item_DataBinding(object sender, System.EventArgs e)
    {
        PlaceHolder ph = (PlaceHolder)sender;
        GridViewRow ri = (GridViewRow)ph.NamingContainer;
        for (int i = 0; i < htControls.Count; i++)
        {
            if (htBindPropertiesNames[i].ToString().Length > 0)
            {
                Control tmpCtrl = (Control)htControls[i];
                String item1Value = (String)DataBinder.Eval(ri.DataItem, htBindExpression[i].ToString());
                Control ctrl = ph.FindControl(tmpCtrl.ID);

                Type t = ctrl.GetType();
                System.Reflection.PropertyInfo pi = t.GetProperty(htBindPropertiesNames[i].ToString());

                pi.SetValue(ctrl, item1Value.ToString(), null);
            }

        }



    }

    private Control CloneControl(System.Web.UI.Control src_ctl)
    {
        Type t = src_ctl.GetType();
        Object obj = Activator.CreateInstance(t);
        Control dst_ctl = (Control)obj;
        PropertyDescriptorCollection src_pdc = TypeDescriptor.GetProperties(src_ctl);
        PropertyDescriptorCollection dst_pdc = TypeDescriptor.GetProperties(dst_ctl);

        for (int i = 0; i < src_pdc.Count; i++)
        {

            if (src_pdc[i].Attributes.Contains(DesignerSerializationVisibilityAttribute.Content))
            {

                object collection_val = src_pdc[i].GetValue(src_ctl);
                if ((collection_val is IList) == true)
                {
                    foreach (object child in (IList)collection_val)
                    {
                        Control new_child = CloneControl(child as Control);
                        object dst_collection_val = dst_pdc[i].GetValue(dst_ctl);
                        ((IList)dst_collection_val).Add(new_child);
                    }
                }
            }
            else
            {
                dst_pdc[src_pdc[i].Name].SetValue(dst_ctl, src_pdc[i].GetValue(src_ctl));
            }
        }

        return dst_ctl;

    }
}

在这里, GridView控件显示的数据是好的。这里的有关问题的当我在LinkBut​​ton的点击页面重新加载,没有网格回传后显示

Here the Data showing in gridview is fine. Here the Issues are when i click on the linkButton the page reloads and no grid is displaying after the postback.

第二个问题是,对于 的LinkBut​​ton 点击事件不点火

second issue is, for LinkButton the Click Event is not firing.

请为我提供帮助的完整信息/样品,显示模式窗口,当我们点击GridView控件的LinkBut​​ton的。

Please provide me the help full information/Sample to show the modal window when we click on the linkButton of the gridview.

推荐答案

首先,调用 GenateGridView 方法时,让你不得不调用此方法每次你的GridView将被创建你回来后,那么你的Page_Load应

First, your GridView will be created when calling GenateGridView method, so you have to call this method everytime you do post back, then your Page_Load should be

protected void Page_Load(object sender, EventArgs e)
{
   GenateGridView();
}

第二,我想present你的其他方式的LinkBut​​ton添加到GridView的动态。
我修改您的 GenateGridView 只需添加标签只进 DynamicTemplate 后,也加入这一行的 gvDynamicArticle.RowDataBound + =新GridViewRowEventHandler(gvDynamicArticle_RowDataBound); 以处理增加的LinkBut​​ton。

Second, I would present you the other way to add LinkButton to GridView dynamically. I modified your GenateGridView to just add only label into DynamicTemplate, also add this line gvDynamicArticle.RowDataBound += new GridViewRowEventHandler(gvDynamicArticle_RowDataBound); to handle adding LinkButton.

private void GenateGridView()
{
    TemplateField tempField;
    DynamicTemplate dynTempItem;
    Label label;

    GridView gvDynamicArticle = new GridView();

    gvDynamicArticle.Width = Unit.Pixel(500);
    gvDynamicArticle.BorderWidth = Unit.Pixel(0);
    gvDynamicArticle.Caption = "<div>Default Grid</div>";
    gvDynamicArticle.AutoGenerateColumns = false;
    gvDynamicArticle.RowDataBound += new GridViewRowEventHandler(gvDynamicArticle_RowDataBound);
    DataTable data = getBindingData();

    for (int i = 0; i < data.Columns.Count; i++)
    {
        tempField = new TemplateField();
        dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);

        string ColumnValue = data.Columns[i].ColumnName;
        tempField.HeaderText = ColumnValue;

        label = new Label();

        label.ID = string.Format("Label{0}", i);
        dynTempItem.AddControl(label, "Text", ColumnValue);
        label.Width = 100;

        tempField.ItemTemplate = dynTempItem;
        gvDynamicArticle.Columns.Add(tempField);
    }

    gvDynamicArticle.DataSource = data;
    gvDynamicArticle.DataBind();

    divContainer.Controls.Add(gvDynamicArticle);
}

我实现像这样的的RowDataBound 在GridView添加LinkBut​​ton的和隐藏我们之前加入该标签的事件处理程序:

I implement like this in RowDataBound event handler of the GridView to add LinkButton and hide the Label which we added it before:

protected void gvDynamicArticle_RowDataBound(object sender, GridViewRowEventArgs e) 
{
    for (int j = 1; j < e.Row.Cells.Count; j++)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lnkButton = new LinkButton();
            lnkButton.ID = string.Format("lnkButton{0}{1}", e.Row.DataItemIndex, j);
            lnkButton.Click += new EventHandler(lnkButton_Click);

            Label tempLabel = e.Row.FindControl("Label" + j) as Label;
            lnkButton.Text = tempLabel.Text;
            lnkButton.CommandArgument = tempLabel.Text;

            tempLabel.Visible = false;

            e.Row.Cells[j].Controls.Add(lnkButton);
        }
    }
}

您还可以设置为 CommandArgument 的LinkBut​​ton的,那么你就可以显示它在警告任何值。

You can also set any value to CommandArgument of LinkButton, then you can show it in the alert.

最后,似乎要显示在警报一定的价值,你可能code这样的

Final, seems you want to show some value in the alert, you may code like this

public void lnkButton_Click(object sender, EventArgs e)
{
    // showing cell values in popUp here.. 
    LinkButton lnk = (LinkButton)sender;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked, value " + lnk.CommandArgument + "')", true);
}

这篇关于如何火点击LinkBut​​ton的事件在GridView和显示在asp.net中弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆