wpf 如何从代码隐藏中禁用用户控制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12176347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable user control from code-behind?
提问by RobinHood
I have 2 user controls in an ASPX page. By default the second user control should be disabled. In that user control i am having only one text box. So i find the control in the page load event like this:
我在 ASPX 页面中有 2 个用户控件。默认情况下,应禁用第二个用户控件。在那个用户控件中,我只有一个文本框。所以我在页面加载事件中找到了这样的控件:
TextBox txtLocation = (TextBox)PI_CompLocationTree.FindControl("txtLocation");
txtLocation.Enabled = false;
But i am getting txtLocation as null. How do I get the control in the ASPX page from the ASCX control?
但我得到 txtLocation 为空。如何从ASCX控件获取ASPX页面中的控件?
My Updated Code..In Aspx page..
我更新的代码..在 Aspx 页面..
<%@ Register Src="~/UserControl/PI_CompLocationTree.ascx" TagName="PI_CompLocationTree"
TagPrefix="uc1" %>
<div id="Div2">
<div class="location">
<div class="usLocation">
<uc1:PI_CompLocationTree ID="PI_CompLocationTree1" runat="server"/>
</div>
</div>
</div>
In Page Load...
在页面加载...
PI_CompLocationTree PI_CompLocationTree = new PI_CompLocationTree();
protected void Page_Init(object sender, EventArgs e)
{
var userControl = (PI_CompLocationTree)this.FindControl("PI_CompLocationTree1");
userControl.EnabledTextBox = false;
}
In ASCX Page...
在 ASCX 页面...
<asp:TextBox ID="txtLocation" CssClass="fadded_text fadded_text_ctrl" Style="width: 260px;
float: left;" runat="server" Text=""></asp:TextBox>
In ASCX Code Behind...
在背后的 ASCX 代码中...
public partial class PI_CompLocationTree : System.Web.UI.UserControl
{
public bool EnabledTextBox
{
get { return txtLoc.Enabled; }
set { txtLoc.Enabled = value; }
}
}
回答by Amol Kolekar
Use FindControl Methods as Follow..
使用 FindControl 方法如下..
1. UserControlClass objOfUserControl = (UserControlClass)Page.FindControl("UserControlID");
TextBox txtLocation= objOfUserControl.FindControl("txtLocation");
txtLocation.Enabled = false;
2.You Can Also use Public Property as Follow
2.你也可以使用公共属性如下
In User Control Codebehind
在用户控制代码隐藏中
public bool TextBoxUSC
{
set{txtLocation.Enabled = value;}
}
In Aspx Code Behind
在 Aspx 代码背后
UserControlClass.TextBoxUSC=false;
If You are using Master Page
如果您使用的是母版页
ContentPlaceHolder cph = (ContentPlaceHolder)this.Page.Master.FindControl("MainContent");//"MainContent" is ContentPlaceHolder's ID
UserControlClass userCntrl = (UserControlClass)cph.FindControl("UserControlID");
userCntrl.TextBoxUSC = false;
回答by Harry89pl
Try this
尝试这个
Edited
已编辑
Make Enabledfalsein aspx you can make like this:
Enabledfalse在 aspx 中制作,您可以这样制作:
Add property to your UC:
将属性添加到您的 UC:
public bool EnabledTextBox
{
get{return IdTextBox.Enabled;}
set{IdTextBox.Enabled=value;}
}
then in aspx:
然后在aspx中:
IdOfYourUserControlWithProperty.EnabledTextBox = false;
Hope it helps
希望能帮助到你
回答by Steve
PI_CompLocationTree1.EnabledTextBox = false; //from .aspx page. There's no need to use FindControl if you've added it statically to the webpage.
回答by Aghilas Yakoub
Robin You can delete
罗宾 你可以删除
TextBox txtLocation = (TextBox)PI_CompLocationTree.FindControl("txtLocation");
txtLocation.Enabled = false;
In your aspx, add forms with runat="server"
在您的 aspx 中,添加带有 runat="server" 的表单
Delete also
也删除
PI_CompLocationTree PI_CompLocationTree = new PI_CompLocationTree();
You don't need because you use FindControl
您不需要,因为您使用 FindControl
Your work is good
你的作品很好

