I found this question that shows properties on a custom user control (ascx) can be assigned inline as an HTML attribute: Custom attribute in UserControl (*.ascx)?
This works great but what about if I register a custom user control on my page and want to set/get attributes from that control in my code behind?
ASPX:
<%-- I can assign ActivePage inline and this works fine --%>
<wd:NavBar ID="MyNavBar" runat="server" ActivePage="navbarItem1" />
ASPX.CS:
// I need to change the ActivePage
if (what == "internal")
{
RunInternalComparison();
MyNavBar.ActivePage = "navbarItem1";
}
else if (what == "external")
{
RunExternalComparison();
MyNavBar.ActivePage = "navbarItem2";
}
That's what I want to do but it doesn't work. Is this possible?
Do'h! This actually works. I guess Visual Studio wasn't auto generating controls. Simply adding protected NavBar MyNavBar; to the top of my Page solved the problem. I hope someone else finds this useful.
Related
I have a Listview controls like this
<asp:ListView ID="categoryList" ItemType="CodeCamper.EntityLayer.Transaction.FavoriteVO" DataKeyNames="FavoriteID" GroupItemCount="1" SelectMethod="GetCategories" runat="server">
.....
</asp:ListView>
and in the code behind I'm setting the SelectMethod property
categoryList.SelectMethod = "GetbyTime";
Now if I want to call a parameterized method
public List<FavoriteVO> GetbyTime(string message)
{
...
}
How do I have to modify and assign to categoryList.SelectMethod = section ?
It looks like you want to be able to change the SelectMethod to whatever you want. Because of this, I would remove SelectMethod="GetCategories" from the control declaration and set that in codebehind as well. At least that's where I would start.
Then it will be up to you to set the categoryList.SelectMethod in the proper places in code. e.g. you probably want categoryList.SelectMethod = "GetCategories"; in if (IsPostBack) {} and then categoryList.SelectMethod = "GetbyTime"; within some other event or method, like from a button click or whatnot.
It is VITAL, I repeat VITAL to set EnableViewState="False" on the html declaration of your usercontrol, if you are hosting an asp:ListView inside an ascx control, inside an aspx control!
I wasted days of development time because of this.
I have a user control that contains the following text (among other things) in the .ascx file:
<div id="divLine" runat="server">
<-- Stuff -->
</div>
In the C# code, I have this property:
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CssStyleCollection LeftStyle
{
get { return divLeft.Style; }
}
This is pretty much copied from System.Web.UI.HtmlControls.HtmlControl.cs. However, when trying to use my custom control in .aspx pages, I can't seem to access <uwc:UserControl LeftStyle="stuff"> like I could access it directly in <div style="stuff">. Is there any way to?
No answer, so I took a different approach. divLine only contained an asp.net Label, which I exposed as a public property of the control. Apparently you can do things like <asp:Label runat="server" style="stuff"/>. Intellisense will not indicate that style is a valid attribute, but it can still be used. Just like other asp.net controls, attributes the IDE doesn't recognize will still be written to HTML. Heck, once you get style=" written, you'll even get the typical drop-down guide for it.
Hi basically i want to create a Control that can add popup to menuItems which is declared in the markup of the page. I would like the syntax to be like below:
<uc1:Sidebar id="" runat="server" enablePopup="True">
<PopUpBindings>
<PopedControlId="" BackgroundClass="" TargetItemIndex=""/>
<PopedControlId="" BackgroundClass="" TargetItemIndex=""/>
..
</PopUpBindings>
</uc1:Sidebar>
I am able to create enablePopUp using Member Fields in the asmx Page. Like Below
private bool _enablePopup;
public Boolean EnablePopup
{
get { return _enablePopup; }
set { _enablePopup = value; }
}
but creating a bindings set as in markUp 1. Never done that before. Could someone give hints to this.
Question 2:
This usercontrol is being created because modalPopup could not be created along with Repeater's Databinding. I tried the below in repeaters ItemDatabound event but would throw exception "Target control Id ={0} could not be found"
mpcpnlNewBp.TargetControlID = e.Item.ID;//open modalpopup
What you need is Collection properties on your control - see this example code from MSDN to get an idea.
If more designer support such as UI for editing properties is needed then you need to create Collection Editor - check this related example.
EDIT:
With templated controls such as repeater, target control needs to be found in the correct parent control context which Modal Popup cannot do. The solution is to use a dummy (hidden) control on the page/user control as a target control for the modal popup and then use extender's java-script API to hide/show popup as per needs (see this article for client side API).
I have created simple controls that are based on dot net controls . For example there is a simple GridView control that is based on dot net GridView control , I just set some setting in my control to use it in my .aspx pages , for example I set the Width of GridView in the constructor method :
// constructor of my custom class
public GridView(): base()
{
this.Width = new Unit(100, UnitType.Percentage);
}
and also I've added some custom properties :
public int SelectedID
{
get
{
if (ViewState["SelectedID" + this.ID] == null)
ViewState["SelectedID" + this.ID] = "-1";
return Convert.ToInt32(ViewState["SelectedID" + this.ID]);
}
set
{
ViewState["SelectedID" + this.ID] = value;
}
}
The *Problem* : when I use Tools>Generate Local Resource in VS2010
the aspx markup before I use this tool is like this :
<RPC:GridView ID="grdData" runat="server" onrowcommand="grdData_RowCommand">
but this tool adds any public property or any setting to my aspx markup , like this :
<RPC:GridView ID="grdData" runat="server" onrowcommand="grdData_RowCommand"
meta:resourcekey="grdDataResource1" SelectedID="-1" Width="100%">
I don't like VS2010 add my settings (like width) and my custom properties (like SelectedID) to aspx markup , this prevent me having the ability of changing my custom control code and reflect changes in all aspx pages that include this control , for example if
I change the width of my control to 50% , it doesn't reflect to any pages
Please tell me what should I do to fix my problem
Thank you very much for your feedbacks
This is a slightly complicated topic to address in one answer here to be honest! There are more than one approaches you can take to resolve this problem. It all depends on the kind of properties your control has and if it is a templated control or not. As a quick fix try decorating your public properties with the following attribute
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
or if you don't want the user to be able to set the public property at all via HTML markup then use
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
another attribute declaration which will be helpful with
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
is
PersistenceMode(PersistenceMode.Attribute)
I've found doing any initialisation in the ctor causes major headaches for local resource generation (even corruption). Use the DefaultValue attribute on properties and/or use OnLoad if possible. (As a side note use CSS rather than explicitly setting control width).
The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?