i have 2 aspx files . I need to access a public function in b.aspx from a.aspx. How can i do that in asp.net using C#
My function from a.aspx is the following:
<script language="C#" runat="server">
public String user()
{
return l1.Text;
开发者_开发百科 }
</script>
Ideally, don't. Don't put common logic in aspx files - put it in other classes which both pages can access. Code in a page should only be about displaying that page.
Don't forget that when you're displaying b.aspx
, there is no instance of page a
, logically. You could create a new one, but if you're trying to get the value of a label which was displayed previously to a user, or something like that, this could go badly wrong.
@Sir Jon says,Create a new Class for Properites
public class UserEntity()
{
public UserEntity(string user)
{
User = user;
}
public UserEntity()
{
}
public string User { get;set;}
}
then use and pass it on your pages:
var ent = new UserEntity();
ent.User = li.Text
Regards
While that's not an everyday task, it's still possible.
You could use cross-page posting, and then access the properties of the first page. The MSDN article Cross-Page Posting in ASP.NET Web Pages describes it.
精彩评论