I think i'm missing something. What is the proper way to display an html div, for example, if the actual condition is met using Page_Onload + isPostBack + asp:button as a trigger for the event as following:
Page_Load -> Click event fired
Page load - element(a dynamically generted string for multiple table rows in this case)
status hidden
Client click submit button:
Element toggled to visible.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nothing yet
}
else
{
suppose to check on client event handle by asp button
}
}
// So here is the event fired by client
protected void imgButSbmt_Click(object sender, ImageClickEventArgs e)
{
initializeReportTable();
}
and suddenly while trying to make sure I am correct with this argument,
I could see that imgButSbmt_Click is not "hitting" (with break Point attached while in debugging mode).
So first... I could note at this time that it's an unusual behavior (and while I am writing this question supposbly related to all my projects, this one was not firing at all events, so I solved it by deleting the solution file as I was writing this post).
So now, it does work.
BUT... this time (while solution is not yet to be Created) I re-opened my website "project" via File-Open-Website.
Hit F5 ...break Point works fine in red color.. no explanation marks this time. However, all events from the button & Drop down List that are configured to AutoPostBack
protected void DDL_Month(object sender, EventArgs e)
{
initializeReportTable();
}
were not firing. All events except for Page_Load are firing now. After checking again it does fire but only on the second time!
What is happening here?
I remembered some readings I came across awhile ago that said I should check the autoeventwireup and make sure it is set to true (it's the default I think).
So these are the page directives:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Here is an example of a control DropDownList + imgButton within a table:
<td>
<asp:DropDownList ID="DDL_Month" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDL_Month"></asp:DropDownList>
</td>
<td>
<asp:ImageButton ID="imgButSbmt" ImageUrl="images/sendreq.png"
runat="server" Height="25px" OnClick="imgButSbmt_Click"
style="margin-right: 0px; border-width:" />
</td>
as sugested by #JonH , and as a solution i've already had to use in some other even bugier scenario
if you're experiencing any UCB (un identified Code Behavior)...
just do as follow
1) open a new folder as a new home for the project Work-folder
2) copy all sub folders from The Buggy project, such as images, js etc'
3) Create a new webSite / any other application you had unusual problems with
then just apply all references needed for the application , copy codes to newly created .cs/vb/aspx
accordingly , use exisiting items (your other classes that are in separeted files)
and now you're ready to continue working...
the bugs should be behind you by now
just don't forget to delete the old project and any solutions files associated with it.
hope it will help some of other fresh developers who also encounterd the U.C.B Phenomenon
Related
I have access to a server with aspx pages. I need to add a title, parapgraphs, etc to a page. The page currently only has the following line:
<%# Page Language="C#" AutoEventWireup="true" Inherits="Access.Login" %>
I do not have access to the CS files, just the DLL. Anyway, when I try to add any html to the document nothing changes. I am able to change the CSS, and if I remove the "inherits" then whatever HTML I have gets displayed, but when the "inherits" is there only the default page gets displayed and none of my additions.
Admittedly I am new to ASP and moreover I am not trying to become a guru just to add some HTML to a page, but any advice would be great, thanks!
Try putting your Page_Load embedded in the .aspx and add controls that way:
<%# Page Language="C#" AutoEventWireup="true" Inherits="Access.Login" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
Controls.Add(whatever);
}
}
</script>
<!-- Try this if the above does not work -->
<script runat="server">
new protected void Page_Load(object sender, EventArgs e) {
base.Page_Load(sender, e);
if (!Page.IsPostBack) {
Controls.Add(whatever);
}
}
</script>
Fundamentally, I'm afraid this is not possible. .NET is a single-inheritance language/framework. So when it says Inherits="Access.Login" that means you can only have it use Access.Login OR your code-behind, but not both.
That said, you could jump through some crazy hoops to accomplish your goal. Like create a brand new "wrapper" page, then in the code-behind fire off an http request to the page you want. Load the response, which will just be a really long string into a 3rd-party DOM parser, or if you're confident you're getting 100% valid XML back, use .NET's built-in XmlDocument or XDocument to parse the page, find your html elements, make your changes, then do a Response.Write with your modified content.
And that's a real-life example of going around your elbow to get to your...
I am not 100% certain this will work, but you could have a code-behind file inherit from Access.Login and use the new (override will not work if Page_Load isn't marked as virtual) keyword with Page_Load. Then you could use Inherits="YourAssembly.NewLogin".
The part I am not sure about is whether or not asp.net uses the page class or your subclass to call the Page_Load method. If page_Load was virtual, it wouldn't matter, but since it isn't the new will only be called if the page is cast into your subclass. It is worth a try though.
I want to embed a C# class in a module so that I can call the functions using buttons and click events. I have no idea how to do this. I've managed to write the class I want to use, but where do I put the code? I created a module in DNN and got this:
<%# Control Language="C#" ClassName="MailingSystem" Inherits="DotNetNuke.Entities.Modules.PortalModuleBase" %>
<h1>Congratulations</h1>
<p>You have successfully created your module. You can edit the source of the module control by selecting the View Source Action from the Action Menu.</p>
<script runat="server">
</script>
I can't put my code in here, I get all sorts of errors about namespaces not allowed, can't import classes with "Using", and so on. So what am I supposed to do? My class is working, I just need to wrap it in a module and put it on a DNN page.
It is better to start with a DotNetNuke Module Template, like this one. It isn't as easy as creating an aspx page.
simply you can double click on design part of the page , then the page load section will be appear in the page and you can put your c# code there.
You may want to do something like this:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
/// code goes here
}
</script>
If you don't want want to go the whole module template route. Do the following.
Create an webusercontrol (.ascx)
Go to the code behind file (.ascx.cs) and change the class to inherit from DotNetNuke.Entities.Modules.PortalModuleBase (your will need to add DotNetNuke.dll as reference)
Add what ever controls you want to the ascx and attach any event handlers. I prefer to do this in the page init method
In ASCX:
<asp:Button ID="btnButton" Text="Click me" runat="server" />
In Code Behind:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
btnButton.Click += btnButton_Click;
// OR
btnButton.Click += (sender, e)=> { // Button clicked! Do something };
}
protected void btnButton_Click(object sender, EventArgs e)
{
// Your button has been clicked, Do something
}
Compile code
Get [yourprojectname].dll file from the bin folder of your project and copy it into DNN's bin folder. Then, copy your module control ascx into a dedicated folder in DNN's DesktopModules Folder
Example Path: DesktopModule > YourProjectName > [YourASCXName].ascx
Login to DNN, go to Host>Extensions and click add extension. Go through the wizard making sure to set your extension type to Module (there are many different types of extensions in DNN).
Once added, you will be be taken back to the module extensions page. Scroll down and find your module extension. Click edit, go to module definitions and add a module definition with a meaningful name.
Example: YourProjectNameMainView
Then, add your ASCX file as a view to that module extension. Click save and you are done with setup
You should be able to drop your (VERY BASIC) module on a page and use it!
TL;DR: All controls within a usercontrol that's being used outside it's home project are null when that usercontrol's Page_Init/Page_Load methods are called.
The setup is like this:
Projects "UI.Frontend", "UI.ControlPanel", and "UI.Common" are "ASP.NET Web Application"s. UI.Common is never meant to be accessed directly- it just contains UserControls that are needed in both the frontend and the control panel.
So, an aspx file (SomeFrontendPage.aspx) in UI.Frontend contains the lines:
<%# Register tagprefix="BP" Namespace="UI.Common" Assembly="UI.Common" %>
and later:
<BP:MyControl runat="server" ID="ctlMyControl" />
while over in UI.Common, there's a control named MyControl (normal ascx, ascx.cs, and ascx.designer.cs files). Now, when I open SomeFrontendPage.aspx in a browser, ctlMyControl gets loaded and it's init+load methods get executed. The problem is all subcontrols of MyControl never get initialized. Example (if MyControl.ascx has a textfield of ID txtBlah):
protected void Page_Init(object sender, EventArgs e)
{
txtBlah.Text = "test";
}
The above code will run, but will cause a null pointer (well, "Object reference not set to an instance of an object") since txtBlah will be null.
Edit:
An example control would be:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="Common.MyControl" %>
Whatever: <asp:TextBox ID="txtWhatever" runat="server" />
You may find you have more problems that what is immediately shown. ASCX files aren't embedded in assemblies by default, and when they are, you then need to create a virtual path provider to access them.
Can we see an example control?
I have an ImageMap control in an ascx file of mine. I'm trying to make something happen when the user clicks on an area in the map, but the page just posts back without my "imageMap_Click" event handler never being invoked. Suggestions?
<asp:ImageMap ID="imageMap" runat="server" ImageUrl="~/images/MapImages/map.jpg" HotSpotMode="PostBack" OnClick="imageMap_Click">
My imageMap_Click looks like this, just to see if it's invoked at all:
protected void imageMap_Click(object sender, ImageMapEventArgs e)
{
throw new NotImplementedException();
}
I've found it already. It was due to the control having viewstate disabled, like this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MapControl.ascx.cs"
Inherits="XXX.MapControl" EnableViewState="false" %>
I don't understand why. Anyone care to tell me?
Just from the docs, shouldn't your image map have the different locations in it for the click mapping?
eg:
<asp:ImageMap ID="SaturnImage"
ImageUrl="~/saturn.PNG"
runat="server" OnClick="SaturnImage_Click">
<asp:CircleHotSpot AlternateText="planet" HotSpotMode=PostBack
PostBackValue="planet" Radius=40 X=100 Y=100 />
<asp:CircleHotSpot HotSpotMode=Inactive
Radius=60 X=100 Y=100 />
<asp:CircleHotSpot AlternateText="rings" HotSpotMode=PostBack
PostBackValue="rings" Radius=80 X=100 Y=100 />
</asp:ImageMap>
Because I'm assuming with ImageMap that when you click on it it only does the postback if you click on a part which is set up to be clicked. It shouldn't generated a clicked event if none of the mapped parts are clicked on.
I have an ASP.NET (C#) page with some 3rd party controls, some ajaxy stuff and some normal ASP.NET Button controls.
The Button click events do not fire when clicked.
Double-clicking the button in design mode in VS 2008 switches to the code-behind but doesn't create the event handler.
Creating the event handler manually doesn't help.
The whole page is too big to include here, but this is the top bit:
<%# Page Language="C#" MasterPageFile="~/basewidepage2.master" AutoEventWireup="true" EnableEventValidation="false" CodeFile="CompanyCompliance.aspx.cs" Inherits="CompanyCompliancePage" Title="3DSS" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%# Register Assembly="obout_Grid_NET" Namespace="Obout.Grid" TagPrefix="cc2" %>
<%# Register Src="usercontrols/CalendarEx.ascx" TagName="CalendarEx" TagPrefix="uc2" %>
<%# MasterType VirtualPath="~/basewidepage2.master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceholder1" runat="Server">
<script type="text/javascript">
// a bunch of function declarations
</script>
...and my button declaration on the page:
<asp:Button ID="LicenseCsvButton" runat="server" Text="Export to CSV" OnClick="LicenseCsvButton_Click" />
...and the code-behind:
protected void LicenseCsvButton_Click(object sender, EventArgs e)
{
// get data
CompanyCompliance cc = new CompanyCompliance(Master.theCompany.ID);
DataTable dt = cc.BusinessLicenses.Tables[0];
// send to browser as download
Tools.SendTableAsCsvToBrowser(Response, dt, "LicenseData");
}
Any ideas? could it be the ajax or something? Maybe the 3rd party "obout" grid control?
Update:
I did fix this a month or two ago, so I came back to this question to answer it for posterity but couldn't remember exactly how I fixed it! (Curse you, old age!) I had some limited success by replacing some of the ASP.NET AJAX controls with jQuery UI ones but I think the real solution was that one of the properties in the one of the tags was pointing to a control that no longer existed.
If you're in this same situation, try that and let me know what works in the comments and I'll post the correct answer.
During debugging, check the CausesValidation property of your button. For some reason, one of my pages had a button defaulting to "True." When I explicitly set it to "False" everything worked.
I know that this should be resolved by now, but just to share - I just had a similar issue. In my case, the problem was that I had a RequiredFieldValidator in a page, that wasn't visible because it was part of a popup. That validator was supposed to be invoked only when clicking its related "Save" button, but since I didn't have any ValidationGroup set, it would prevent the page to submit for any clicked button. Adding a ValidationGroup to the right button resolved the issue.
Experienced the same issue recently - cause was eventually determined to be that the button was disabled by a juavascript method onbeforesubmit (designed to prevent multiple submissions), and unfortunately web forms appears to check the enabled state of the button before it allows the codebehind event to trigger.
I had this problem just now and it was caused by another control having an AutoPostBack="true" which would submit the form before the button had a chance to submit the form to trigger the click event.
Change
CodeFile="CompanyCompliance.aspx.cs"
to
CodeBehind="CompanyCompliance.aspx.cs"
Related question: CodeFile vs CodeBehind
The inherits directive seems to point to a non-existant type.
Take a look in the code behind file of the CompanyCompliance page - ("CompanyCompliance.aspx.cs").
you should find the correct namespace and something like "public partial class xxx" where xxx is the name of the class which should correspond with your inherits directive. maybe you have to fully qualify the name as TJB already stated.
I had a similar issue, and it was really tricky. I recently migrated a VS 2005 ASP.NET 2.0 web forms app to VS 2010 ASP.NET 4.0.
It was difficult to setup and buggy as expected, but the issue of buttons not firing the click event was getting on my nerves.
In the end, it was as simple as not declaring a submit action in the site.master form. I changed:
<FORM runat="server" name="MainForm" method="post" action="Default.aspx" id="m_mainForm" enctype="multipart/form-data">
to
<FORM runat="server" name="MainForm" method="post" id="m_mainForm" enctype="multipart/form-data">
Boom. Problem solved. I hope it saves days of troubleshooting to someone. Sadly, I couldn't find anything about it on the internet by myself.
I had this problem I have changed the Button property "UseSubmitBehavior"=false. Now it is working.
For future answer seekers : If you are assigning the click handler in code behind, make sure that it is not inside an IsPostBack == false check :
void Page_Load (object oSender, EventArgs oEventArgs)
{
if (IsPostBack == false)
{
oButton += new EventHandler(oButton_Click); // does not work
}
oButton += new EventHandler(oButton_Click); // does work
}