我有一个复选框,负责显示/隐藏div。我创建了一个自定义属性'myDiv',并将复选框负责的div的名称放在那里。
<asp:CheckBox ID="CheckBox1" myDiv="divRegisteration" myText=" הרשמה - " runat="server" AutoPostBack="true" Font-Size="18px" Font-Bold="true" Text=" הרשמה - הצג" OnCheckedChanged="CheckBox_CheckedChanged"/>
当我尝试从后面的代码中获取div的名称时,我收到错误:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)(sender)).Checked==true)
{
CheckBox chk = (CheckBox)(sender);
object div = chk.Parent.FindControl(chk.Attributes["myDiv"]);
它找不到属性“myDiv”。由于某种原因,它只找到2个属性,我甚至不知道它们来自哪里。是否有另一种获取自定义属性的方法?
答案 0 :(得分:2)
您只能在客户端设置要呈现的属性,当来自POSTED
时,无法直接访问属性值(在服务器端)。要访问属性值,您可以使用隐藏字段,可以通过javascript在客户端设置。
答案 1 :(得分:0)
你可以使用jquery
来做到这一点if ($('#CheckBox1').is(':checked')) {
$("#Urdiv").show();
} else {
$("#Urdiv").hide();
}
在CheckBox1上使用clientId
答案 2 :(得分:0)
Yes, you can get the value of an attribute from code behind. For example, if you have a checkbox control like this:
<input type="checkbox" runat="server" id="chkBoxCreate" class="item" itemid="1" onclick="Create();" />
then you can get it's value in code behind like below:
string itemId = chkBoxCreate.Attributes["itemid"];
Enjoy :)