Call Web Reference using ASPX page - asp.net

In visual Studio I've added a WSDL web reference which loads a variety of "methods" for calling a SOAP api. I have a blank aspx page. How do I call these methods loaded by the web reference? I've called the web reference mywsdl.
Here's my code so far:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<%# Import Namespace="WebApplication1" %>
<%
End Sub
Private Sub Call_Web_Service_Method()
Dim output
Dim CallWebService As New ServerName.mywsdl()
Dim sGetValue As String = CallWebService.GetSomeValue()
output = sGetValue
End Sub
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=output%>
</div>
</form>
</body>
</html>

Add the web service as a reference at the top of your code behind web page, instantiate a new instance of it where you need it and use it. The methods should already be exposed as you've successfully imported the .asmx I believe, however if you're not getting data back it might require authentication.
using mywsdl;
mywsdl my = new mywsdl();
or
Imports mywsdl
Dim my = new mywsdl()

Related

Using server tags inside of a local ASP.NET page contained in an iframe

I have a class that modifies the path of a resource (e.g., a stylesheet) based on that file's last modified time. This is in order to prevent caching of stale resources.
The class works on all "normal" pages, but I can't get it to work in a local ASP.NET page that's rendered inside of an iframe. The class is located in ~/App_Code:
fingerprint.aspx.cs
public class Fingerprint {
public static string Tag(string rootRelativePath) {
if (HttpRuntime.Cache[rootRelativePath] == null) {
string absolute = HostingEnvironment.MapPath(rootRelativePath);
DateTime date = File.GetLastWriteTime(absolute);
int index = rootRelativePath.LastIndexOf('/');
string result = rootRelativePath.Insert(index, "/v-" + date.Ticks);
HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute));
}
return HttpRuntime.Cache[rootRelativePath] as string;
}
}
And in under most circumstances, it works successfully if called as follows:
somepage.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MainSite" CodeFile="somepage.aspx.cs" %>
<html>
<head runat="server">
<title>Title</title>
<link rel="stylesheet" href="<%# Fingerprint.Tag("/path/to/resource.css") %>">
<!-- other markup -->
</head>
<body>
<!-- other markup -->
</body>
</html>
But if I have an ASP.NET page that loads another ASP.NET page inside an iframe, it doesn't seem to get called. Example:
parent.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MainSite" CodeFile="parent.aspx.cs" %>
<asp:Panel ID="Container" runat="server">
<iframe id="testFrame" src="child.aspx"></iframe>
</asp:Panel>
child.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MainSite" CodeFile="child.aspx.cs" %>
<html>
<head runat="server">
<title>Title</title>
<link rel="stylesheet" href="<%# Fingerprint.Tag("/path/to/resource.css") %>">
<!-- other markup -->
</head>
<body>
<!-- other markup -->
</body>
</html>
I don't get any errors, but Fingerprint.Tag doesn't seem to even be getting called. I just get an empty string in the href portion of the <link> tag where the path to my stylesheet would normally be.
Is there a trick to getting server tags to work inside of local ASP.NET pages contained in iframes? I'm using ASP.NET WebForms (no other choice, client requirement).
What about changing <%# Fingerprint.Tag("/path/to/resource.css") %> to <%=Fingerprint.Tag("/path/to/resource.css") %>? Changing # to =.
As far as I know, <%# ... %> is more like a data binding syntax.

Referencing a codebehind function from an ASPX page with a Namespace

I have a very unusual problem and would appreciate help. I have a web application in VS 2019.
I am trying to create a page that runs in its own namespace so I can share it with other websites. Below is a simple sample page and code.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="test1.aspx.vb" Inherits="xxxx.test1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= Myfunc1() %>
</div>
</form>
</body>
</html>
Namespace xxxx
Public Class test1
Inherits System.Web.UI.Page
Public Function Myfunc1() As String
Myfunc1 = "Hello"
End Function
End Class
End Namespace
The compilation shows <%= Myfunc1() %> as MyFunc1 not found.
If I remove the "Namespace xxxx" from the code behind and change the Inherits="xxxx.test1" to Inherits "TestWebsite.test1" (TestWebsite is the namespace for the web project and the default setting when you add the test page). This code compiles and works OK.
I would appreciate advice on how I can achive my aim of making the namespace for the page independent of the website project.
Thanks
Paul

Why is this clearly visible symbol unresolvable?

I'm getting an ASP Error, according to Resharper, "Solution MembersOrderEntry.sln
Project MembersOrderEntry
MembersOrderEntry\segovias-abq\Default.aspx:1 Cannot resolve symbol 'segovias-abq'"
In the same Default.aspx file, there is a similar error, "Solution MembersOrderEntry.sln
Project MembersOrderEntry
MembersOrderEntry\segovias-abq\Default.aspx:1 Cannot resolve symbol '_Default'"
Why are these symbols ('segovias-abq' and '_Default') unresolvable? The project contains many files named Default.aspx, with Default.aspx.vb beneath them; most of them throw no errors. But after letting Resharper "fix" funky namespace (where it thought they were wrong), I'm getting four of these errors.
Why are the lion's share of the Default.aspx files able to resolve their parent file, but this one is not? As you can see here, the relationship between the failing one and others are the same (a folder name, with Default.aspx beneath that, and Default.aspx.vb beneath that):
Seashore, SimonLeeman, Stern, Weyand, and many others all work fine.
The entire contents of the failing Default.aspx is:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="segovias-abq._Default" title="Web Order Entry" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Order Entry Login Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
...and the Default.aspx.vb for it is:
Partial Class _Default
Inherits Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Session("SelectedMenu") = "Home"
Response.Redirect("../Login.aspx?MemberNo=B1212")
End Sub
End Class
OTOH, the entire contents of one of the non-failing Default.aspx is:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Weyand._Default" title="Web Order Entry" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Order Entry Login Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
...and its Default.aspx.vb is:
Namespace Weyand
Partial Class _Default
Inherits Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Session("SelectedMenu") = "Home"
Response.Redirect("../Login.aspx?MemberNo=031")
End Sub
End Class
End NameSpace
IOW, except for the namespaces, they seem to be identical. But even when I added what would seem to be the one expected:
Namespace segovias-abq
...it changed nothing; I still get, "Cannot resolve symbol 'segovias-abq'"
ReSharper removes hyphens or underscores from namespaces when making the namespace names match their directory paths. During a refactoring, ReSharper will not look at string references unless you allow it.
I believe when you refactored, the hyphen was removed form the segovias-abq namespace but that namespace change does not appear to have been corrected in this portion of Default.aspx:
Inherits="segovias-abq._Default"

Simple ASP.NET with VB.NET - Could not load type

I am a c# guy but I need to get a small vb.net test harness working. For the life of me I can't get this to work, and can't discover why. Here's the aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Source Page</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
And here is the .vb:
Public Class _Default
Inherits System.Web.UI.Page
End Class
Always the error "Could not load type '_Default'." I even turned the folder into a true Application in IIS but it made no difference. What am I missing?
What am I missing?
Try the below two -
If your code-behind contains a namespace then modify your inherits attribute in Page directive as Inherits="namespace._Default".
If it doesn't, simply remove this Inherits attribute from Page Directive.
Changing the Codebehind attributename to CodeFile worked for me:
vb
CodeFile="Default.aspx.vb"
C#
CodeFile="Default.aspx.cs"

How do you access user controls on a masterpage from the asp:content page using the master?

I keep getting these requests for 'make me a tool to do xyz' for a web app we're putting up.
So after the third one, I realized it'd be easier to lump them all together and use a master page.
I've got a user control called MessageCenter I use for error, success, and informational messages, and so I dropped that on the master page.
<%# Master Language="VB" CodeFile="tfMasterPage.master.vb" Inherits="tfMasterPage" %>
<%# Register Src="MessageCenter/msgCenter.ascx" TagName="msgCenter" TagPrefix="uc1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>INSERT TITLE HERE</title>
<link href="Stylesheets/EogTool.css" rel="stylesheet" type="text/css" />
<link href="stylesheets/TF_Main_Styles.css" rel="stylesheet" type="text/css" />
<link href="stylesheets/TF_Print_Styles.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body style="background-color: #eeeeee">
<form id="form1" runat="server">
<div class="page">
<div class="headerArea">
<div class="LogoImg">
<img alt="Transparency Florida" src="images/TF_Logo.jpg" /></div>
<div class="SealImg">
<img alt="Shining the Light on Florida's Budget" src="images/TF_Seal.jpg" /></div>
</div>
<div class="content">
<h1>
FIS - EOG Table Maintenance</h1>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="content">
<div>
<uc1:msgCenter ID="MsgCenter1" runat="server" />
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
...
Normally, when the msgcenter is on a regular aspx page, I call its method and stuff from the codebehind as in this sub:
...
rtn = dal.deleteRow(CInt(e.CommandArgument), currentTab())
If Not IsNumeric(rtn) Then
MsgCenter1.addMessage("An Error occured deletion" & rtn, , , , "E")
Else
MsgCenter1.addMessage("Delete Successful", , , , "S")
End If
bindGrid()
MsgCenter1.Visible = True
End Sub
But when I try to do that from the asp:content thing on the page using the masterpage, it tells me that msgCenter1 is not declared. It's some sort of scope issue.
I've read about using findcontrol like
ctype(master.findcontrol("tbWhatever"), textbox).text = "FOO"
But when I try to cast to my user control, it complains because it once again, isn't declared.
I feel as though I'm just missing one piece of the puzzle, but it's been eluding me since around 4PM yesterday.
Any advice, pointers, or links would be most appreciated.
Thanks.
First add this directive to the content page you want to access the master page
<%# MasterType VirtualPath="~/NameOfMasterPage.master"%>
Second, On the master page setup a public propery that returns the control you want to access
public Label MasterLabel
{
get
{
return lblMaster;
}
private set
{
//do nothing
}
}
Lastly just access the control in the content page like so
Master.MasterLabel.Text = "Hello from the content page!";
I know your question has been answered and this doesn't apply to it, but I noticed you're passing in 1 length characters for your "MessageCenter" control. I would use an Enum instead of a string to make your code a little less brittle. As it stands now you can pass "fart" in as a parameter and it will compile just fine. An Enum will give you some compile time checking and avoid any issues at runtime.
Examples:
Message.Success
Message.Error
Message.Warning
this is what i'd been used.
Master.FindControl("ControlID").Visible = false;

Resources