Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在数据绑定之前在何处初始化usercontrol属性?_C#_Asp.net_Events_User Controls - Fatal编程技术网

C# 在数据绑定之前在何处初始化usercontrol属性?

C# 在数据绑定之前在何处初始化usercontrol属性?,c#,asp.net,events,user-controls,C#,Asp.net,Events,User Controls,如何编写C#代码(aspx)以使用一些默认属性值初始化用户控件?我是说在哪里/哪项活动?我在容器页面上绑定了一个带有gridview的用户控件。在容器页面的页面加载时,我绑定数据。我尝试在initproperties中编写初始化代码,但没有成功。请建议在哪里设置usercontrol的默认值 编辑: 正如杜克所建议的。。我已经有了一个属性,我更改了其中一个div的宽度。这是不起作用的代码 public int CollapsiblePanelWidth { set

如何编写C#代码(aspx)以使用一些默认属性值初始化用户控件?我是说在哪里/哪项活动?我在容器页面上绑定了一个带有gridview的用户控件。在容器页面的页面加载时,我绑定数据。我尝试在initproperties中编写初始化代码,但没有成功。请建议在哪里设置usercontrol的默认值

编辑: 正如杜克所建议的。。我已经有了一个属性,我更改了其中一个div的宽度。这是不起作用的代码

public int CollapsiblePanelWidth
    {
        set
        {
            if (DataDiv.Attributes["style"] != null)
            {
                if (DataDiv.Attributes["style"].Contains("width:"))
                {
                    string[] array = DataDiv.Attributes["style"].Split(new char[] { ';' });
                    array = Array.FindAll(array, ContainsWidthAttribute);
                    string result = "";
                    foreach (string s in array)
                    {
                        result += s + "; ";
                    }                   
                    DataDiv.Attributes["style"] = result + " width: " + Convert.ToString(value != null ? value : COLLAPSIBLEPANELWIDTH) + "px;";
                }
                else
                {
                    DataDiv.Attributes["style"] += " width: " + Convert.ToString(value != null? value:COLLAPSIBLEPANELWIDTH) + "px;";
                }
            }
            else
            {
                DataDiv.Attributes.Add("style", "width: " + Convert.ToString(value != null ? value : COLLAPSIBLEPANELWIDTH) + "px;");
            }
            GridView1.Width = Unit.Parse(Convert.ToString(value != null ? value : COLLAPSIBLEPANELWIDTH - 2));
        }
    }

只有在我设置调用程序的宽度时,这才有效。否则,不会添加默认值
style=“width:105px;”“

如何创建用户控件的属性并为其提供默认值?这些属性将在初始化UserControl时设置,您可以在代码中更改它们

您可以将对属性的访问指定为公用、专用或受保护。然后,您可以从父级更改公共属性


也许您可以将代码更改为真正简单的代码,直到现有属性正常工作为止。排除问题出在代码而不是属性本身的可能性。让属性工作,然后让它做一些复杂的事情。

基本上,您需要的是虚拟属性CollablePanelWidth,它只处理获取宽度或返回默认值(如果尚未设置)

然后在UserControl的
override void OnPreRender(EventArgs)
方法中执行样式设置代码

编辑:这样的东西应该足够了

public MyUserControl()
{
    CollapsiblePanelWidth = 105;
}

public int CollapsiblePanelWidth { get; set; }

protected override void OnPreRender(EventArgs e)
{
    DataDiv.Style[HtmlTextWriterStyle.Width] = CollapsiblePanelWidth.ToString();
    GridView1.Width = CollapsiblePanelWidth - 2;

    base.OnPreRender(e);
}

我实际上有一处房产,但似乎不起作用。基本上,我只有一个Set属性,我设置了DIV的宽度,比如:DataDiv.Attributes[“style”]+=“width:”+Convert.ToString(value!=null?value:collapsablepanelwidth)+“px;”;如果用户没有在其页面上指定,我希望div获得默认值。但是在容器页面的page_load事件之后调用OnPreRender。用户在页面加载中设置宽度,并在prerender中用我的默认值覆盖该宽度。@user465876:我添加了一个代码示例来说明我的意思。默认值在构造函数中设置。因此,如果用户不更改它,则使用默认值。如果用户更改它,则使用用户的选择。我还简化了你的风格设置代码。格雷格,你太棒了!感谢您提供了精彩的解决方案,代码简单易懂。祝您有个美好的一天!既然每次回发时都会调用它,为什么不直接在If(page.IsPostBack)下的父窗体的page_load下编写呢?