I have checkboxes in my aspx page as :
<input type="checkbox" name="daySelectors" value="monday"/>
<input type="checkbox" name="daySelectors" value="tuesday"/>
<input type="checkbox" name="daySelectors" value="wednesday"/>
I am not using CheckBoxList control as these checkboxes are at different areas in my html and i need value of only selected checkboxes. Now, I can get the values of selected Checkboxes using
String dayselector = Request.Form["daySelectors"];
It works fine until here. The problem is how can i make the checkboxes checked from code behind ie my aspx.cs page. The scenario is when i come to this page i have the checkboxes values that need to be checked by default. How can i do this using the values. One way that comes to mind is i can trigger a jquery/ Javascript function from .cs passing in the values. In that case how can i set checkbox checked using their values from jquery.
Finally i just called a Javascript method from my .cs file using
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey","SetCheckBox('sunday');",true)
and to the JavaScript method passed in the value of CheckBox that needs to be checked :
function SetCheckBox(value) {
$("input:checkbox[value=" + value + "]").attr("checked", true);
}
You cannot. If the control is not runat=server
, you don't have access to it in code behind.
如果您绝对不想使用runat =“ server”(但我认为您不想使用它的原因可能是出于误解),则可以始终在Page类中具有一些自定义属性,然后再添加属性(例如“选择”)直接进入您的复选框标记...
<input type="checkbox" selected="<%=SelectedAttribute%>" .... />
In the checkbox tag:
in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
try
{
if (Request.Form["Archived"].ToString() == "1")
{
Session["checkboxstate"] = " checked='checked' ";
}
}
catch (Exception eee)
{
Session["checkboxstate"] = "";
}
}
}
Worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.