如何遍历表以查看是否选中了复选框

时间:2014-09-30 18:04:52

标签: asp.net checkbox

我正在尝试遍历表格以查看是否选中了复选框。在按钮上单击SendCheckEmail。我收到错误EmailForm没有声明,我也不确定如何遍历表来处理检查的每个复选框。

 Sub Page_Load
    If Request.QueryString("show") <> "" then
      dim Sr as StreamReader = New StreamReader("d:\netdrvs\prs\emaillist\" &   Request.QueryString("show"),FileMode.Open)
      output="<table id=EmailForm border=1 padding=4  >" & VBCR
      output+="<tr><td>Customer Code</td><td>Email</td><td>Invoice Number</td><td>Regarding</td><td>  <input type=checkbox name=chkHeader id=chkHeader></td></tr>"
      While Not SR.EndOfStream
      dim Temp(3) as String
      Temp = Split(SR.ReadLine(),vbTab)
      dim temp2(3) as String
      Temp2 = Split(Temp(0),"_")
      output += "<tr>" & VBCR
      output += "<td>" & Temp2(1) & "</td>" & VBCR
      output += "<td>" & Temp(1) & "</td>" & VBCR
      output += "<td>" & Temp2(0) & "</td>" & VBCR
      output += "<td>" & Temp(2) & "</td>" & VBCR
      output += "<td> <input type=checkbox name=chkRow id=chkRow> </td>" & VBCR
      output += "</tr>"& VBCR   
    End While
    output+="</table>" & VBCR   
    SR.Close()
    ProcessChk.Visible = True
   end if
End Sub

Sub SendCheckEmail (sender As Object, e As System.EventArgs) 

    Dim item 

    For each item in EmailForm 
        'loop through each row
    NEXT

End Sub

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Lathams Invoice Email Sending</title>
<script type="text/javascript" src="ajax.googleapis.js"></script>
<script type="text/javascript">
  $("[id*=chkHeader]").live("click", function () {
    var chkHeader = $(this);
    var grid = $(this).closest("table");
    $("input[type=checkbox]", grid).each(function () {
        if (chkHeader.is(":checked")) {
            $(this).attr("checked", "checked");
            $("td", $(this).closest("tr")).addClass("selected");
        } else {
            $(this).removeAttr("checked");
            $("td", $(this).closest("tr")).removeClass("selected");
        }
    });
});
$("[id*=chkRow]").live("click", function () {
    var grid = $(this).closest("table");
    var chkHeader = $("[id*=chkHeader]", grid);
    if (!$(this).is(":checked")) {
        $("td", $(this).closest("tr")).removeClass("selected");
        chkHeader.removeAttr("checked");
    } else {
        $("td", $(this).closest("tr")).addClass("selected");
        if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) {
            chkHeader.attr("checked", "checked");
        }
    }
 });
</script>
</head>
<body>
<form id="form1" runat="server">
  <asp:label id="test" runat="server" />

  <%=output%>



   <p>&nbsp;</p>
    <asp:Button id="ProcessFile" visible="false" runat="server" Text="Process Emails" onClick="SendMails" />
    <asp:Button id="ProcessChk" visible="false" runat="server" Text="Process Checked Emails" OnClick="SendCheckEmail" />

 </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

EmailForm不是服务器控件,因此您无法在CodeBehind中访问它。

您可以尝试使用绑定到文件内容的转发器或表控件。

否则,您可以在输入标记中添加电子邮件地址作为value属性。       输出+ =“”&amp; VBCR

然后,您将通过访问请求(“chkRow”)

收到以逗号分隔的电子邮件地址列表

答案 1 :(得分:1)

runat="server"设为EmailForm

然后

IN C#

foreach (var item in tbl.Rows)
        {
            HtmlTableRow row = (HtmlTableRow)item;

            foreach (var item1 in row.Controls)
            {
                HtmlTableCell cell = (HtmlTableCell)item1;



            }
        }

在VB.NET中

Dim rowitem
Dim cellitem 

For Each rowitem In tbl.Rows
HtmlTableRow row = (HtmlTableRow)rowitem;


For Each cellitem In tbl.Rows
HtmlTableCell cell = (HtmlTableCell)cellitem;
// cell items

next

next


// can directly access checkbox if it is set to runat='server'