如何从代码中获取属性的值?

时间:2022-08-25 21:49:21

I have a checkbox which is responsible for showing/hiding a div. I created a custom attribute 'myDiv' and put the name of the div which the checkbox is responsible for there.

我有一个复选框,负责显示/隐藏div。我创建了一个自定义属性'myDiv'并将复选框负责的div的名称放在那里。

<asp:CheckBox ID="CheckBox1" myDiv="divRegisteration" myText=" הרשמה - " runat="server" AutoPostBack="true" Font-Size="18px" Font-Bold="true" Text=" הרשמה - הצג" OnCheckedChanged="CheckBox_CheckedChanged"/>

When I try to get the name of the div from the code behind, I get an error:

当我尝试从后面的代码中获取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"]);

it does not find the attribute "myDiv". for some reason it only finds 2 attributes which I dont even know where they came from. Is there another way to get a custom attribute?

它找不到属性“myDiv”。由于某种原因,它只找到2个属性,我甚至不知道它们来自哪里。有另一种获取自定义属性的方法吗?

2 个解决方案

#1


2  

You can only set attributes to render at client side, it is not possible to directly access the attribute value (at server side) when from is POSTED. To access the attribute value, you can use hidden field which can be set at client side through javascript.

您只能在客户端设置要呈现的属性,当来自POSTED时,无法直接访问属性值(在服务器端)。要访问属性值,您可以使用可以通过javascript在客户端设置的隐藏字段。

#2


0  

You can do that by using jquery

你可以使用jquery来做到这一点

if ($('#CheckBox1').is(':checked')) {

    $("#Urdiv").show();
} else {
    $("#Urdiv").hide();
} 

use clientId at CheckBox1

在CheckBox1上使用clientId

#1


2  

You can only set attributes to render at client side, it is not possible to directly access the attribute value (at server side) when from is POSTED. To access the attribute value, you can use hidden field which can be set at client side through javascript.

您只能在客户端设置要呈现的属性,当来自POSTED时,无法直接访问属性值(在服务器端)。要访问属性值,您可以使用可以通过javascript在客户端设置的隐藏字段。

#2


0  

You can do that by using jquery

你可以使用jquery来做到这一点

if ($('#CheckBox1').is(':checked')) {

    $("#Urdiv").show();
} else {
    $("#Urdiv").hide();
} 

use clientId at CheckBox1

在CheckBox1上使用clientId