[英]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
won't fire when I have update panel, but it works when it is not in the update panel. 当我有更新面板时,不会触发Button_click_event
,但当它不在更新面板中时,它将起作用。
your update panel look like 您的更新面板看起来像
<asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
<ContentTemplate>
....
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Buttonid" EventName="Click"/>
</Triggers>
Try setting UpdateMode="Conditional"
and ChildrenAsTriggers="False"
to the UpdatePanel.. 尝试将UpdateMode="Conditional"
和ChildrenAsTriggers="False"
到UpdatePanel。
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
Also remove the EventName attribute for your trigger and try again.. 同时删除触发器的EventName属性,然后重试。
删除按钮的属性,单击CommandName =“ ViewItem”,然后尝试运行,如果不起作用,还添加按钮UseSubmitBehaviour = true的属性。
You could solve this in various ways. 您可以通过多种方式解决此问题。 One way is on rowdatabound in the code behind adding a control that can handle an onclick event, for example a div, and then add a postback handler to your clickable control: 一种方法是在代码中的rowdatabound上添加可处理onclick事件的控件(例如div),然后在其可单击控件中添加回发处理程序:
HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("Buttonid");
div.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));
Alternatively, I implemented another workaround that works for me: 另外,我实施了另一个对我有效的解决方法:
place a button outside your gridview, but inside the Updatepanel. 在gridview的外部,但在Updatepanel的内部,放置一个按钮。 Also, add a hiddencontrol, where you are going to store the rowindex clicked. 另外,添加一个hiddencontrol,将在其中存储单击的rowindex。 Then Add to your template field a 'clickable div' (in which you can put a label for your text), that calls a javascript. 然后在您的模板字段中添加一个“可点击的div”(您可以在其中放置文字标签),该标签会调用javascript。 Something like this (you can add a display none style to hide the button).: 这样的事情(您可以添加无显示样式来隐藏按钮)。
<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>
Then within the javascript function, set the rowindex and call the button to do the postback: 然后在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.