且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不C#查找属性? asp.net

更新时间:2023-11-11 19:13:58

由于属性集合不工作的方式:


  

获取任意属性的集合(只用于呈现)
  该不符合以与控件的属性。


块引用>

如果你想拥有这样的特性,你需要创建一个具有所需的属性自定义的控制。或者,作为替代,在承载一个复选框和相关的格或诸如此类的东西一个用户控件 - 那么你可以通过ID在codebehind只是参考之一相关股利。实例化控件的多个实例,你是好去。

编辑:我的WebForms福是有点生疏,但在这里不用什么

控件类:

 使用系统;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
命名空间UserControlExample {
    [ParseChildren(假)]
    公共类TogglePanel:用户控件{
        私人复选框cbToggleContent =新的复选框();
        私人面板pnlContentPlaceholder =新面板();        公共TogglePanel(){
            负载+ =的OnLoad;
        }
        公共BOOL经过{搞定;组; }        私人无效的OnLoad(对象发件人,EventArgs EventArgs的){
            Controls.Add被(cbToggleContent);
            Controls.Add被(pnlContentPlaceholder);            如果(!的IsPostBack){
                cbToggleContent.Checked =选中;
                pnlContentPlaceholder.Visible =选中;
            }            cbToggleContent.AutoPostBack =真;
            cbToggleContent.CheckedChanged + =(S,参数)=> {
                pnlContentPlaceholder.Visible = cbToggleContent.Checked;
            };
        }        保护覆盖无效AddParsedSubObject(obj对象){
            pnlContentPlaceholder.Controls.Add((控制)目标文件);
        }
    }
}

和其用法:

 <%@注册标签preFIX =a的命名空间=UserControlExample大会=UserControlExample%GT;< A:TogglePanel选中=真=服务器>
    这里这东西会显示或者基于该复选框隐藏
< / A:TogglePanel>

I have 4 checkboxes, below each checkbox is a div. Each checkbox is responsible for showing or hiding the checkbox that is below it. for example:

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

the checkbox 'CheckBox1' is responsible for showing or hiding the div "divRegisteration", which is addressed in the custom attribute "myDiv".

problem is, in the code behind, it does not find the attribute "myDiv":

if (((CheckBox)(sender)).Checked==true)
{
  CheckBox chk = (CheckBox)(sender);
  object div = FindControl(chk.Attributes["myDiv"]); //// it does not find myDiv, and therefore doesn't find the control so the program crashes.
  HtmlGenericControl addressDiv = (HtmlGenericControl)(div);
  addressDiv.Visible = true;     
}

Because the Attributes collection doesn't work that way:

Gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control.

If you want to have properties like that, you need to create your own custom control that has the properties you want. Or, as an alternative, create a UserControl that hosts a single CheckBox and associated div or whatnot -- then you can just reference the one related div by ID in the codebehind. Instantiate multiple instances of that control, and you're good to go.

Edit: my WebForms-fu is a bit rusty, but here goes nothing.

The control class:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UserControlExample {
    [ParseChildren(false)]
    public class TogglePanel : UserControl {
        private CheckBox cbToggleContent = new CheckBox();
        private Panel pnlContentPlaceholder = new Panel();

        public TogglePanel() {
            Load += OnLoad;
        }
        public bool Checked { get; set; }

        private void OnLoad(object sender, EventArgs eventArgs) {
            Controls.Add(cbToggleContent);
            Controls.Add(pnlContentPlaceholder);

            if (!IsPostBack) {
                cbToggleContent.Checked = Checked;
                pnlContentPlaceholder.Visible = Checked;
            }

            cbToggleContent.AutoPostBack = true;
            cbToggleContent.CheckedChanged += (s, args) => {
                pnlContentPlaceholder.Visible = cbToggleContent.Checked;
            };
        }

        protected override void AddParsedSubObject(object obj) {
            pnlContentPlaceholder.Controls.Add((Control) obj);
        }
    }
}

And its usage:

<%@ Register TagPrefix="a" Namespace="UserControlExample" Assembly="UserControlExample" %>

<a:TogglePanel Checked="True" runat="server">
    This stuff here will be shown or hidden based on the checkbox
</a:TogglePanel>