C# 如何以编程方式将Gridview添加到UpdatePanel
我不知道如何通过编程将带有按钮的C# 如何以编程方式将Gridview添加到UpdatePanel,c#,asp.net,gridview,updatepanel,C#,Asp.net,Gridview,Updatepanel,我不知道如何通过编程将带有按钮的GridView添加到UpdatePanel 我可以用按钮和标签等简单的控件来实现,但是当我尝试添加带有按钮的GridView时,会出现一个完整的Postback() <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitio
GridView
添加到UpdatePanel
我可以用按钮和标签等简单的控件来实现,但是当我尝试添加带有按钮的GridView
时,会出现一个完整的Postback()
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected override void OnInit(EventArgs e)
{
UpdatePanel up1 = new UpdatePanel();
up1.ID = "UpdatePanel1";
Button button1 = new Button();
button1.ID = "Button1";
button1.Text = "Submit";
button1.Click += new EventHandler(Button_Click);
Label label1 = new Label();
label1.ID = "Label1";
label1.Text = "A full page postback occurred.";
GridView gv1 = new GridView();
//Where the xml gets bonded to the data grind
XmlDataSource xds = new XmlDataSource();
xds.Data = xml;
xds.DataBind();
xds.EnableCaching = false;
gv1.DataSource = xds;
createButton(gv1, up1);
gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand);
gv1.DataBind();
up1.ChildrenAsTriggers = true;
up1.ContentTemplateContainer.Controls.Add(button1);
up1.ContentTemplateContainer.Controls.Add(label1);
up1.ContentTemplateContainer.Controls.Add(gv1);
Page.Form.Controls.Add(up1);
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "buttonClicked")
{
int index = Convert.ToInt32(e.CommandArgument);
}
}
void createButton(GridView g)
{
ButtonField tea = new ButtonField();
tea.ButtonType = ButtonType.Image;
tea.ImageUrl = "~/checkdailyinventory.bmp";
tea.CommandName = "buttonClicked";
tea.HeaderText = "space";
g.Columns.Add(tea);
}
protected void Button_Click(object sender, EventArgs e)
{
((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " +
DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>UpdatePanel Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
</form>
</body>
</html>
根据:
我不介意在设计时创建更新面板。我只需要能够以编程方式添加内容(比如包含GridView的表,其中包含按钮),然后能够进行部分回发
基本上,我对您的代码进行了一些小的更改:
- 从
事件中删除了绑定,我在Init
事件中执行它Load
是在设计时使用嵌套面板创建的,您只需将动态控件添加到该面板中即可UpdatePanel
代码隐藏
受保护的无效页\u Init(对象发送方,事件参数e)
{
Button button1=新按钮();
button1.ID=“button1”;
按钮1.Text=“提交”;
button1.Click+=新建事件处理程序(按钮单击);
标签标签1=新标签();
label1.ID=“label1”;
label1.Text=“发生了整页回发。”;
var s1=Builder.CreateListOfSize(15.Build();
GridView gv1=新的GridView();
gv1.DataSource=s1;
创建按钮(gv1);
gv1.RowCommand+=新的GridViewCommandEventHandler(CustomerGridView_RowCommand);
this.myPanel.Controls.Add(按钮1);
this.myPanel.Controls.Add(label1);
this.myPanel.Controls.Add(gv1);
}
void createButton(GridView g)
{
按钮字段tea=新按钮字段();
tea.ButtonType=ButtonType.Image;
tea.ImageUrl=“~/checkdailyinventory.bmp”;
tea.CommandName=“buttonClicked”;
tea.HeaderText=“空间”;
g、 添加(tea);
}
受保护的无效按钮\u单击(对象发送者,事件参数e)
{
((Label)Page.FindControl(“Label1”)).Text=“面板刷新时间”+
DateTime.Now.ToString();
}
受保护的无效页面加载(对象发送方、事件参数e)
{
这个.DataBind();
}
public void CustomersGridView_row命令(对象发送方,GridViewCommandEventArgs e)
{
如果(e.CommandName==“按钮点击”)
{
//int index=Convert.ToInt32(e.CommandArgument);
this.lblMessage.Text=DateTime.Now.ToString();
}
}
输出
难道您不能在设计时将GridView放在那里,然后通过设置
Visible=false来隐藏它吗
如果您不知道需要重复多少个GridView,那么可以将GridView包装到ListView中。这里介绍了这一概念:
这可能不是一个完美的解决方案,我只是认为我会提供它,因为我认为你到目前为止已经遇到了悬赏。你是否尝试过在页面中包含一个占位符,并将更新面板添加到此占位符,而不是添加到页面。控件?只是尝试了一下,但没有成功<代码>占位符1.Controls.Clear();占位符1.控件.添加(up1)//Page.Form.Controls.Add(up1)代码>尝试将UpdateMode设置为UpdatePanelUpdateMode.Conditional。我正在读MSDN关于这个问题的一些帖子。一个人在javascript方面有问题,这不是你的情况。另一个可以使用此UpdateMode设置。将UpdateMode设置为conditional也不起作用。我想我必须将每个按钮添加到触发器中,但我不能100%确定如何正确地执行;trigger.ControlID=“按钮ID”;up1.Triggers.Add(触发器);如果我想使用
而不是面板,会有什么不同吗?我希望将GridView并排保存。我认为这样可以很好地工作,如果您遇到一些问题,您可以动态创建表,并将它们添加到面板中
,只需几个问题就可以了。为什么要将绑定添加到页面加载?为什么它需要点击两下才能开始异步?(我在做了一些测试后才注意到这一点)我必须在页面_Init
中做吗?或者,这是我唯一可以通过编程方式添加GridView的地方吗?它只需单击一次即可工作(至少在我发布代码的环境中)。Microsoft建议在Init
事件(使用母版页时)或PreInit
(不使用母版页时)中创建动态控件。原因是,在这些事件之后创建的控件将在页面生命周期的晚些时候引发其事件。实际上,您可以在Init
事件中调用DataBind
方法,但是在Load
事件中(在Load
中,ViewState已经绑定)可以使用:如果(!this.IsPostBack)this.DataBind()代码>由于grid ViewState将在每次回发时保留其内容,因此必须以编程方式添加GridView,因为直到运行时,我才知道需要添加多少个GridView。添加一个额外的想法可以覆盖这种情况
void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
AsyncPostBackTrigger t = new AsyncPostBackTrigger();
t.ControlID = e.Row.Cells[0].ClientID;
t.EventName = "blah";
up1.Triggers.Add(t);
}
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel>
<br />
<asp:Label runat="server" ID="lblMessage"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Init(object sender, EventArgs e)
{
Button button1 = new Button();
button1.ID = "Button1";
button1.Text = "Submit";
button1.Click += new EventHandler(Button_Click);
Label label1 = new Label();
label1.ID = "Label1";
label1.Text = "A full page postback occurred.";
var s1 = Builder<Product>.CreateListOfSize(15).Build();
GridView gv1 = new GridView();
gv1.DataSource = s1;
createButton(gv1);
gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand);
this.myPanel.Controls.Add(button1);
this.myPanel.Controls.Add(label1);
this.myPanel.Controls.Add(gv1);
}
void createButton(GridView g)
{
ButtonField tea = new ButtonField();
tea.ButtonType = ButtonType.Image;
tea.ImageUrl = "~/checkdailyinventory.bmp";
tea.CommandName = "buttonClicked";
tea.HeaderText = "space";
g.Columns.Add(tea);
}
protected void Button_Click(object sender, EventArgs e)
{
((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " +
DateTime.Now.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "buttonClicked")
{
//int index = Convert.ToInt32(e.CommandArgument);
this.lblMessage.Text = DateTime.Now.ToString();
}
}