I am trying to use this solution to access items inside a telerik menu item:
ascx code:
<asp:Label ID="DivLeave" runat="server"></asp:Label>
In the ascx.cs file I run this code to disable the asp label
RadMenuItem expenses = RadMenu1.FindItemByText("Expenses");
Label DivLeave = (Label)expenses.FindControl("DivLeave");
DivLeave.Visible = false;
but I get this error when I try to run the code:
{"Object reference not set to an instance of an object."}
Can anyone tell me how to fix this problem. I really need to run this server side as code surrounding the above code does some work server side and it will all fit in neatly...
Kind regards
This is because the name of your label is not "DivLeave" when the HTML for your form is rendered. Since it is inside a user control it will be a combination of the user control name on the page and then "DivLeave". You should be able to see the name by looking at the code behind. Also why can't you just reference DivLeave.Visible without using the FindControl? Its an ASP.NET control with the runat server attribute so it should be available to you.
Can you do a quickwatch for 'expenses' object in Visual Studio and see if 'DivLeave' is available? It may so happen that:
The label control is available but at a different level in the object.
The label control itself is not getting added to the parent 'expenses'.
Also, it would be a good idea to do a null check for expenses and DivLeave objects before accessing them.
Related
I am having trouble executing a control inside the <script runat="server"> tags in an *.aspx page.
The control works when it is defined declaratively in the HTML section of the page, but I am unable to make it work when placed within the script tag itself.
At the beginning of the page I register my control with:
<%# Register assembly="App_Web_exemple.ascx.cc671b29" namespace="Moncontrol" tagprefix="moncontrol" %>
Then, in the HTML, I call it (successfully) with the following declaration:
<moncontrol:exemple ISBN="9782894646151" runat="server" />
When I try to add it programmatically within the <script runat="server">, however, I am unable to execute it. I tried with the tags <asp:Text /> and <asp:Literal />, as follows, but that also doesn't doesn’t work.
In the HTML part:
<asp:Text id="TestControl" runat="server" />
In the script part
TestControl.Text = "<moncontrol:exemple ISBN=\"9782894646151\" runat=\"server\" />";
To clarify, what you're looking to do is programmatically add a User Control to your Web Forms page at runtime. There are a few ways of accomplishing this.
Before we begin, it's worth noting that the code you wrote likely "works" insomuch that it compiles and doesn't throw a runtime error. But it's also not executing the control. I suspect if you look at your HTML, you'll find the control declaration being output as a string literal (i.e., unprocessed by the server). It is then disregarded by the browser since it doesn't know what the <moncontrol:exemple /> tag represents. That's obviously not what you want.
Establishing a Control Container
Regardless of which approach you take, you'll want to start with some type of container on your page that you can add the control to, such as a Panel. If you don't want the container to output any wrapper markup, you can use a Placeholder:
<asp:Placeholder id="ControlContainer" runat="server" />
This serves a similar purpose as your current Text control, except its only purpose is to provide a container that you will add your user control to. From ASP.NET's perspective, however, this can be any type of server control, including a <script runat="server">, as per your request. More on that later.
Programmatically Creating the Control
Next, you're going to create the control programmatically. This is where we run into various options. The most universal approach is to use ParseControl() method (reference). This looks something like this:
Control control = Page.ParseControl("<%# Register assembly=\"App_Web_exemple.ascx.cc671b29\" namespace=\"Moncontrol\" tagprefix=\"moncontrol\" %><moncontrol:exemple ISBN=\"9782894646151\" runat=\"server\" />");
That will parse the control using the same method that processes the declarative syntax on the page, and return a Control object with your Exemple control as the first control in its Controls collection.
I find that syntax a bit sloppy, however, since it's representing a .NET object and its properties as a string literal. Given that, there are some cleaner approaches. In this case, it appears that your control is being compiled into an assembly and, therefore, likely has a Code Behind defined which inherits from UserControl. If so, you should be able to simply do something like:
Exemple control = new Exemple();
And then set the properties on it programmatically, the way you would in any other C# object. Much cleaner!
If your control was instead being compiled dynamically by the server, then you'd instead use the Reference directive with the LoadControl() method, as described in the MSDN article How to: Create Instances of ASP.NET User Controls Programmatically. I don't believe that method will work for you, however.
Adding the Control Instance to the Page
Regardless of which approach you take, the next step is the same: you then add the control you've programmatically added to your page by adding it to the Controls collection of the target container. E.g.,:
ControlContainer.Controls.Add(control);
Note: You can technically just add this to the Page class's Control collection, too, but that doesn't give you any control over where on the page it is placed; having a PlaceHolder control (or equivalent) lets you specify exactly where you want the control to appear.
I hope this helps. There are a couple of caveats depending on how you wrote and compiled your control, but this should give you the basic structure needed to address your problem.
best to show this by code example:
html
<div runat="server" id="PI"> </div>
c#
protected void addNewProject_Click(object sender, EventArgs e)
{
PI.attributes.add("z-index", "0");
}
basically gives me an error saying "the name PI does not exist int he current context."
Any idea why it says that?
Make sure that your .designer.* file has been updated with a variable for PI. Some source control systems lock the file and prevent Visual Studio from automatically updating the file.
If the variable hasn't been created for you, you can always go back to your .designer file and add the new variable yourself. Just follow the patter for your other ASP.NET controls.
You're looking for a partial class with the same name as your code behind class.
This SO question addresses your issue as well.
I found an alternative solution, instead of making the image switch z-indexes, I am making the asp.net panel switch z-indexes. For some reason when I try to declare my object in designer.cs file...it registers in code behind. This is good, however right when I test it out on a web-browser, the designer file auto-regenerates and gets rid of it again. So instead of playing around with the html controls I played around with what I know works and used the asp.net panel controls.
I always declare them in my codebehind file, something like this:
var myDiv = new HtmlGenericControl("div");
Then you just treat it the same as any other control on your page.
i added some controls in designer file but cs file not accepting those controls, it says textbox1 does not exist in current context.
i have tried these solutions:
1) convert to web application
2) recreated both files .aspx and .aspx.cs
but it didn't solve my problem ?
Any help will be appreciated.
Its hard to analyze the situation ....
may be because of the container (like panel)
try like this
TextBox tb=(TextBox)<"container name">.FindControl("textbox1");
when u are creating a new aspx page make sure u select place code in separate file.if it's ok, then after creating the aspxx file drag and drop a textbox from toolbox's standard option. then check the id of the textbox from property window. use the id like this textbox1.Text. if you are using html text box then make it runat="server" and use the id of the textbox.
I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks
You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.
Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).
I'm wondering if anyone has any experience converting User controls to Web controls?
Ideally, I'd like to offload some of the design work to others, who would give me nicely laid out User Controls. Then, I could go through the process of converting, compiling, testing and deploying.
Until MS comes up with the magic "Convert to Server Control" option, it looks like I'm pretty well stuck with re-writing from scratch. Any ideas?
Is there a reason you must convert these user controls to server controls? Remember that it is possible to compile a user control into an assembly.
You are right there is no magic bullet here but since you already have a User Control its not that difficult.
Make sure all properties, events, etc. are set in the code behind since you won't have any mark up when you're done
Create a new Server Control
Paste all of the event handling and property setting code into the new control
override the Render method for each child control call the RenderControl Method passing in the supplied HtmlTextWriter
protected override void Render(HtmlTextWriter writer)
{
TextBox box = new TextBox();
//Set all the properties here
box.RenderControl(writer);
base.Render(writer);
}
I searched for hours and found many blogs about it.
The only thing worked for me was this article https://blogs.msdn.microsoft.com/davidebb/2005/10/31/turning-an-ascx-user-control-into-a-redistributable-custom-control/.
It says self-contained with given restrictions, but it does not mentions that the codebehind must be included in ascx file.
I used a Web Site project (not Web application!) and had to inline the code behind into the ascx file and only use control directive like:
<%# Control Language="C#" ClassName="MyPackage.MyControl"%>
So basically i just have a single file left for the user control. When codebehind was a separate file all control's where null when i referenced the final dll.
I also tried http://blog.janjonas.net/2012-04-06/asp_net-howto-user-control-library-compile-dll-file but with reflection the ascx file could not be found.