我有一个GridView,其中一个单元格包含一个附加了CalendarExtender的TextBox。另一个单元格拥有一个触发CalendarExtender的按钮。选择日期后,在客户端触发checkDate函数,在其结束时我想触发按钮的服务器端事件。我唯一的问题是如何确定用户点击了哪一行,所以我可以从javascript触发右键的事件?
这是我的GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblMovieId" Text='<%#Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie Name">
<ItemTemplate>
<%#Eval("MovieName") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Return Date">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtRetDate" Text='<%# ((DateTime)Eval("ReturnDate")).ToShortDateString()%>'
BackColor="#EEEEEE" BorderStyle="None"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="btnUpdate"
TargetControlID="txtRetDate" Format="dd/MM/yyyy" OnClientDateSelectionChanged="checkDate" >
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" HeaderStyle-Width="135px">
<ItemStyle VerticalAlign="Top" />
<ItemTemplate>
<asp:Button runat="server" ID="btnUpdate" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:0)
我不明白为什么你必须从JS调用服务器端的Button_Click事件处理程序... 如果您需要在javascript服务器端调用“某事”作为最后一个操作来执行javascript函数,您可以使用JQuery / Javascript来调用该函数。您应该重构代码,将服务器端单击按钮的代码放入一个方法,您可以从服务器端调用Button_Click和javascript ...
在服务器端,我假设你是button_click事件。你必须重构它,如下所示:
Public void Button_Click(object sender,eventArgs e){
FuntionIWantCall();
}
//this is the code that was into the button click befeore refactoring:
private void FuntionIWantCall(){
//Do something on the server side
}
//this is the function you can call from javascript
[WebMethod]
public static void CalledFromJS(){
FuntionIWantCall();
}
在您的页面上,您必须添加脚本管理器:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True"></asp:ScriptManager>
//at the end of your javascript function
PageMethods.CalledFromJS();