簡體   English   中英

在更新面板中時,GridView中的按鈕單擊不觸發

[英]Button click in gridview not firing when inside update panel

<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="Timer1_Tick" />
<asp:UpdatePanel runat="server" id="pnlUpdate">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer1" eventname="Tick"/>
    </Triggers>
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="table table-hover table-condensed" 
            GridLines="None" AllowPaging="true" PageSize="20"
            OnPageIndexChanging="GridView1_PageIndexChanging">
        <Columns>
            <asp:BoundField ItemStyle-Width="50%" DataField="Story" HeaderText="Story"/>
            <asp:BoundField ItemStyle-Width="15%" ItemStyle-Wrap="false" DataField="AssignedTo" HeaderText="Assigned To"/>
            <asp:BoundField ItemStyle-Width="15%" ItemStyle-Wrap="false" DataField="Status" HeaderText="Status"/>
            <asp:BoundField ItemStyle-Width="15%" ItemStyle-Wrap="false" DataField="Started" HeaderText="Started"/>
            <asp:BoundField ItemStyle-Width="5%" ItemStyle-Wrap="false" DataField="Updated" HeaderText="Updated"/>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Buttonid" runat="server" CommandName="ViewItem" Text="View"  BorderStyle="None" BackColor="White" 
                        OnClick="Button_click_event"></asp:Button>   
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <RowStyle CssClass="cursor-pointer" />
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

當我有更新面板時,不會觸發Button_click_event ,但當它不在更新面板中時,它將起作用。

您的更新面板看起來像

<asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
 <ContentTemplate>
     ....
 </ContentTemplate>
<Triggers>
 <asp:AsyncPostBackTrigger ControlID="Buttonid" EventName="Click"/>
</Triggers>

嘗試將UpdateMode="Conditional"ChildrenAsTriggers="False"到UpdatePanel。

 <Triggers>
            <asp:AsyncPostBackTrigger ControlID="GridView1" />
        </Triggers>

同時刪除觸發器的EventName屬性,然后重試。

刪除按鈕的屬性,單擊CommandName =“ ViewItem”,然后嘗試運行,如果不起作用,還添加按鈕UseSubmitBehaviour = true的屬性。

您可以通過多種方式解決此問題。 一種方法是在代碼中的rowdatabound上添加可處理onclick事件的控件(例如div),然后在其可單擊控件中添加回發處理程序:

HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("Buttonid");
div.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));

另外,我實施了另一個對我有效的解決方法:

在gridview的外部,但在Updatepanel的內部,放置一個按鈕。 另外,添加一個hiddencontrol,將在其中存儲單擊的rowindex。 然后在您的模板字段中添加一個“可點擊的div”(您可以在其中放置文字標簽),該標簽會調用javascript。 這樣的事情(您可以添加無顯示樣式來隱藏按鈕)。

  <asp:Button id="Buttonid" runat="server" OnClick="Button_click_event" /> <asp:HiddenField ID="hdnRowClicked" runat="server" Value="" /> <itemtemplate> <div onclick="javascript:rowClicked('<%= Buttonid.ClientID %>','<%= hdnRowClicked.ClientID %>', this);"> <asp:Label ID="lblImgDetail" runat="server" Text="+"></asp:Label> </div> </itemtemplate> 

然后在javascript函數中,設置rowindex並調用按鈕進行回發:

  function rowClicked(btnId, hdnRowSelected, lnk) { var rowIndex = lnk.parentNode.parentNode.rowIndex - 1; $('#' + hdnRowSelected).val(rowIndex); $('#' + btnId).click(); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM