asp.net output from a SOAP page location using response.write - asp.net

I hope someone can help. I created a webservice and it returns XML via SOAP fine and well. It transforms fine with XSLT and I return the HTML to a ASP VB.net webform.
I call the functions in the code behind with a button.
Everything works great except the transformed output always ends up at top of the page. I return the result to a label, put the label in a different contentplaceholder but it always is on top. I want the input items(text box) and button to float at the top.
Here is the very simple main webform - plus a bit from where the results are coming from.
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.vb" Inherits="WS_NewCar.WebForm1" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<p>
<img src="images/barner.jpg" style="width: 508px; height: 198px; margin-left: 280px" /></p>
<p>
</p>
<p>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
</p>
<p>
<br />
Click here for SOAP request. <asp:Button ID="btnConvert" runat="server" Text="Search" />
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="lblMake" runat="server" Text="Please enter vehicle make"></asp:Label>
</asp:Content>
Dim sr As New StreamReader(memoryStream)
response.Write(sr.ReadToEnd())
sr.Close()
End Function
Protected Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
lblMake.Text = callWS(txtInput.Text)
End Sub

Response.Write(sr.ReadToEnd()) will always render at the top of the page. It is called before the page is rendered. Try changing it by adding a label where you want the output to appear and do the following in your page_load
label1.Text = sr.ReadToEnd()

Related

Find Label Control Inside ContentBody

I have a label control on a page (test1.aspx) that has a master page. I want to access a label control from the code behind of the test1.aspx page (not the master page) using FindControl.
I can make it work going directly using Button2 (see below) but I want to be able to do it using FindControl (see Button1 below). I'm getting a NULLReference exception I believe because I don't know the proper syntax to target the label control inside a content control.
On a page without a Master page I would just use FindControl("MenuItemName1") but because the control is in the Content3 / ContentBody I believe I need more. Any help would be greatly appreciated.
<%# Page Title="" Language="VB" MasterPageFile="~/Shared/MasterPages/SiteLayout.Master" AutoEventWireup="false" CodeFile="Test1.aspx.vb" Inherits="Test1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="StyleSheetPage" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentBody" Runat="Server">
<asp:Label ID="lblMenuItemName1" runat="server" Text="Label"></asp:Label><br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" /><br /><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="JavaScriptPage" Runat="Server">
</asp:Content>
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim C As Content
C = CType(Me.FindControl("Content3"), Content)
Dim lblMenuItemName As Label = C.FindControl("lblMenuItemName1")
lblMenuItemName.Text = "hello"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lblMenuItemName1.Text = "Direct"
End Sub
Found the solution:
Dim lblMenuItemName As Label = TryCast(Master.FindControl("ContentBody").FindControl("lblMenuItemName1"), Label)

how to access textbox value to code behind using asp.net Content Page?

i have a textbox and a button in my aspx file,i want to get the textbox value to code behind while clicking the button,I implemented this before,but now iam using asp:content pages,but now i cant get the value to codebehind
my aspx
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="test1.test" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<body>
<div>
<input type="text" runat="server" id="abc" />
<asp:Button
ID="Button1"
runat="server"
Text="Search"
class="btn"
OnClick="Button1_Click"
/>
</div>
</body>
</asp:Content>
My aspx.cs
protected void Button1_Click(Object sender,
System.EventArgs e)
{
string txtboxvalue = Request.Form["abc"];
}
but i cant get the textbox value in button click.
Since you have already given runat to your input control,
<input type="text" runat="server" id="abc" />
You can access the value of text like this
string txtboxvalue = abc.Value;
//Here no need of using Request.Form since input is now a server control
Your control doesn't have a name, so it doesn't get POSTed in the form that you can read it as Request.Form["abc"]. WebForms will generate its name, something like master00$formName$abc. You can check this in the HTML source, but it may be different per request or change after you add more controls on the page. So you should not use this field when it's autogenerated.
Either give it a name:
<input type="text" runat="server" id="abc" name="abc" />
Or, because it's runat="server" and has an id, you can simply access it from code behind:
string textBoxValue = abc.Value;

Visual Basic subprocedure issue

I'm working on some .NET code in visual studio web developer and I'm having an issue. When I double click on a button in design view, instead of loading a subprocedure, all it does is highlight this: <asp:Button ID="btnEnter" runat="server" Text="Enter" onclick="btnEnter_Click" />
When I try to run that just to see what happens, I get this error:
Line 8: <asp:Button ID="btnEnter" runat="server"
Compiler Error Message: BC30456: 'btnEnter_Click' is not a member of 'ASP.default_aspx'.
If I erase the onclick="btnEnter_Click" and run it, it works. Either way though, when I double click the button / element, shouldn't it create a subprocedure for me that looks something like?
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
I've tried entering that manaully but the keywords don't turn blue or any sort of color, and when I run it, it just shows up as text on the webform with my other elements. Here is all I have so far:
<%# Page Title="Home Page" Language="VB"%>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Enter a person's name below"></asp:Label>
<p>
<asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="btnEnter" runat="server"
Text="Enter" onclick="btnEnter_Click" />
</p>
<p>
<asp:Button ID="btnDisplay" runat="server" Text="Display all and exit" />
</p>
<p>
<asp:Label ID="lbl2" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl3" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl4" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl5" runat="server" Text=" "></asp:Label>
</p>
</form>
Edit:
When I enter my code inside the default.aspx.vb file, it highlights keywords and such, but it can't reference the elements I created in the design view.
Partial Class _Default
Inherits System.Web.UI.Page
End Class
Class Lab1
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
Public Const length As Integer = 3
Shared counter2 As Integer = 0
Public Shared studentList As String() = New String(2) {}
Protected Sub btnEnter_Click(sender As Object, e As EventArgs)
Label1.Text = "Enter a person's name"
Dim studentName As [String] = txtStudentName.Text
If studentList.Length <= length Then
If txtStudentName.Text <> "" Then
Dim match As [Boolean] = True
Dim i As Integer = 0
While counter2 >= i
If studentList(i) IsNot Nothing Then
If studentList(i).ToUpper() = txtStudentName.Text.ToUpper() Then
match = False
Label1.Text = "This name has already been used"
End If
End If
i += 1
End While
If match = True Then
studentList(counter2) = txtStudentName.Text
counter2 += 1
End If
End If
End If
End Sub
End Class
Your Page declaration seems to say you are using inline style:
<%# Page Title="Home Page" Language="VB"%>
Which means your code is/should be within aspx file itself in a script runat="server" tag:
<%# Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
'Do something
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
However, your other code indicates you seem to want to use code-behind model, where you have .vb files (e.g. foo.aspx.vb)
The Page is declared like this for web sites (for web applications it will say CodeBehind="foo.aspx.vb"):
<%# Page Language="VB" AutoEventWireup="false" CodeFile="foo.aspx.vb" Inherits="foo" %>
You will find a foo.asp.vb file in your project
it's class name will be the name of your file (foo)
your code should be scoped to this class (I'm not sure what Class Lab1 in your post above is for....
foo.aspx.vb:
Partial Class foo
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do something
End Sub
End Class
The "fix" is for you to:
choose which model you want to write code: inline or code-behind
declare the Page appropriately
for inline, your code will be in the aspx file itself
for code-behind, your code will live, and should be encapsulated within the Class file of the page.
The Page declaration actually says so:
CodeFile="foo.aspx.vb" is the file where the code is
Inherits="foo" is the Partial Class in the file (Partial Class foo)
Try this
Private Sub btnEnter_Click(sender As Object, e As System.EventArgs) Handles btnEnter.Click
End Sub
Here, btnEnter_Click is your event name and use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable

Intellisense not showing up for some controls in code behind

i'm kind of an asp.net beginner and i'm trying just to set the visible attribute dynamically but i can't access "TestLinkBox" nor the "TestLink" id in the code behind. Ive tried to rebuild the solution, to delete the designer.cs file and let it be recreated and tried a few solutions I found on stackOverflow as you can see in the code behind but i always get the "Object reference not set to an instance of an object." error. What am I doing wrong?
PART OF THE ASP.NET CODE
<%# Page Title="" Language="C#" MasterPageFile="~/Public/Main.Master"
AutoEventWireup="true" CodeBehind="SpecifikacijaDetails.aspx.cs" Inherits="web.Public.SpecifikacijaDetails"
%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<mc:VijestiTraka ID="vijestitraka" runat="server"></mc:VijestiTraka>
<div class="middle_content_box">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="SpecifikacijaID"
DataSourceID="SqlDataSource1">
<AlternatingItemTemplate>
<div class="details_title_box">
<p class="title"><strong><%# Eval("Brand") %></strong>
<%# Eval( "Model") %>
</p>
</div>
<div class="left_column">
<div class="pregled_modela_slika margb30">
<img src='<%# Eval("SlikaMobitela") %>' />
</div>
<div id="TestLinkBox" class="testLink" runat="server" visible="false">
<asp:HyperLink runat="server" ID="TestLink" NavigateUrl='<%#"~/Public/TestSpecifikacije.aspx?SpecifikacijaID=" + Eval("SpecifikacijaID")%>'
Text="Pogledajte kompletan test"></asp:HyperLink>
</div>
CODE BEHIND
using (MobBL temp = new MobBL())
{
if (temp.ProvjeriImalTest(Int32.Parse(Request.QueryString["SpecifikacijaID"])) > 0)
{
//ListView1.FindControl("link_za_test").Visible = true;
//HtmlControl htmlDivControl = (HtmlControl)Page.FindControl("link_za_tet");
//htmlDivControl.Visible = true;
}
}
The controls your are trying to access in code behind are inside an item template. For this reason, you cannot access them via intellisense because they are not actually on the form - they will be added when the parent control (ListView1) is bound.
Subscribe the the item databound event of the listview control. In there, you can access the child controls for each item in the listview. Something like this would get you the control:
e.Item.FindControl("TestLinkBox");

How to use Eval in codebehind to set Page.Title

I have a SQLDataSource that is bound to a ListView control but I want to place parts of the bound record into the HTML TITLE attribute. Here is my codebehind file that I want to change so it can use Eval to construct a dynamic TITLE based on the data content:
Public Partial Class zShowAd
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Page.Title = " Dynamically set in ASPX page"
'how to use Eval here instead of the above constant ??
End Sub
End Class
Here is the corresponding .aspx file:
<%# Page Language="vb" AutoEventWireup="false" MasterPageFile="~/zSEO.master"
CodeBehind="zShowAd.aspx.vb" Inherits="Zipeee.zShowAd" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<asp:ListView ID="ShowAd" runat="server" DataSourceID="aPosting">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<div>
<div id="wrapper">
<div id="header"></div>
<div id="main">
<div id="nav"> AdID: <%#Eval("AdID")%></div>
<div id="extras">Price: <%#Eval("Price")%></div>
<div id="content"> <%#Eval("AdDesc")%></div>
</div>
<div id="footer"></div>
</div>
</div>
</ItemTemplate>
</asp:ListView>
<asp:sqldatasource runat="server" id="aPosting"
ConnectionString="<%$ ConnectionStrings:ZIPeeeConnectionString2 %>"
SelectCommand="spGetAdByID" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="AdId" QueryStringField="a" Type="String" />
</SelectParameters>
</asp:sqldatasource>
</div>
</asp:Content>
You can call a method (Sub) of the page's code behind by putting the following somewhere inside the ItemTemplate of your ListView:
<%# SetPageTitle(Eval("SomeProperty")) %>
Then in your code behind (sorry it's in C#):
protected void SetPageTitle(object title)
{
this.Title = title.ToString();
}
Alternatively, you can also pass the complete data item, instead of just one property:
<%# SetPageTitle(Container.DataItem) %>
Update (to answer your comment):
<%# ... %> is a so-called data-binding expression. It only works inside of a data-bound control (the ListView in your example) and it always works with the current record (typically you display more than one record in a data-bound control like the ListView).
So when you use <%# Eval("Price") %>, you are displaying the value of the current record's "Price" column. If your query, would return more than one record, then this would be executed for each record, and when setting the page title (as shown above), the page's title would be the value from the last record.
On the other hand <%= ... %>, is just a normal server-side code snippet (don't know if there is a specific name for it), which does not know about the data-binding context (e.g. which is the current record).
Please see the following question for more details: When should I use # and = in ASP.NET controls?

Resources