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.
Related
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 have a asp.net form with 5 HTML file input controls with runat=server and a submit button. after user selects the files and clicks the submit button, the files are upload in the server.
Problem is the HTML file input controls are editable, and user can edit the path after he has already selected the file from the browse button.
If he enters a invalid file path, the file is not uploaded because it does not exist.
How can I stop users from manually changing the file path? I have tried to make the controls read only, but it disables the browse button also.
You can't. Write your server side logic to cope with missing uploads.
you could try something like having a hidden field and when the user selects the file from browse, it populates to the hidden field as well. Then when they upload, you either read from the hidden field for your upload or using JS replace the values back to the original control and read from that.
Is this what you want to have...
You can do like that hiding the control when user select the file
and showing only the path to the user
in a div
if he disagree, let him click on a link called Choose Another next to the path div, so when user click Choose another toggle the same div with flushing the previous value
see this fiddle : http://jsfiddle.net/T2D3X/
While brian's suggestion works (Thank you brian for this), finally we decided to follow the server side handling approach. since in case of invalid file, the file size would be zero, so the response is instant and we do not need to wait till the file is uploaded because in this case there is not file in the first place.
//check if file exists
if (uploadCtrl.PostedFile.ContentLength > 0)
{
uploadCtrl.PostedFile.SaveAs(fileNameWithPath);
uploadCtrl.Dispose();
}
else
{
fileNameWithPath = "invalid";
}
You can also use these methods:
One before validating: http://jsfiddle.net/T2D3X/1/
One After validating: http://jsfiddle.net/T2D3X/2/
Friend of mine posted the same answer before I have re-modified it.
There are some internal jsfiddle error on second link you can ignore that, i have just added more functionally to check on submit what value exactly you are sending.
Let us know if you find the it helpful ;)
Thank!
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.
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.
OK I'm new to DotNetNuke and need to write a simple module in DNN that will display an article for everyone, and allow the admin to edit the article/add a new one.
I have a test page that contains a DNN module with one module definition and two controls in that definition. The default control shows the article based on an articleID field in the querystring. You then click a button that is supposed to load the edit control and pass the articleID in the query string.
If I use EditURL() in the onClick the edit control is loaded with the correct articleID, but using the admin skin. If I use Globals.NavigateURL() then the correct skin is shown but my edit control isn't loading in the page.
Any clue as to how to what I'm doing wrong or how to get the edit control loading with the correct skin?
My two methods of switching to the edit control (in my button click event) are listed below:
string newURL = this.EditUrl("articleID", Request.QueryString["articleID"], "EditArticle");
Response.Redirect(newURL);
and
string newURL = Globals.NavigateURL(this.TabId, "EditArticle","articleID="+Request.QueryString["articleID"]);
Response.Redirect(newURL);
Actually you are doing this correctly - the editurl in DNN does load the Admin skin - usually this skin is based on someone administering content so it strips out all other modules and shows the 'basics'. Right or wrong this is what it does.
If you dont want to to do that you could provide a switch in the querystring and show a seperate panel or do a multiview control and show different views based on the switch in the query string.
There are a few other approaches like changing the content area to editing text area with ajax or using popup modal style windows.