简体   繁体   English

在 ASP.net 中的页面加载时从代码后面调用代码?

[英]Call code from code behind on page load in ASP.net?

I am simly trying to call the code from a code behind immediateky, as soon as the page loads.我只是想在页面加载后立即从 immediateky 后面的代码中调用代码。

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 :文件名是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:这是.aspx文件的相关部分:

<%@ 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.以上是我尝试单击按钮/链接以调用背后代码的 4 种不同方式。 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或错误: 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();我使用 <%#CSCodeName() %> 这种格式来获取后面的代码,有时我必须添加一个像这样的分号 <%#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?如果没有这个信息,那么页面加载后,我们点击那个隐藏的按钮,然后页面会再次加载,页面加载后,代码会一次又一次地点击按钮,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM