2017-01-02 88 views
0

網絡應用程序。我有一個帶有複選框的gridview。每當選中複選框時,我想向用戶發送電子郵件。我的gridview的列是id,名稱,emailid等。每當複選框被選中,我想發送電子郵件。我編寫了javascript代碼來趕上gridview中的所有emailids,並將所有電子郵件ID推送到數組中。我很困惑如何將這些ID帶到服務器端。 這是我的按鈕。如何獲取asp.net按鈕點擊事件內的javascript數組?

<asp:Button ID="Button1" class="submitLink blueButton" runat="server" Text="GETID" OnClientClick="javascript:sendEmail()" OnClick="Button1_Click" /> 

使用下面的代碼行我能獲得所需的電子郵件ID的

$('#<%= gdvRegretletter.ClientID %> input[type="CheckBox"]').each(function() { 
    if ($(this).closest('tr').find('input[type="checkbox"]').prop("checked") == true) 
     email.push($(this).closest('tr').find('td:eq(3)').text().trim()); 
}); 

數組電子郵件將捕獲所有必需的電子郵件ID的,但我想在服務器端Button1_Click事件中這些值。我可以有一些想法來實現這一目標嗎?感謝您的時間。

+0

http://stackoverflow.com/questions/3713/call-asp-net-function-from-javascript – Valkyrie

+0

嘿感謝您的答覆。我可以知道,除了做Ajax調用還有其他方法嗎? –

回答

0

您可以循環GridView中的所有行並查看哪個CheckBox已被選中。在這個片段中,電子郵件地址也以文字形式顯示在GridView中,但有許多其他方法可以從GridView或其原始源獲取電子郵件地址。

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:CheckBox ID="CheckBox1" runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("email") %>'></asp:Literal> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 


protected void Button1_Click(object sender, EventArgs e) 
{ 
    //loop all the rows in the gridview 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     //find the checkbox with findcontrol and cast it back to one 
     CheckBox checkbox = row.FindControl("CheckBox1") as CheckBox; 

     //is it checked? 
     if (checkbox.Checked == true) 
     { 
      //do the same for the label with the email address 
      Literal literal = row.FindControl("Literal1") as Literal; 

      string email = literal.Text; 

      //send email 
     } 
    } 
} 
+0

感謝您的回覆。我會這樣做。基本上MVC developer.new到asp.net世界 –

+0

我可以知道我應該把標籤放在哪裏嗎? –

+0

我已經更新了我的答案。但正如我所提到的,您還可以通過其他方式獲得行值。但最好的解決方案將取決於您的GridView設計。 – VDWWD