2010-11-12 67 views
0

我有一个GridView,其中一个单元格包含附加了CalendarExtender的TextBox。另一个单元格包含触发CalendarExtender的按钮。选择一个日期后,会在客户端触发一个checkDate函数,并在其结尾处触发该按钮的服务器端事件。我唯一的问题是如何弄清楚用户点击了哪一行,这样我就可以触发javascript的右键事件了?在GridView中获取正确的行

这里是我的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

我不明白你为什么要呼吁从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(); 
+0

当CalendarExtender由一个按钮触发,按钮的服务器端button_click事件不是触发。这就是为什么我完成后必须从JavaScript触发它。在你的回答中,我仍然不知道用户在哪一行点击了按钮(它出现在每一行上),并且我仍然需要它从该行提取数据,以查找事件背后的代码。 – Guy 2010-11-12 10:13:57

+0

您可以请发表gridView源代码吗? (只是“HTML”) – 2010-11-12 11:46:44

+0

好吧,我已经添加了我的GridView的简化版本 – Guy 2010-11-12 20:33:06