简体   繁体   English

C#CheckBox问题

[英]C# CheckBox Question

I have HTML Generated Checkboxes on a page. 我在页面上有HTML生成的复选框。 How can I check to see if they are 'Checked' with c#? 我如何检查它们是否已被c#检查? I am looking to use an if statement 我正在寻找一个if语句

if (checkbox.checked = true)
   {
        // EXECUTE CODE HERE
   }

I don't know how to call the element since it's HTML. 我不知道如何调用元素,因为它是HTML。

For my HTML I use another aspx to generate the HTML 对于我的HTML,我使用另一个aspx生成HTML

FileListLabel.Text += ("<input type='checkbox' name='option" + counter +
                       "' value='" + SPEncode.HtmlEncode(oListItem["ID"].ToString()) +
                       "'>" + SPEncode.HtmlEncode(oListItem["LinkFilename"].ToString()) + "<BR>");

Is there a way to make that runat server? 有没有办法使该运行服务器? Or Should I use the Request.Form? 还是我应该使用Request.Form?

Thank you. 谢谢。

An HTML checkbox will only be submitted if it is checked. 如果选中了HTML复选框,则将提交该复选框。

So, if it exists in the postback, it was checked. 因此,如果它存在于回发中,则对其进行检查。

使用C#,您需要将复选框声明为runat="server"才能按名称进行访问,或者检查Request.Form中的值。

In my opinion you should do this with javascript only in a way like <input onclick="__doPostBack('__Page', 'yourCheckboxNumberNIsChecked');" /> 我认为您应该仅使用<input onclick="__doPostBack('__Page', 'yourCheckboxNumberNIsChecked');" /> <input onclick="__doPostBack('__Page', 'yourCheckboxNumberNIsChecked');" /> // where yourCheckboxNumberNIsChecked is flag which you will set when you define that checkbox has property checked set in "true". <input onclick="__doPostBack('__Page', 'yourCheckboxNumberNIsChecked');" /> // // yourCheckboxNumberNIsChecked是标志,当您定义复选框的“ true”设置为已选中时,将设置该标志。 Then in code-behind you can define this event in a such way: 然后,可以在代码隐藏中以以下方式定义此事件:

If Page.IsPostBack Then
   Dim eventArg As String = Request("__EVENTARGUMENT")
   If eventArg = "yourCheckboxNumberNIsChecked" Then
       Response.Write("You check it !")
   End If
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM