2010-11-12 64 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