Call aspx event handler with http request - c#

Consider the following code from behind an aspx page:
protected void onBtnClick(object sender, EventArgs e)
{
}
Is it possible to create/craft a POST request (HTTPWebRequest/HttpClient) to call the event handler behind of the aspx page ?
Thanks

Out of curiosity I was able to get this to work, but it takes specific changes to your .aspx page that you probably do not want to do (or cant do): <%# Page EnableEventValidation="false" also have to set your ClientIDMode to static
Given this Test.aspx webform markup:
<form id="form1" runat="server">
<asp:Button runat="server" OnClick="OnClick" Text="Click Me" ClientIDMode="Static" ID="Button1" />
</form>
And this codebehind:
protected void OnClick(object sender, EventArgs e)
{
Response.Write("clicked");
}
I was able to post to Test.aspx from an arbitrary html page (same site) and have OnClick fire, also Page.IsPostBack was set to true:
<form method="post" action="Test.aspx">
<input type="hidden" name="Button1" value="Click+Me" />
<input type="hidden" name="__VIEWSTATEGENERATOR" value="" />
<input type="hidden" name="__VIEWSTATE" value="" />
<input type="submit" value="submit" />
</form>
Again, this is not recommended, probably opens Test.aspx up to all kinds of nasty hacks.

Related

Codebehind does not create button click event

I have created a ASP.NET Empty Website in a new install of Visual Studio 2017 Community. I am attempting to replicate the steps shown by the following tutorial: https://youtu.be/5dCAXwhjIYU
I'm simply planting three text boxes on the form, along with a submit button. When i double click the button, The IDE simply highlights the HTML for the button in the source view of the page. It does not create the event handler in the code-behind as shown in the video and as expected.
I manually created the event handler for the button click in the code behind. When I try to run the code, it is telling me that the fields I'm referencing do not exist:
Error CS0103 The name 'UserName' does not exist in the current context
Here's my HTML code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Users.aspx.cs" Inherits="Users" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
UserName<br />
<input id="UserName" type="text" /><br />
UserEmail<br />
<input id="UserEmail" type="text" /><br />
UserCampus<br />
<input id="UserCampus" type="text" /><br />
<input id="SaveUser" type="submit" value="submit" /></div>
</form>
</body>
</html>
The C# code:
public partial class Users : System.Web.UI.Page
{
protected void SaveUser_Click(object sender, EventArgs e)
{
SqlConnection BIGateConnection = new SqlConnection("Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=True");
{
SqlCommand BIGateSQLCmd = new SqlCommand("Insert into [BIGateway].[dbo].[User] (UserName, userEmail, userCampus) VALUES (#userName, #userEmail, #userCampus)", BIGateConnection);
BIGateSQLCmd.Parameters.AddWithValue("#", UserName.Text);
BIGateSQLCmd.Parameters.AddWithValue("#", UserEmail.Text);
BIGateSQLCmd.Parameters.AddWithValue("#", UserCampus.Text);
}
}
}
What the heck am I doing wrong?
if you have taken HTML text box then code is as follows:
<input id="UserName" type="text" /><br />
In this you will have to add the line on your own as runat="server"
It will look as follows:
<input id="UserName" type="text" runat="server"/><br />
Then you can use it for serverside.
Hope its helpful.
Video doesn't show the ASPX page. I believe he used TextBox and Button server controls.
<form id="form1" runat="server">
<div>
UserName<br />
<asp:TextBox runat="server" ID="UserName"/><br />
UserEmail<br />
<asp:TextBox runat="server" ID="UserEmail"/><br />
UserCampus<br />
<asp:TextBox runat="server" ID="UserCampus"/><br />
<asp:Button runat="server" ID="SaveUser" OnClick="SaveUser_Click" Text="Submit"/>
</div>
</form>
If he used regular html input with runat="server", he will have to access the value as UserName.Value inside Button1_Click event which he did not.
Afais you didn't find the click event to the button.
Add
SaveUser.OnClick += SaveUser_Click;
To the page_load event.
In general I would prefer asp:Button instead of input.
In fact it's rendered as a input control. But with more options. But indeed runat="server" is missing for the input.

Getting value of Html textbox when html Button Clicked

I am trying to get html textbox value when click on html button without using "runat" attribute.I need to do it in code behind ,is it possible?
<button id="button1" onclick="btnclick">Click Here</button><input type="text" id="txtBox1" name="txtBox12" />
and My code Behind is like:
protected void btnclick(object sender, EventArgs e)
{
string name = Request.Form["txtBox12"];
Response.Write(name);
}
MODIFIED
To access any control in code behind it needs to have runat=server attribute attached to it.
In your case, like you pointed you can transfer the value of input text to asp hidden field and then access
its value on button click in server-side but in this case too you have to place hidden field inside form runat=server tag.
<form id="form1" runat="server">
<input type="text" id="txt" onkeyup="PassValue(this);";/>
<asp:HiddenField ID="hf" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
</form>
-----------------------
<script type="text/javascript">
function PassValue(obj) {
document.getElementById("hf").value = obj.value;
}
</script>

Processing HTML form with c#

I am a newbie to C# development. I created an HTML form, and I want to run its input using C#.
My HTML form right now:
<form action="default.aspx.cs" enctype="multipart/form-data" method="post">
<input type="text" name="first_name" id="fname"
placeholder="e.g. Jane Doe" required maxlength="20" >
<p>
Please specify a file, or a set of files:<br>
<input type="file" name="datafile" size="40">
</p>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<!--<input type="submit" value="Submit"> -->
</div>
</form>
My Default.aspx.cs file contains:
protected void Button1_Click(object sender, EventArgs e)
{
}
I want to pass the contents from the html form to that Button1_Click function. The name input is there as a dummy field, my actual job is to get that file.
You can access the values through the Request.Form object like so;
Request.Form["fname"]
Or you can do it the more correct way, by changing the <input> tags to be <asp:TextBox> tags, you can then access them by name eg. fname.Text

prevent Asp.NET button from submitting form

So, I have a Paypal form that worked wonders. However, I now need to add a coupon field, where someone can enter a code, and get a reduction based on whatever the backend replies.
This all works wonderfully, but I've ran into an issue when adding the option to know before checking out whether your code is valid or not. Currently, my form (once simplified) looks like this :
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">
<asp:TextBox runat="server" ID="txtCode" />
<asp:Button Text="Validate" OnClick="ValidateDiscount" runat="server" />
<asp:Label ID="txtDesc" runat="server" />
<input type="submit" name="Submit" value="Pay up!" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
...
</form>
With the backend having the function :
protected void ValidateDiscount(object sender, EventArgs e)
{
this.txtDesc.Text = "fetch from database using: " + txtCode.Text;
}
My issue is that wheneve I click on the Validate button, the form is submitted and I end up on the Paypal website. I used Jquery at first with preventDefault(), but that actually prevents my server-side function from firing. I've also tried putting a standard <button> or <input type='button'> tag instead, but I couldn't get it to fire my server-side function.
Is there any way to have the Validate button not submit the form, or should I just remove the action from the form and manually submit the form when clicking on the submit button?
You have set your form action to post to PayPal.
This is the action:
action="https://www.sandbox.paypal.com/cgi-bin/webscr"
Here is where you have it in you form tag:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">
Remove this from your form tag and it should postback to your application.

File Upload Issue - how to fix?

I'm trying to upload file in tow scenarios
First:
<input id="File2" runat="server" name="name" type="file" clientidmode="Static" />
<asp:Button ID="Button4" runat="server" clientidmode="Static"
Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
This works correctly and the postedFile not null in code behind C#
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
Second:
I want to change the browse button "text" and I know already , can't do that directly so i worked around it like that:
<b>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" >
<input id="File1" runat="server" name="name" type="file" clientidmode="Static" onchange="setHiddenValue()" style=" visibility:hidden;" />
<br />
<input id="Button2" type="button" clientidmode="Static" onclick="triggerFileUpload()" value="HTML Button" />
<br />
<asp:Button ID="Button3" runat="server" clientidmode="Static" Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
<script language="javascript">
function triggerFileUpload() {
document.getElementById("File1").click();
}
</script>
</asp:Content>
<b>
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
When I press the Button2 the fileDialog open, I select file and everything Ok.
But when I press Button3 to get file in server side c# the Request.Files[0] is null
and Found no file posted.
I want the Request.Files[0] because I want save it in database as byte
so please if u have any idea I'll appreciate it
Thank You in advance
I did the same code (second scenario) in a blank project, and this works fine. You could do the same and check it.
Probably something around this slice of code is breaking this. Check if masterpage or current page Page_Load() method have something that break the file 'postback'. You can try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Page_Load code
}
}

Categories

Resources