6

我不知道如何以编程方式将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 添加到 an而不会在单击时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);
}
4

2 回答 2

2

那么根据:

而且我不介意在设计时创建更新面板。我只需要能够以编程方式添加东西(比如包含包含按钮的网格视图的表),然后能够进行部分回发

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

  • 从事件中删除绑定并在事件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();
        }
    }

输出

在此处输入图像描述

于 2012-07-05T18:26:24.470 回答
2

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

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

这可能不是一个完美的解决方案,我只是想我会提供它,因为我假设你到目前为止已经遇到了一个赏金。

于 2012-07-05T18:41:19.330 回答