【问题标题】:asp.net gridview in update panel, make visible and update content更新面板中的asp.net gridview,使内容可见并更新
【发布时间】:2010-10-15 05:25:16
【问题描述】:

目前,我的页面上有一个动态创建的网格视图。当用户在文本框中输入内容并按下按钮时,整个页面都会刷新以填充 gridview 并使其可见。我不想再那样了。我将如何使用 UpdatePanel 使 gridview 可见并填充它?

<div class="span-93 prepend-2 top">
        <strong>Enter  Number</strong><br />
        <asp:TextBox ID="PartNumber" runat="server" Width="100"></asp:TextBox>
        <asp:Button ID="CreateButton" runat="server" Width="85" Text="Locate" OnClick="CreateButton_Click" />
    </div>
<asp:Label ID="Select" runat="server" Font-Bold="true" Text="Select choice" Visible="false"></asp:Label><br />
            <ajax:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:GridView ID="GridView" Visible="false" runat="server"  HeaderStyle-Width="200" HeaderStyle-BackColor="#2B6292" HeaderStyle-ForeColor="White" 
                    AllowSorting="true" AllowPaging="true" Width="600" AutoGenerateColumns="False" OnRowCreated="GridView_OnRowCreated" 
                    DataKeyNames="Id" onsorting="GridView_OnSort">
                        <Columns>
                            ...
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
                <Triggers>
                    <ajax:AsyncPostBackTrigger ControlID="CreateButton"/>
                </Triggers>
            </ajax:UpdatePanel>

页面上有另一个按钮叫做 CreateButton,很明显,它将填充 gridview 并使其可见,以便用户可以从中进行选择。这可能吗?提前致谢。

编辑:将代码绑定到gridview:

    protected void Create_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Number.Text))
            {
                BLL newbll = new BLL();
                Database.DataTable tempTable = newbll.GetItemByPartNumber(Number.Text);

                if (Table.Count != 0)
                {
                DataTable table = tempTable ;

                string[] VID = { "Id" };
                GridviewDiv.Visible = true;
                GridView.DataSource = table;
                GridView.DataKeyNames = VID;
                GridView.DataBind();
            }
        }
    }

【问题讨论】:

  • 我也不清楚你是否有一个或两个按钮作为 GridView 显示的触发器 - 你能澄清一下吗?

标签: asp.net gridview updatepanel


【解决方案1】:

由于您的更新面板的更新模式设置为条件,在您调用网格视图上的 DataBind 之后的代码中,您需要在更新面板的名称上调用 Update()(在本例中名为 UpdatePanel) .

【讨论】:

  • 我在页面上出现错误提示:Sys.InvalidOperationException:找不到 ID 为 ctl00_Main_GridviewUpPanel 的 UpdatPanel。如果它是动态更新的,那么它必须在另一个 UpdatePanel 中
  • 首先,“UpdatPanel”是更新面板的名称,还是拼写错误?在您的示例中,它被命名为“UpdatePanel”。您的更新面板是否嵌套在另一个面板中?您必须遍历外部更新面板中的子控件才能找到您需要更新的更新面板。
  • Lance - 它的名称是 UpdatePanel,只是拼写错误,不,它不在另一个更新面板中。
  • 你能在后面的代码中发布你的绑定代码吗?
  • 在调用 GridView.DataBind() 之后,您是否能够引用更新面板?它应该出现在 Intellisense 中,这意味着页面将知道它的存在。只需从中调用 Update() 方法。
【解决方案2】:

将 CreateButton 声明为触发器后,您不必显式调用 Update() 来进行刷新

您在哪里声明了 CreateButton ? - 它必须与 UpdatePanel 位于相同的“NamingContainer”中才能找到

您能否在更新面板中移动 CreateButton 的声明。如果是这样,您不需要显式声明触发器 - 看看这是否会有所不同

【讨论】:

  • Tom,我不能,因为有问题的按钮必须在初始文本框旁边。当它被点击时,下面的网格视图变得可见并填充了代码隐藏。
  • OK...但是(没有显式调用Update())单击按钮会导致:显示gridview,发生完全刷新,什么都没有,或者是否有一些错误消息显示(或编译错误)?
  • 当我单击按钮以显示网格视图时,没有任何反应,并且页面上出现错误,我在 RSolberg 发布的第一个 cmets 中已经说明了这一点
  • 您需要验证Button的Naming Container是否与UpdatePanel的相同- CreateButton在哪里声明?
  • 已编辑,我已在更新面板上方声明了创建按钮
【解决方案3】:

使用下面的

<asp:UpdatePanel ID="..." runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">       
    <ContentTemplate>
      <asp:GridView  ....>
      </asp:GridView .....>

    ///buttons ..............whatever

    </ContentTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多