aspx Request.Form.Count=0 - asp.net

I am not getting any data when submitting a form. I would expect Request.Form.Count=2, but instead RequestForm.Count=0.
Any assistance would be appreciated.
<%# Page Language="VB" EnableViewState="false" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="ecomm_Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1" method="post">
<input id="textfield" type="text" value="My Text" />
<input type="submit" value="Send it" />
</form>
</body>
</html>
Code behind part
Imports System.Diagnostics
Partial Class ecomm_Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Request.HttpMethod = "POST" Then
Debug.WriteLine(Request.Form.Count)
End If
End Sub
End Class

You input fields are missing names. Make sure you add them:
<input id="textfield" name="foo" type="text" value="My Text" />
<input type="submit" name="bar" value="Send it" />
Now on the server you will get both foo and bar keys with their respective values.

Related

asp.net difference between accessing as local machine or remote client

We developp an web application in asp.net Framework 4.7, and we have a difference between accessing the application from the local computer (localhost, or direct name make no difference) and from a remote computer.
The form action is not show with the same information. From the local machine, i get this :
<form name="aspnetForm" id="aspnetForm" action="/MediOnlineNet/(S(id55ddmoixefhknices1zwj0))/DefaultPopup2011.aspx?ppname=_MOPPAG_undefined&Mod=1&0&_MOPPAG_undefined&PPID=254&caller=6&agndPatientId=0&agndContactInformationId=0&numAgenda=5081&numResponsable=10746&typeContact=0&canSeePrive=1&ctrlID=ctl00_CPH_ctl00_Uc_agnd_ctactAff_011&contactName=&contactVorName=&contactDate=&contactNoPatient=" method="post">
and from an remote computer, I get this :
<form name="aspnetForm" method="post"
action="/MediOnlineNet/(S(yevu1jhfwkrqc3wsx4ag3cot))/DefaultPopup2011.aspx?PPID=258&ppname=_MOPPAG_undefined&Mod=1&isModal=0" id="aspnetForm">
And I don't understand why I don't get the same information in each execution.
Any help on this problem ?
Thank you and regards
Philippe RITTER
This is a sample wich show our problem with Request.Url.PathAndQuery
Run this with Default.aspx?tutu
When you click on the second button, on a server, you will get :
trans
/Test/Default.aspx?trans
But if you run it on a dev machine, you will get :
trans
/Test/Default.aspx?tutu
Which in this case, is a false response
Default.aspx :
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:button runat="server" text="?trans Preserve:TRUE" OnClick="Unnamed4_Click" style="height: 26px" />
<asp:button runat="server" text="?trans Preserve:FALSE" OnClick="Unnamed3_Click" style="height: 26px" />
<asp:button runat="server" text="Preserve:TRUE" OnClick="Unnamed1_Click" style="height: 26px" />
<asp:button runat="server" text="Preserve:FALSE" OnClick="Unnamed2_Click" />
</div>
</form>
</body>
</html>
Default.aspx.vb :
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Response.Write(Request.QueryString)
End If
End Sub
Protected Sub Unnamed1_Click(sender As Object, e As EventArgs)
Server.Transfer("Default2.aspx", True)
End Sub
Protected Sub Unnamed2_Click(sender As Object, e As EventArgs)
Server.Transfer("Default2.aspx", False)
End Sub
Protected Sub Unnamed4_Click(sender As Object, e As EventArgs)
Server.Transfer("Default2.aspx?trans", True)
End Sub
Protected Sub Unnamed3_Click(sender As Object, e As EventArgs)
Server.Transfer("Default2.aspx?trans", False)
End Sub
End Class
Default2.aspx :
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Default2.aspx.vb :
Partial Class Default2
Inherits System.Web.UI.Page
Private Sub Default2_Load(sender As Object, e As EventArgs) Handles Me.Load
Response.Write(Request.QueryString)
Response.Write("<br>")
Response.Write(Request.Url.PathAndQuery)
End Sub
End Class

Set or get html tags value or innerhtml in razor view

how to set or get html elements inner text or html controls value in Razor syntax.
i know how we can do this in aspx file using Runat="server" attribute like this
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%Response.Write(Page.Title); %></title>
<script runat="server">
protected void Button1_ServerClick(object sender, EventArgs e)
{
p1.InnerText = "Hello " + textbox1.Value; //get textbox value and set in html p tag
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="textbox1" runat="server" /><br />
<input type="button" id="btn" runat="server" value="Click Me" onserverclick="Button1_ServerClick" />
<p id="p1" runat="server">see result here</p>
</div>
</form>
</body>
</html>
Basically something like this...
View (SomeAction.cshtml):
...
<p>ViewBag.SomeValue</p>
...
Controller:
...
[HttpGet]
public ActionResult SomeAction()
{
ViewBag.SomeValue = "Hello";
return View();
}
...
However you should learn much about approach which is used to build applications using ASP.MVC. It's completely different than ASP.NET WebForms. WebForms technology uses events while ASP.MVC uses Model View Controller. You have to change your mindset.

OnSelectedIndexChanged Not working

I'm not sure what I'm doing wrong here. I'm try to get an OnSelectedIndexChanged event working, but I'm trying to do it without using the asp form controls.
In the below example the OnServerClick works for the <a> element but neither the OnSelectedIndexChanged nor OnServerClick seem to work for the <select>.
<%# Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html>
<html>
<head>
<script runat="server">
Sub HtmlAnchor_Click_1(sender As Object, e As EventArgs)
Message.InnerHtml = "this doesn't work"
End Sub
Sub HtmlAnchor_Click_2(sender As Object, e As EventArgs)
Message.InnerHtml = "this works"
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<select id="AnchorSelect" name="select1" OnSelectedIndexChanged="HtmlAnchor_Click_1" runat="server">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br /><br />
<a id="AnchorButton" onserverclick="HtmlAnchor_Click_2" runat="server">Click Here</a>
<br /><br />
<span id="Message" runat="server"/>
</form>
</body>
</html>
Any ideas, or solutions would be appreciated.
Cheers.
select is an HTML input and the OnSelectedIndexChanged would be a Javascript function that gets called.
Use <asp:DropDownList> and set autopostback=true. Then, you would put the OnSelectedIndexChanged in your codebehind to use it.
Check out this example: DropDownList's SelectedIndexChanged event not firing

Retrieving image uploaded from ASP.NET page

I've got an ASP.NET upload form on one page, where the user can upload an image. heres the code for that one:
<%# Import Namespace="System" %>
<%# Import Namespace="System.IO" %>
<html>
<head>
<script language="VB" runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
If imageupload1.HasFile Then
imageupload1.SaveAs(Server.MapPath(".") + "/uploadedimages/" & imageupload1.FileName)
Label1.Text = "Received " & imageupload1.FileName & " Content Type " & imageupload1.PostedFile.ContentType & " Length " & imageupload1.PostedFile.ContentLength
Else
Label1.Text = "No uploaded file"
End If
end sub
</script>
</head>
<body>
<form id="imguplad" runat=server>
<asp:FileUpLoad id="imageupload1" AlternateText="You cannot upload files" runat="server" />
<asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server" />
<asp:Label id="Label1" runat="server" />
<input type="button" value="Click here when image is uploaded"
onClick="location.href='imageloadtest.aspx';">
</form>
</body>
</html>
Then I am trying to retrieve it on another page, using the following code:
<%# Page Language="C#" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.IO" %>
...blah
<body>
<img id="image" src="Server.MapPath(".")+"/uploadedimages/"+Request.Form["imageupload1.FileName"]";">
</body>
...blah
Can't seem to get it to show up? I must have some path slightly wrong.
EDIT: By the way, the image uploads perfectly, it just won't show up on the next page.
Try with
<img id="image" src="<%=Server.MapPath("~")%>/uploadedimages/<%=Request.Form["imageupload1.FileName"]%>">
You also may have to set manually the image name in session because you won't have access to imageupload1.FileName in another page, where the control imageupload1 doesn't exist.
So in your click event add Session["ImagePath"] = imageupload1.FileName
And the img tag replace Request.Form["imageupload1.FileName"] by Session["ImagePath"]

Need to understand why upload isn't happening in this case?

I've an html file with the below markup:
<!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>
<title>Upload Page</title>
</head>
<body>
<form id="frmUpload" action="UploadHandler.ashx" method="post" enctype="multipart/form-data">
<input type="file" /><br />
<br />
<input id="Submit1" type="submit" value="Submit" />
</form>
</body>
</html>
I have a handler (ashx) file to handle the upload which goes like this:
<%# WebHandler Language="VB" Class="UploadHandler" %>
Imports System
Imports System.Web
Imports System.Diagnostics
Public Class UploadHandler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim str As String
str = "EncType = " & context.Request.ContentType
str &= vbCrLf
str &= "File Count = " & context.Request.Files.Count
context.Response.ContentType = "text/plain"
context.Response.Write(str)
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
When I am working with the html page, I select a file and do a submit, I get a response like this:
EncType = multipart/form-data; boundary=---------------------------7d9232a130656
File Count = 0
I was expecting the file count to be 1 here but it is 0...what is wrong?
Try giving a name to your input tag:
<input type="file" name="fileToUpload" />
You don't have a name attribute on your file <input>:
<input type="file" name="myFile"/><br />

Resources