I am simly trying to call the code from a code behind immediateky, as soon as the page loads.
How can I accomplish this? I tried making an invisible button and then tried clicking that on load, but that is not working for me. Hoping there is a simpler way.
This is the code behind. Filename is CodeBehind.aspx.cs:
public partial class DoTheThing: InjectablePageBase
{
private string GetURL(Object sender, EventArgs e){
// RUN MY CODE
}
}
And this is the relevant part of the .aspx file:
<%# Page Language="C#" AutoEventWireup="false" CodeBehind="CodeBehind.aspx.cs" Inherits="Test.GuideMe.DoTheThing" MasterPageFile="~/Theme/Framework.Master"%>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="PageContentPlaceHolder">
<script Language="JavaScript">
window.onload = function() { document.querySelector('#invisibleGuideMeBtn').click() };
</script>
<div>
<a id="invisibleGuideMeBtn" href="#" onclick="<%= GetURL %>" target="_self" runat="server">The Link</a>
<%-- <asp:Button ID="invisibleGuideMeBtn" Text="Edit" onclick="GetURL" CssClass="btn btn-primary"/> --%>
<%-- <asp:LinkButton id="invisibleGuideMeBtn" Text="" OnClientClick="GetURL" runat="server"/> --%>
<%-- <u><b><asp:LinkButton OnClick="GetGuideMeURL" runat="server">Click here</asp:LinkButton></b></u> --%>
</div>
</asp:Content>
Above are 4 different ways I tried clicking a button/link to call the code behind. I either get the error message:
Uncaught TypeError: Cannot read properties of null (reading 'click')
at window.onload
OR the error:
The function GetURL cannot be found
I came across this error once and the following fixed it, not sure if it'll work for you.
I used <%#CSCodeName() %> this format to get the code behind and sometimes I'd have to add a semi-colon like this <%#CSCodeName(); %>
so, the page is to load, then display and THEN we are to click a button and run some code.
but, that means the page will re-load, and after page load, your code runs again then right?
so, there has to be additional information to correctly answer this question. You can certainly with ease trigger some code, and even have such code click a hidden button to run your code stub in code behind. But that of course will cause the page to post back, re-load, and now we back to after the page loads - which it just did, we going to run the code again?
So, what criteria are you to use after page load to run, or not run the code you want again? Without this information, then after page load, we click that hidden button, and then page will load again, and after page load, the code will click the button again and again, then right?
Related
I have a page "Main2.aspx" and inside this page, I have a button "Transactions" upon clicking this button opens a popup form "Transactions" and Inside this popup, I have coded a button "AddRadbutton" and Upon clicking this "AddRadbutton" on using Javascript URL redirection a new popup "SaveDetails" form will appear.
This "SaveDetails" form is having a "SaveRadButton", upon clicking this "SaveRadButton" my form details will be saved and the popup closes successfully.
The problem I'm facing here is, "AddRadbutton" on the "Transactions" form is still visible even after filling and submitting the details using "SaveRadButton" on "SaveDetails" form.
Earlier, I'm able to achieve the same scenario by coding on Server Side, when I have used the same "SaveDetails" popup page in User Control Page which is inside "Main1.aspx" but not here on Main2.aspx.
In Main2.aspx page I can only achieve this only when page refresh happens. but I need this "AddRadbutton" to be Hidden Immeadiately after submitting the form in "SaveDetails" popup form.
Techniques I've tried so far:
Below is the Code for AddRadButton (which is in Transactions popup)
<div id="AddDetailsDiv" runat="server" visible="true" style="width: auto; height: auto; float: left; margin-right: 10px;">
<telerik:RadButton ID="AddRadButton" runat="server" Text="Add Details"
CausesValidation="False" TabIndex="14" OnClientClicking="showDetailsWindow" UseSubmitBehavior="false" >
</telerik:RadButton>
</div>
Below is the Code for SaveRadButton (which is in SaveDetails popup)
<telerik:RadButton ID="SaveRadButton" runat="server" Text="Save" OnClick="InsertRadButton"
In Server Side for OnClick is there any chance of handling this issue using below line:
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "RadWindow", "CloseBind();", true);
I've tried coding another Javascript function and use it in OnClientClicking or OnClientClicked and call both functions but unable to achieve it because Telerik UI is not supporting or I might be missing something else.
I've tried this basic AddRadButton.Visible = false on server side, its working, but only after page refresh but I needed it to be hidden Immeadiately after popup form submission.
I've tried handling in the below code() but unable to achieve it.
function CloseBind() {
var oWindow = GetRadWindow();
oWindow.close();
oWindow.BrowserWindow.loadDetails();
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
I've searched and tried many stackoverflow-related questions, scenarios and also on many community developer sites but those didn't help me either.
Any Ideas or suggestions would be appreciated.
AddRadButton.Visible = false
should work. You will need to make sure that it's inside an if which checks for IsPostback. If that does not work for you, then it's possible that a later event in the WebForms lifecycle overrides it, maybe a render-related event is where this should be implemented, like OnPreRender. You could also register a callback script (see How can I do a call back to the code behind method using javascript (properly)?) and invoke a Javascript functions that changes the display styling to none.
I've tried to solve the problem by introducing an alert() before the window is closed so that the page refresh happens upon alert "OK" button click from the user side.
I've tried by calling an alert function in telerik: RadWindow using OnClientClose="saveDetailsWindowClose"
<telerik:RadWindowManager EnableShadow="true" Behaviors="Maximize, Close" ID="MainRadWindowManager"
DestroyOnClose="false" Opacity="100" runat="server" Modal="true" AutoSize="false"
CssClass="lte_colpopup" KeepInScreenBounds="true" RestrictionZoneID="MainContent"
VisibleStatusbar="false" style="overflow: hidden !important">
<Windows>
<telerik:RadWindow ID="MyRadWindow" ReloadOnShow="true" ShowContentDuringLoad="false" OnClientClose="saveDetailsWindowClose"
Width="950" Height="450" runat="server" Title="Person Details">
</telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>
<telerik:RadScriptBlock ID="MasterScriptBlock" runat="server">
I've used below simple Javascript function to alert the User:
<script type="text/javascript">
function saveDetailsWindowClose(sender, args) {
alert('A Screen refresh is mandatory to load the details, so please click "OK" button to proceed');
window.location.reload();
}
</script>
</telerik:RadScriptBlock>
This is how I solved the Issue, fortunately, AddRadbutton on both these Main1.aspx and Main2.aspx and also on popup pages are working as intended. Luckily it didn't hamper with the closing of a window in UserControl Page on Main1.aspx which is using the same saveDetails.aspx page.
I have created an aspx page "Analyse.aspx", I want to pass an argument to this page when calling it on a button click of another page say 'Master.aspx' by-
OnClientClick="javascript:window.open('Analyse.aspx?Param=');"
how to do it?
"Could not load type" can happen due to various reasons and not because of popup. Try to call Analyse.aspx directly in your browser and make sure it works. To troubleshoot read Parser Error Message: Could not load type 'webmarketing'
UPDATE
Your code is not well formed.
Instead of
OnClick="javascript:window.open(Analyse.aspx?Param=""');"
must be
OnClick="javascript:window.open('Analyse.aspx?Param=');"
UPDATE #2
To wire a js code to js onclick event you should use the OnClientClick() event
<asp:Button ID="btnDeDupCheck" runat="server"
OnClientClick="javascript:window.open('PieChart.aspx?Param=');"
Text="De-Duplication Check" Visible="False" />
The OnClick() event is used to run a .net code.
UPDATE #3
To add values from asp.net controls you need to inject asp.net code. Read How to pass a value into a javascript function within an aspx page onClientClick or OnClientClick Javascript:confirm get value in textbox?
<asp:Button ID="btnDeDupCheck" runat="server"
OnClientClick="javascript:window.open('PieChart.aspx?Param=<%=txt1.ClientID%>');"
Text="De-Duplication Check" Visible="False" />
I finally got the it,successful executed code-
OnClientClick="javascript:window.open('Analyse.aspx?Param=');"
but my new query is how to pass parameter from current page to this new page 'Analyse.aspx',
say my parameter is - "txtPwd.text"
and how to catch the argument on pageload of Analyse.aspx
I am working with aps.net and vb.net
In my application after submitting the web form (application.aspx) users are redirected into another (application_complete.aspx) page .
The webform in application.aspx page is very long. and user has to scroll down a lot to complete the form and submit. Problem is after submitting the the form (appplication.aspx) the application_complete.aspx page also becomes very long (same size as the application.aspx) although there is not much content in it and makes the browser automatically position at same place (bottom of the page). Therefore user has to scroll up to read the message. which is very irritating for the users. (see picures)
picture1 : (appplication.aspx)
picture2: application_complete.aspx
What I require
I want to make the page (application_complete.aspx) position at the top of the page when it loads and set focus at the message section of that page.
This is my application_complete.aspx page code
<%# Page Title="" Language="VB" MasterPageFile="~/c/MasterPage.master" AutoEventWireup="false" CodeFile="application-complete.aspx.vb" Inherits="c_application_complete" %>
<%# MasterType VirtualPath="~/c/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" Runat="Server">
<h2>Thank you! we have now received your application</h2>
<div class="btn-wrapper">
<a target="_parent" href="<%= Master.EmployerCareersHome %>" class="btn v-view-all">View other open positions</a>
</div>
</asp:Content>
and this is my application_complete.aspx.vb code
Partial Class c_application_complete
Inherits System.Web.UI.Page
End Class
I have tried adding the following script in application_complete.aspx page and by giving a id to the div section.
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptContent" runat="server">
<script type="text/javascript">
$(document).ready(function() {
$("#<%= dvbtn.ClientID %>").focus();
});
</script>
</asp:Content>
and in application_complete.aspx.vb page.
Protected Sub PageLoad(ByVal sender As Object, ByVal e As EventArgs)
Page.SetFocus(dvbtn)
End Sub
but thats not working.
For your information
the whole webform and its all functionality is in a iframe .is it possible to make parent web page scroll to a specific position (at the top) from an iframe.
Though there is not much content in application_complete page still because of long form (in application.aspx page) this page(application_complete) also becomes long.and if someone refreshes it then the page gets back to its original size and focuses on the top position. but refreshing causes the re submission of the form so can't do that.
Looking forward to your help.
In you page put an anchor something like:
<a name="loadtohere" />
Then in your redirect, send the page to:
page.aspx#loadtohere
EDIT:
OK, in your source code file add the html from above:
<a name="loadtohere" />
Then in your code behind:
// Create the redirect link (better way of creating your string)
Dim redirect = String.Format("application-complete.aspx?e={0}&b={1}&i={2}&hp={3}&hI={4}#loadtohere, Vacancy.EmployerID, CInt(Request("b")), Vacancy.ID, Request("hp"), Request("hI"))
// Redirect to the page
Response.Redirect(redirect)
The "loadtohere" text can be changed to whatever you want, it is just the name of the anchor to move the page to when it loads.
The reason that my element wasn't receiving focus was because it is a <div> and not an actual <button> element. <div> elements by default aren't picked up as part of the tab order so they don't generally receive focus at all.
So i solved the focusing problem by adding a tabindex attribute to my div element.
<div class="btn-wrapper" id="dvbtn" runat="server" tabindex="0" autofocus>
<a target="_parent" href="<%= Master.EmployerCareersHome %>" class="btn v-view-all">View other open positions</a>
</div>
and with the script
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptContent" runat="server">
<script type="text/javascript">
$(document).ready(function() {
$("#<%= dvbtn.ClientID %>").focus();
});
</script>
</asp:Content>
setfocus also solved the problem of scrolling.
I am attempting to populate a html page by passing in values using QueryString and my values are passing in the QueryString but my limited to NO knowledge of JS is preventing me from being able to deduce why the textbox on the page isn't populating with the passed value.
This is my HTML showing the JS Function
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Test.aspx.cs" Inherits="TestProject.Pages.Test" %>
<asp:Content ID="ContentHeaderID" ContentPlaceHolderID="MainContent" runat="Server">
<div class="BackgroundOfWhite">
<font class="BB">Select Instructor:</font>
<asp:DropDownList runat="server" ID="dropdown1"
AutoPostBack="true" CssClass="DropDownLists" ></asp:DropDownList>
<asp:Button runat="server" ID="btnOpenPage"
CssClass="Buttons" Text="Open Page With Params" OnClick="btnLoadPage_Click" />
<div class="White"></div>
</div>
<script type="text/javascript">
document.getElementById('InstructorName').value = Instructor;
</script>
This is my C# info here
protected void btnLoadPage_Click(object sender, EventArgs e)
{
string openthis = "http://whiskeyinthewatertestofsendingdata.html";
string Instructor = "Tyler Moore";
Response.Redirect(openthis+"?"+Instructor);
}
I feel that the issue is I am not actually calling the JS function to populate the textbox on the hmtl page, but how would I do such?
EDIT:
This is the html behind the textbox
<input id="InstructorName" name="InstructorName" maxlength="255" style="width: 240px;">
EDIT 2
I see this 1st few lines of HTML of the page...does this mean on the page load they force the fields to have a null value (which of course would mean their is no way to achieve what I am after)
<head>
<script type="text/javascript">
var forcefieldstonull =
{
"InstructorName":null,
"InstructorClass":null,
"InstructorBuilding":null,
"InstructorRoomNum":null
};
Try this:
protected void btnLoadPage_Click(object sender, EventArgs e)
{
string openthis = "http://whiskeyinthewatertestofsendingdata.html";
string Instructor = "Tyler Moore";
Response.Redirect(openthis+"?Instructor="+Instructor);
}
and then, on your page, change your javascript function to do it like this:
<script type="text/javascript">
document.getElementById('InstructorName').value = '<%=Request.QueryString["Instructor"]%>';
</script>
You have to wait for the page to be loaded completely before you can change it's elements.
Though the javascript is at the bottom it comes to my mind that it might be executed before the InstructorName div is rendered.
You should surround it with window.onload() to make sure that it is executed after the page is fully loaded. https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onload
Additionally what you can do is simply check the Browsers console if the script gives you an error.
So what I want to do is click on a button which will cause JS code to run which will cause another button to click and button #2 will be runat="server" and cause a C# function to run onClick.
But using the below does not cause the C# function to run even though simply clicking the button2 does cause it to run.
function button1_click() {
document.getElementById('button2').click();
__doPostBack('#button2', 'onclick');
$('#button2').trigger('click');
// neither worked (tried them seperately)
}
<asp:Button runat="server" Visible="true" text="you should not see this" ClientIDMode="Static" ID="button2" onclick="button2_Click" formnovalidate />
Useful knowledge:
- button one is in a client form
- button two is in a runat="server" form
This code
$('#button2').trigger('click');
should work.
Have you checked that the ID 'button2' is not changed during runtime?
Sometimes, when using master pages or user controls, the client ID of an element with runat="server" attribute will get a prefix at run time.
Use a debug console (depends on your browser, try pressing F12) to check the effective client ID.
Also, have you checked that button1_click() function is actually called?