C# 在 UpdatePanel 中使用 GridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9257581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:34:38  来源:igfitidea点击:

Using GridView inside UpdatePanel

c#asp.netasp.net-ajaxupdatepanel

提问by vml19

I have an Updatepanel and Gridview inside it.

我里面有一个 Updatepanel 和 Gridview。

<asp:UpdatePanel ID="uplPanel" UpdateMode="Conditional" runat="server" OnLoad="uplPanel_Load">
<ContentTemplate>
 <asp:GridView ID="gvPrList" runat="server" AutoGenerateColumns="false" AllowPaging="false"
       AllowSorting="false" CssClass="list-table" HeaderStyle-CssClass="header">
       <Columns>
     <ItemTemplate>
               <asp:Button ID="btnEdit" runat="server" Text="Edit" CssClass="button save" OnCommand="onPrItemCmd"
                   CommandName="editRow" CommandArgument='<%#Bind("ID") %>' Style="width: 80px" />
               <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="button save" OnCommand="onPrItemCmd"
                   CommandName="deleteRow" CommandArgument='<%#Bind("ID") %>' Style="width: 80px" />
           </ItemTemplate>
       </asp:TemplateField>
   </Columns>

When I click on my buttons inside the Griview, it does not fire the events. Any idea?

当我单击 Griview 中的按钮时,它不会触发事件。任何的想法?

采纳答案by vml19

I did the following and it works

我做了以下工作并且它有效

I replace asp button with html button and call javascript method to fire Update Panal Load event.

我用 html 按钮替换 asp 按钮并调用 javascript 方法来触发更新面板加载事件。

<input id="btnDelete1" type="button" onclick="javascript:DeletePrItem('<%# Eval("ID") %>');" value="Delete" class="button save" style="width: 80px" />

My Js :

我的JS:

    function DeletePrItem(_Id) {
        __doPostBack('<%= uplPanel.ClientID %>', _Id);
    }

My Code behind :

我背后的代码:

    protected void uplPanel_Load(object sender, EventArgs e)
    {
        var arg = Request.Params.Get("__EVENTARGUMENT");

        if (arg != null)
        {
            if (arg != "")
            {
                string recordId = arg.ToString();
                //Do deletetion and rebind data grid

    }
     }
}

回答by Naveed Butt

You need to add OnCommandevent of GridView and then handle that inside that event like this:

您需要添加OnCommandGridView 事件,然后在该事件中像这样处理它:

OnRowCommand="gvPrList_OnRowCommand" 

or alternatively add a click event for the individual button and then handle in the code behind file:

或者为单个按钮添加一个单击事件,然后在文件背后的代码中处理:

<asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" CssClass="button save"
                   OnCommand="onPrItemCmd" CommandName="editRow" CommandArgument='<%#Bind("ID") %>' Style="width: 80px" />

回答by aleafonso

This would be the Event Handler for your command in the codebehind:

这将是代码隐藏中命令的事件处理程序:

protected void onPrItemCmd(object sender, CommandEventArgs e)
    {
        //do whatever you want
        //probably you will need the "ID" or "CommandArgument":
        string myArgumentID = e.CommandArgument.ToString();

        uplPanel.Update(); //since the UpdatePanel is set *UpdateMode="Conditional"*
    }

UPDATE:

更新:

Probably, you might be doing some validation when you click on buttons. If so, you need to add CausesValidation="false"in your buttons or links properties

当您单击按钮时,您可能会进行一些验证。如果是这样,您需要在按钮或链接属性中添加CausesValidation="false"

回答by Ruruboy

Please add this code into the UpdatePanel.

请将此代码添加到 UpdatePanel 中。

</ContentTemplate> 
 <Triggers>
   <asp:PostBackTrigger ControlID="gvPrList" EventName="Click" />
 </Triggers>
 </asp:UpdatePanel>

回答by John

I had the same issue where column buttons with a OnClick were causing a postback but the OnClick method was not being hit. When I commented out the update panel and it all worked.

我遇到了同样的问题,其中带有 OnClick 的列按钮导致回发,但未命中 OnClick 方法。当我注释掉更新面板时,一切正常。

I resolved this issue by adding a postback trigger for the grid within the update panel:

我通过在更新面板中为网格添加回发触发器解决了这个问题:

</ContentTemplate>
   <Triggers>
       <asp:PostBackTrigger ControlID="uxWebDataGrid" />
   </Triggers>
</asp:UpdatePanel>

Hope this helps someone else!

希望这对其他人有帮助!

回答by Filipe Leal

I added an OnRowCommand Event and add this trigger to the UpdatePanel:

我添加了一个 OnRowCommand 事件并将这个触发器添加到 UpdatePanel:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="gvPrList" EventName="RowCommand" />
</Triggers>

Note that it's an Async trigger.

请注意,这是一个异步触发器。

回答by user2784648

I had a similar issue.

我有一个类似的问题。

Depending on your situation, as in mine...All of the clickable controls inside of the update panel I will want to be triggers; So i was simply able to use the UpdatePanel property 'ChildrenAsTriggers="true"' so solve the issue.

根据您的情况,就像我的情况一样......更新面板内的所有可点击控件我都希望成为触发器;所以我只能使用 UpdatePanel 属性 'ChildrenAsTriggers="true"' 来解决这个问题。

    <asp:UpdatePanel runat="server" ID="UPCommunicationlogForm" ChildrenAsTriggers="true" >

This solved my issue, now my edit and delete buttons that are generated from the ItemTemplate inside my gridview call their respective methods on the server.

这解决了我的问题,现在我从 gridview 中的 ItemTemplate 生成的编辑和删除按钮在服务器上调用它们各自的方法。

回答by code-it

This helped me. In my case I was changing a value in a asp:DropDownList control and then going back to the server to update my asp:GridView which is in a different asp:UpdatePanel. I just forgot to add code to refresh the Update Panel with the list. Once I did that everything worked fine.

这对我有帮助。就我而言,我正在更改 asp:DropDownList 控件中的一个值,然后返回服务器更新我的 asp:GridView,它位于不同的 asp:UpdatePanel 中。我只是忘记添加代码以使用列表刷新更新面板。一旦我这样做了,一切都很好。

Server side

服务器端

System.Data.DataSet ds = MySQL.DAL.GetRecord(sql);
GV_View.DataSource = ds;
GV_View.DataBind();
UP_MainList.Update(); //refresh the update panel

Aspx code

Aspx代码

            <asp:UpdatePanel runat="server" ID="UP_ListChange" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:DropDownList runat="server" ID="ddFilter" DataTextField="Desc" AutoPostBack="true" DataValueField="Detail" OnSelectedIndexChanged="GV_CodeView_RefreshScreen" ></asp:DropDownList>
                </ContentTemplate>
             </asp:UpdatePanel>
            <div class="medium">Select Filter to view database codes</div>
        </div>
        <div class="div-row-header" runat="server" id="divList">
        <asp:UpdatePanel runat="server" ID="UP_MainList" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="GV_View" runat="server" AllowSorting="True" AutoGenerateColumns="false" DataKeyNames="id">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
                    <asp:BoundField DataField="Type_Cd" HeaderText="Code" />
                    <asp:BoundField DataField="Short_Desc" HeaderText=" Description" />
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:Button ID="Code_Edit" runat="server" Text="Edit" onClick="GV_Code_Edit_Click" class="button" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>             
        </ContentTemplate>

        </asp:UpdatePanel>
        </div>