【问题标题】:template field column of gridview is being created on each click每次点击都会创建gridview的模板字段列
【发布时间】:2014-01-30 12:36:18
【问题描述】:

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

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

以下是后面的代码:

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 都不适合我。

【问题讨论】:

  • 您需要将Page_Load 中的字段添加到if(!IsPostback) 块中。如果您不这样做,它会在每次回发时重新添加。
  • 即使这样它也不能正常工作。然后没有其他列出现,但所有按钮突然从模板字段中消失

标签: asp.net ajax gridview


【解决方案1】:
if(!IsPostBack)
{
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();
}

<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>
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="GridView1" />
        </Triggers> 
        </asp:UpdatePanel>

    </div>

您可以通过这些链接获得帮助 http://www.codeproject.com/Questions/513399/UpdateplusPanelplusdataplusisplusnotplusrefreshing

http://ajax.net-tutorials.com/controls/updatepanel-control/

【讨论】:

  • 即使这样它也不能正常工作。然后没有其他列出现,但所有按钮突然从模板字段中消失
猜你喜欢
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多