如何以编程方式将Gridview添加到UpdatePanel

时间:2012-07-03 14:49:18

标签: 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-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>

那么,如果单击UpdatePanel,如何以编程方式向PostBack()添加带有按钮的网格视图而不会导致完整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);
}

2 个答案:

答案 0 :(得分:2)

根据:

  

我不介意在设计时创建更新面板。我只需要能够以编程方式添加内容(例如包含包含按钮的gridviews的表),然后能够进行部分回发

基本上我使用你的代码进行了一些小改动:

  • Init事件中删除了绑定,并在Load事件中执行

  • UpdatePanel是在设计时使用嵌套面板创建的,您只需将动态控件添加到该面板

此代码将执行此操作(它适用于我的环境):

ASPX

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

输出

enter image description here

答案 1 :(得分:2)

难道你不能在设计时把GridView放在那里,只需设置Visible=false隐藏它吗?

如果您不知道需要重复多少个网格视图,那么可以将GridView包装在ListView中。这个概念在这里介绍:

这可能不是一个完美的解决方案,我只是认为我会提供给它,因为有一个赏金,我认为你到目前为止已经撞到了一堵砖墙。