每次点击都会创建gridview的模板字段列 [英] template field column of gridview is being created on each click

查看:74
本文介绍了每次点击都会创建gridview的模板字段列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在ajax面板中放置网格视图的aspx代码。



this is aspx code in which I put a grid view in ajax panel.

<body>
        <form id="form1" runat="server">
        <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>

            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" DataKeyNames="head_code" >
            </asp:GridView>
            
            </ContentTemplate>
            </asp:UpdatePanel>

        </div>
        </form>
    </body>





以下代码背后:



Following is code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace gridview_button
{

    public partial class WebForm2 : System.Web.UI.Page, ITemplate
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            hitechLatestEntities database = new hitechLatestEntities();
            GridView1.DataSource = database.HEADs;

            TemplateField tfObject = new TemplateField();
            tfObject.HeaderText = "Sub-Head";

            tfObject.ItemTemplate = new WebForm2(ListItemType.Item);

            GridView1.Columns.Add(tfObject);
            GridView1.DataBind();

        }

       /////////////////////////////// For template field //////////////////////////////////

        public WebForm2()
        {
        }

        public WebForm2(ListItemType Item)
        {
            myListItemType = Item;
        }

        private ListItemType myListItemType;

        public void InstantiateIn(Control container)
        {

            if (myListItemType == ListItemType.Item)
            {
                Button btn = new Button();
                container.Controls.Add(btn);
            }
        }

      ////////////////////////////////////////////////////////////////////////////////////
    }

}



我已经动态创建了模板字段,并且上面的代码工作正常,但唯一的问题是每次当我点击模板字段中的按钮时,会出现一个新的空白模板字段列标题文本。即使是ajax也不适合我。



有人能告诉我如何动态地将btn添加到UpdatePanel1吗?


I have created template field dynamically and Above code is working fine but only problem is that every time when i click on button in template field, a new blank template field column appears with same header text. Even ajax is not working for me.
or
can anyone tell me how to add btn to UpdatePanel1 dynamically??

推荐答案

<asp:updatepanel id="UpdatePanel1" updatemode="Conditional" runat="server" xmlns:asp="#unknown">
  </asp:updatepanel>


这篇关于每次点击都会创建gridview的模板字段列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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