为什么不C#查找属性? asp.net [英] Why doesn't c# find the attribute? asp.net

查看:95
本文介绍了为什么不C#查找属性? asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个复选框,下面的每个复选框是一个div。每个复选框负责显示或隐藏复选框是它下面。
例如:

 < ASP:复选框ID =CheckBox1myDiv =divRegisteration会将myText =הרשמה - =服务器的AutoPostBack =真FONT-SIZE =18像素字体粗体=真正的文本=הרשמה - הצגOnCheckedChanged =CheckBox_CheckedChanged/>
    < D​​IV ID =divRegisteration=服务器可见=假>

复选框CheckBox1负责显示或隐藏格divRegisteration,这是在自定义属性myDiv解决。

问题是,在code的后面,它没有找到的属性myDiv:

 如果(((复选框)(发件人))。经过==真)
{
  复选框CHK =(复选框)(寄件人);
  对象的div =的FindControl(chk.Attributes [myDiv]); ////它没有找到myDiv,因此没有找到控制,使程序崩溃。
  HtmlGenericControl addressDiv =(HtmlGenericControl)(DIV);
  addressDiv.Visible = TRUE;
}


解决方案

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


  

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


如果你想拥有这样的特性,你需要创建一个具有所需的属性自定义的控制。或者,作为替代,在承载一个复选框和相关的格或诸如此类的东西一个用户控件 - 那么你可以通过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>

这篇关于为什么不C#查找属性? asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆