填充复选框列表(带有复选框子列表)后,如何在代码隐藏中检查它们? C#
首先,这是代码。
DataRow[] exemption = ds.Tables[2].Select("ValidExemptionTypeID='2'");
foreach (DataRow dr in exemption)
{
string exemptionType = dr["ValidExemptionTypeID"].ToString();
string exemptionID = dr["ValidExemptionID"].ToString();
string exemptionDesc = dr["validExemptionDescription"].ToString();
string displayLabel = dr["DisplayLabel"].ToString();
sb.Append("<table><tr><td colspan='3'> <br /></td></tr></table>");
sb.Append("<table align='center' width='730px'>");
sb.Append("<tr><td width='20px' align='left'><input type=\"checkbox\" ID=\"chk" + exemptionID + "\" runat=\"server\" /></td>");
sb.Append("<td align='left'><strong>" + exemptionDesc + "</strong></td>");
sb.Append("</table>");
sb.Append("<table align='center' width='630px'>");
sb.Append("<tr><td>" + displayLabel + "</td></tr>");
sb.Append("<tr><td colspan='2'> </td></tr>");
sb.Append("</table>");
sb.Append("<table style='border: 1px solid gray' align='center' width='700px'>");
sb.Append("<tr><td colspan='3'><strong>Select Exeption Reason</strong></td></tr>");
sb.Append("<tr><td><input type=\"checkbox\" ID=\"chkLocal" + exemptionID + "\" runat=\"server\" /></td>");
sb.Append("<td align='left'><strong>Local Restriction</strong></td>");
sb.Append("<td align='left'><i>NOTE: Please limit explanation to xx characters or less</i></td>");
sb.Append("</tr><tr>");
sb.Append("<td valign='top'><input type=\"checkbox\" ID=\"chkOther" + exemptionID + "\" runat=\"server\" /></td>");
sb.Append("<td valign='top' align='left'><strong>Other (Please Specify to the right)</strong></td>");
sb.Append("<td colspan=><textarea style='width: 390px; max-width: 390px;' id=\"txtOther" + exemptionID + " cols=\"100\" rows=\"2\" wrap=\"virtual\" runat=\"server\"></textarea></td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("<table><tr><td colspan='3'> <br /></td></tr></table>");
count++;
}
return sb.ToString();
}
这将遍历一个包含 14 行的数据行数组,并为每一行创建一个复选框,然后为每一行添加两个复选框以用于本地和其他豁免。我只是不明白如何检查代码隐藏中的框以确保它们被选中。任何有帮助的建议或链接将不胜感激。
First, here is the code.
DataRow[] exemption = ds.Tables[2].Select("ValidExemptionTypeID='2'");
foreach (DataRow dr in exemption)
{
string exemptionType = dr["ValidExemptionTypeID"].ToString();
string exemptionID = dr["ValidExemptionID"].ToString();
string exemptionDesc = dr["validExemptionDescription"].ToString();
string displayLabel = dr["DisplayLabel"].ToString();
sb.Append("<table><tr><td colspan='3'> <br /></td></tr></table>");
sb.Append("<table align='center' width='730px'>");
sb.Append("<tr><td width='20px' align='left'><input type=\"checkbox\" ID=\"chk" + exemptionID + "\" runat=\"server\" /></td>");
sb.Append("<td align='left'><strong>" + exemptionDesc + "</strong></td>");
sb.Append("</table>");
sb.Append("<table align='center' width='630px'>");
sb.Append("<tr><td>" + displayLabel + "</td></tr>");
sb.Append("<tr><td colspan='2'> </td></tr>");
sb.Append("</table>");
sb.Append("<table style='border: 1px solid gray' align='center' width='700px'>");
sb.Append("<tr><td colspan='3'><strong>Select Exeption Reason</strong></td></tr>");
sb.Append("<tr><td><input type=\"checkbox\" ID=\"chkLocal" + exemptionID + "\" runat=\"server\" /></td>");
sb.Append("<td align='left'><strong>Local Restriction</strong></td>");
sb.Append("<td align='left'><i>NOTE: Please limit explanation to xx characters or less</i></td>");
sb.Append("</tr><tr>");
sb.Append("<td valign='top'><input type=\"checkbox\" ID=\"chkOther" + exemptionID + "\" runat=\"server\" /></td>");
sb.Append("<td valign='top' align='left'><strong>Other (Please Specify to the right)</strong></td>");
sb.Append("<td colspan=><textarea style='width: 390px; max-width: 390px;' id=\"txtOther" + exemptionID + " cols=\"100\" rows=\"2\" wrap=\"virtual\" runat=\"server\"></textarea></td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("<table><tr><td colspan='3'> <br /></td></tr></table>");
count++;
}
return sb.ToString();
}
This goes through a datarow array with 14 rows and creates a checkbox for each one, then adds two checkboxes for each row for local and other exemptions. What I just cannot figure out is how to check the boxes in the codebehind to make sure they're checked. Any suggestions or links to help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须改变一些事情。最大的问题是您不能将服务器标签用作字符串。因此,复选框中的 runat 服务器没有执行任何操作。如果你查看生成的 html,你会看到 runat="server" 在 HTML 中。
因此,如果您想采用这种方法(这很好),请为复选框指定一个唯一的名称,例如 name=\"chkOther_dr1\" 然后您可以使用 Request.Form 或 Request.Params 来检查复选框是否已选中,如果不进行检查,它们将不会成为集合的一部分。
即使您实际创建了服务器控件,您也会遇到同样的问题,因为当您尝试在回发中检查它们时,它们将不存在。
所以...
然后让它们在请求上使用参数/表单集合并查找下划线之前的部分(使用 StartsWith)。
You have to change a few things. The big problem is you can't use server tags as a string. So the runat server in your checkboxes isn't doing anything. if you look at the generated html, you will see runat="server" is in the HTML.
so, if you want to take this approach, which is fine, then give the checkboxes a unique name like name=\"chkOther_dr1\" then you can use Request.Form or Request.Params to check to see if the boxes are checked, they will not be part of the collection if they aren't checked.
Even if you actually create the server controls you will have the same problem because they will not exist when you try to check them in your postback.
so...
and then to get them use the params / form collection on Request and look for the part before the underscore (use StartsWith).
尝试对您的其中一个复选框进行此操作,如果有效,则对其他复选框也进行相同的操作。
Try that for one of your checkboxes, if that works, than do the same for the other checkboxes too.