简体   繁体   中英

call a method in asp.net c# compared to vb.net

I am trying to pass from vb.net to c#. In vb.net,on the click event of a button, present in an aspx page, I want to call a subroutine. Let's suppose this subroutine modify the text of a Label also present in the aspx page. No problem to do this in vb.net, but how can I do the same in c#? Label does not seem accessible if I call a method by the click event of the button.

Protected Sub btn1_Click(sender As Object, e As System.EventArgs) Handles btn1.Click
    pippo()
End Sub

Sub pippo()
    lbl1.Text = "text modified pressing the button"
End Sub

In your aspx file, ensure that AutoEventWireup is set to true

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

And then this code would work as usual

protected void btn1_Click(object sender, System.EventArgs e)
{
    pippo();
}

public void pippo()
{
    lbl1.Text = "text modified pressing the button";
}

This will let ASP.NET to automatically associate events with controls. Otherwise you will need to associate it yourself via code.

Well, I think we are confusing things somewhat here.

If a sub/function is in/on the same page for code behind, then of course all server controls can be freely used in code. But this ONLY works becuase all code is "inside" that given page class.

But try calling any external vb sub, and you see that you can't reference anything on that web page. I sure for example you have say a code module in which you place your general code (code that does not belong to or in a page).

So, do this: right click on your project, add->new item->module

在此处输入图像描述

So now we added a standard code module. Now, move your sub to this routine like this:

(just cut and paste it inside)

Module Module3

    Sub pippo()
        lbl1.Text = "text modified pressing the button"
    End Sub

End Module

You note now, that lbl1 does NOT exist in that routine, your code can't compile, and you can't reference lbl1 in that code.

So how you call + use c# code is the above SAME issue.

Now, what we could do of course is PASS the label value to the external routine. So you would now have this:

Protected Sub btn1_Click(sender As Object, e As System.EventArgs) Handles btn1.Click
   pippo(lbl1)
End Sub

And in our code module, we would have this:

Module Module3

    Sub pippo(MyLabel As Label)

        MyLabel.Text = "text modified pressing the button"

    End Sub

End Module

So, the above works because we pass the "thing" in question. And this is what you have to do to call c# code - you have to pass that information since external subs don't have all the page information.

Now, I suppose you could pass the whole web page, say like this:

Sub ChangeThingsOnPage(MyPage As System.Web.UI.Page)

    Dim MyLabel As Label

    MyLabel = MyPage.FindControl("Label1")

    MyLabel.Text = "This will change the label"

End Sub

So any external vb code to that web page, or any external c# code to that page?

You have to pass the information. So your example not really fair, since the sub example you have is inside of the page. You have to pass the controls, values or whatever to external code routines from that page, and all c# code you would use in this case would in fact be external routines - routines that don't know about what's on that web page, and thus needs that information.

So you can put as many general purpose subs/functions into that code module. (just like we always done in vb).

However, code in that module has no idea as to what web page you are on, and thus you cannot modify controls on any old a web page. (you could have 5 web pages open - which web page is the sub to know it belongs to?

The above is a good example, since if you figure out a way to make the sub work in that vb code module? Then you can do 100% the same in c#. But you will be building a non web application for that c# code you call. You simply be building a "class project" which amounts to a externa .net assembly that you can then reference in your current project.

Remember, you can't just inter-mix vb and c# code in the SAME code module. So to introduce say some c# ito our project (or the reverse)? Then you have to create a stand alone vb (or c#) project. In most cases that project would be aa class, like this:

在此处输入图像描述

Now, that above class can be used in OTHER vb.net projects - including web ones, or can be used by c# projects. But you WILL be creating 100% separate stand alone projects when you want to share vb.net and c# code. you can NOT HAVE vb.net and c# SOURCE code in the same project - they have to be separate. Ironic, you can do this with web pages, since they can be setup to compile on the fly if you create a web project as opposed to a web application. But again in that case, only code on that page can use + reference controls - any external call will require passing of the information to the "stand alone" code routines.

But, for the most part? So to mix c# and vb.net?

You create a stand alone c# project. it would ONLY have properties and methods (functions, or functions and subs if vb - it would NOT be a web project.

The result is what we call a .net assembly. You can then add that "library" to your existing project, and then call/use subs/functions in that external library.

Now I undestand the reason of the problem I have; I was trying to translate an aspx page with vbnet code behind in another aspx page with c# code behind. In the aspx page with vbnet code behind all the labels controls were visible in all modules of the page. In the aspx page with c# code behind all the labels controls were visible only in load event, button click event, but not in additional methods defined by me. In both aspx pages there was a reference to a masterpage written in vb.net. I have deleted the reference to the masterpage in the page aspx with c# behind and now in any c# method all the labels controls are available. Thank you to everybody for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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