c# not recognizing html div element - c#

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.

Related

Find what Elements were added by Javascript

I have got an ASP-Site, which enables the user to Add Label-Elements. I don’t know how many Labels where added or which ID they have. I know only, they will be within the Panel pnl_Added. After the user has added all his labels, he pushes a Send-Button for Update.
So, now I am at my Server, awaiting this postback, but I don’t know where, when and how to find out, which Elements were Added to pnl_Added. Can somebody help me?
I have tried something like that:
protected void Page_Load(object sender, EventArgs e)
{
[...]
for (int i = 0; i < pnl_Added.Controls.Count; i++)
{
[...]
}
[...]
}
But I think it is too late because of the loaded ViewState? Is that possible?
I am working with VS 2013, ASP c#, with the .Net Framework 4.
On server, controls tree doesn't created from actual client HTML. Actually, server doesn't know anything about client HTML besides input tags values in scope of submitted form. In general, all controls available in Page_Load method, created on server side from aspx file markup.
To implement your scenario, you need to add hidden field for each label, added from client and save label's inner text into hidden field's value. Then you'll can get these labels texts as below:
var labels = Request.Form["hiddenField's name"] as string[];
You should go one lever deeper and take the added elements from Request variable, because the control pnl_Added doesn't know about them as there was no postback.
Something like this:
Request.Form["field_id"]
I suggest to run the page in debug mode, review Request.Form collection and find what you need. You should see your label elements there.

Visual Studio 2008 can't recognize control(in webform page) in partial class

I insert a Textbox control in webform page,but I can't find it in partial class when I use it like this.txtPaySerialNumber.Text,It looks like I had not insert that in webform page.
The old control I had inserted had no problem. I have develop web form a year, this crazy thing happened only in vs2008.(My OS is Windows 8 64).
It's not exactly clear without an example of your scenario, but a couple of things to check....
Make sure you're control is defined in the page (and properly, check the error view for bad markup declarations, etc.):
<asp:TextBox runat="server" ID="PaySerialNumber />
You'll likely noticed I summarily abandoned the awful Hungarian Notation.
Then in the code-behind, access it (omitting the double this prefix (which I'm not sure whether to take literally from your post or not):
PaySerialNumber.Text = "some text";
If that fails, try it in the markup file itself:
<script runat="server" language="CSharp">
void Page_Load(object sender, EventArgs e) {
PaySerialNumber.Text = "some text";
}
</script>
If that works, then you likely have a mismatched page declaration, so it's not pointing to the right code-behind file.
If all else fails and the page, code-behind, or whatever, still isn't seeing the thing, then try a "clean solution and then "rebuild"; perhaps even restarting VS to reload the project after a clean.
If it still isn't working, then we need more information.

asp.Net <control> does not exist in current context

I am facing a problem: I have taken a dropdownList control and ID is
drpDownCountries in an ASP.NET project. The dropdownlist control is placed on page, in the code behind file of C#, while typing the control name drpDownCountries, this control ID is listed in object member list.
The code-behind code looks like this:
drpDownCountries.Attributes.Add("onBlur", "ErrorHighlight('" + drpDownCountries.ClientID + "','" + lblCountry.ClientID + "');");
But when I compile the project I am getting the following error:
Error: The name 'drpDownCountries' does not exist in the current context
I have checked this thing on different machines too, and the same error is occurring. I do not understand what the reason is or how to fix it.
Right-click on the ASPX (or ascx) file, and select Convert to web application (or something like that). That will force a refresh on the designer file.
I had this same problem and what worked for me was to make a change to the .ascx file in Design view and then save it. This finally forced Visual Studio to regenerate the designer.cs file and include my new control.
I have seen this error occur when there is a copy of the .aspx page in the project folder.
Example:
Error occurs in Test.aspx.
There is a Test-copy.aspx file in the project folder.
Delete, rename with a different extension, or move Test-copy.aspx to a different folder.
Error is resolved.
It's possible there is an error in your aspx/aspx file that is causing the designer file not to be updated correctly. You could confirm this by adding something new (eg. "") and see if you can access that. If not, something is probably broken in the markup that you'll need to fix.
So first check that your ascx document is defined like so
ExampleClass.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ExampleClass.ascx.cs" Inherits="ExampleClass" %>
ExampleClass.ascx.cs
public partial class ExampleClass : System.Web.UI.UserControl
{
protected void Page_Load(object sender, System.EventArgs e)
{
}
}
In aspx this type of error often occurs when you miss runat="server"
Do not let Intellisense fool you. Sometimes (usually after fixing problems with duplicate class names), you simply need to rebuild the project and the reported errors go away. Reopening the file after the build might be necessary.
You should put some code to get help..
Anyway, the problem could be that drpDownCountries is contained within a Panel control.
The Panel control is a Container control, in that it can hold lots of
controls.
In order to access the controls within that Panel control,
you first need to "help" ASP.Net to find it.
The typical way of doing this is to use the FindControl method look here.
Code sample:
DropDownList myDrop = (DropDownList)this.Panel1.FindControl("drpDownCountries");
if(myDrop != null)
{
..somecode..
}
Recreate the project. Just create a new project and add the elements one by one and hope it won't happen again. If it does, well that's part of the Microsoft experience: recreate another project and so on, until you decide to quit your job and join open-source.
CORRECTION
I'm going to redo the project that I have been working on since the last 3 days using ASP .NET MVC. I should be using an open-source tech for sure, but too bad it's not my decision for this project to not use .NET.
If this is happening after copy/move pages to new location, or project, you may simply check if PageName.ascx.designer.cs is included in project.
There is a bug in visual studio (or maybe reshrper): It includes PageName.ascx and PageName.ascx.cs, but not PageName.ascx.designer.cs, which must be included manually.
The only thing that worked for me was to add a temp controller in the aspx file and saving it.
That generated the designer again, and my controllers are now recognized!
You can then remove the temp controller and save; it won't ruin anything.

Controls in a telerik control not accessible from .cs file

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.

Convert User Controls to Server Controls

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.

Categories

Resources