I am trying to implement Sign Up api of Constant Contact. They gave me this HTML code. Runs properly if i save it as a HTML page.
What i want to do is convert this form to C# code that will execute onClick of a button. The HTML portion is looking fine but today is my first day with ASP.net and i have no idea what code to put in the code-behind .cs file. Searched a lot and am super confused.
This method is clicked when the submit button is clicked:
protected void buttonId_Click(object sender, EventArgs e)
{
}
Here is my ui side code:
<asp:TextBox ID="TextBox3" runat="server" class="name" value="First Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'First Name';}"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" class="name" value="Last Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Last Name';}"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" class="name" value="Join our mailing list" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Join our mailing list';}"></asp:TextBox><br>
<asp:Button id="buttonId" OnClick="buttonId_Click" class="btn btn-info sub1" runat="server" Text="SUBSCRIBE">
</asp:Button>
Here is the HTML code that i got from constant contact:
<form data-id="embedded_signup:form" class="ctct-custom-form Form" name="embedded_signup" method="POST" action="https://visitor2.constantcontact.com/api/signup">
<p>Sign up to get interesting news and updates delivered to your inbox.</p>
<!-- The following code must be included to ensure your sign-up form works properly. -->
<input data-id="ca:input" type="hidden" name="ca" value="my-secrect-key">
<input data-id="list:input" type="hidden" name="list" value="3">
<input data-id="source:input" type="hidden" name="source" value="EFD">
<input data-id="required:input" type="hidden" name="required" value="list,email">
<input data-id="url:input" type="hidden" name="url" value="">
<p data-id="Email Address:p" ><input data-id="Email Address:input" type="text" name="email" value="" maxlength="80"></p>
<p data-id="First Name:p" ><input data-id="First Name:input" type="text" name="first_name" value="" maxlength="50"></p>
<p data-id="Last Name:p" ><input data-id="Last Name:input" type="text" name="last_name" value="" maxlength="50"></p>
<button type="submit" class="Button ctct-button Button--block Button-secondary" data-enabled="enabled">Sign Up</button>
</form>
Here's an example of some code you could put in your button OnClick event. This POST's data to another URL.
Hopefully you can figure out what's going on in this code. Basically it is building a string (data) with all of the data from the HTML form and submitting this to the other website using HTTP POST.
Most likely you would also need to check the response back from the other website.
string remoteUrl = "https://visitor2.constantcontact.com/api/signup";
ASCIIEncoding encoding = new ASCIIEncoding();
string data = "ca=my-secrect-key&list=3&source=EFD&required=list,email&url=&email=" + Server.UrlEncode(TextBox1.Text) + "&first_name=" + Server.UrlEncode(TextBox2.Text) + "&last_name=" + Server.UrlEncode(TextBox3.Text);
byte[] bytes = encoding.GetBytes(data);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(remoteUrl);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = bytes.Length;
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
Related
I have a form in my asp.net web app, which sends files uploaded by the user
<form method="post" asp-action="#((Model != null) ? "Update" : "Upload")" asp-controller="Materials" enctype="multipart/form-data">
<input id="cat" type="hidden" name="cat" />
<input id="subcat" type="hidden" name="subcat" />
<input type="file" name="file" max="1" title="#((Model != null) ? "Обновить" : "Загрузить")"/><br />
<input type="file" name="images" multiple title="Загрузить картинки"/><br />
<input id="name" type="text" name="name" maxlength="30" /><br />
<textarea id="desc" name="description" maxlength="199" ></textarea><br/>
<input type="submit" value="Сохранить"/>
</form>
There is one input for a file, and one input for image files, but as soon as I input any files the app just.... shuts down
Here is the method from the controller:
[HttpPost]
public IActionResult Upload(IFormFile file, IFormFileCollection images, string name, string description, string cat, string subcat)
{
FileUploadManager f = new FileUploadManager(environment.WebRootPath + '/');
f.Upload(file,images, name, description, cat + "-" + subcat);
return Redirect(Url.Action("Index", "Material"));
}
the browser does not shut down, the app itself in the VS does, and without any exceptions, it just stops debugger as soon as files contact with HTML
also, I am using Yandex browser
my concern is about post-back. I have 3 textboxes, the last name, first name and username. I want to combine last name and first name to create a username have lastname.firstname format. My code in .aspx is as follows. The post-back is true for first name because after the user enters the first name, it will automatically generate the username.
<strong>LAST NAME: <span style="color: #FF0000">*</span> </strong>
<asp:TextBox ID="txtLname" runat="server"
Width="197px" onkeypress="return Alphabet()" BorderStyle="Solid"></asp:TextBox>
<strong>FIRST NAME: <span style="color: #FF0000">*</span> </strong> <asp:TextBox ID="txtFname" runat="server" Width="199px"
onkeypress="return Alphabet()" AutoPostBack="True"
ontextchanged="txtFname_TextChanged" BorderStyle="Solid"></asp:TextBox>
<strong>USERNAME: </strong> <asp:TextBox
ID="txtUser" runat="server"
Width="225px" ReadOnly="True" style="margin-left: 6px" BorderStyle="Solid" onkeypress="return Alphabet()"></asp:TextBox>
My code in .aspx.cs is as follows.
protected void txtFname_TextChanged(object sender, EventArgs e)
{
string mystring = txtFname.Text;
mystring = Regex.Replace(mystring, #"\s+", "");
txtUser.Text = txtLname.Text + "." + mystring;
}
this is to trigger the automatic generation of username.
To sum it up, I want to know how to stop post-back from going back to the top of the page after entering the first name for automatic generation of username. thank you very much.
Adjust as you need:
<html>
<head></head>
<body>
<form action="">
Last Name :<input id="txtLastName" name="txtLastName" type="text" oninput="updateUserName()"></input><br />
First Name:<input id="txtFirstName" name="txtLastName" type="text" oninput="updateUserName()"></input><br />
Username:<input id="txtUserName" name="txtLastName" type="text" readonly></input><br />
<button type="submit">Submit</button>
</form>
<script>
function updateUserName()
{
var last_name = document.getElementById("txtLastName").value;
var first_name = document.getElementById("txtFirstName").value;
if(last_name !== "" && first_name !== "")
document.getElementById("txtUserName").value = last_name+"."+first_name;
}
</script>
</body>
</html>
I have this FileUpload control that automatically adds a FileUpload Control. But I need to have it also create an RegularExpressionValidator. Not sure how I can do this.
Any ideas?
<script type = "text/javascript">
var counter = 0;
function AddFileUpload() {
var div = document.createElement('DIV');
div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
'" type="file" />' +
'<input id="Button' + counter + '" type="button" ' +
'value="Remove" onclick = "RemoveFileUpload(this)" />';
document.getElementById("FileUploadContainer").appendChild(div);
counter++;
}
function RemoveFileUpload(div) {
document.getElementById("FileUploadContainer").removeChild(div.parentNode);
}
</script>
<div id="FileUploadContainer">
<asp:FileUpload ID="FileUpload1" runat="server" />
<!--FileUpload Controls will be added here -->
</div>
<br />
<input id="Button1" onclick="AddFileUpload()" style="height: 27px; width: 150px;" tabindex="25" type="button" value="Add More Attachments" />
<asp:RegularExpressionValidator ID="rexp" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Only .pdf, .jpg"
ValidationExpression="(.*\.([Pp][Dd][Ff])|.*\.([Jj][Pp][Gg])$)">
</asp:RegularExpressionValidator>
Probably you could try this way:
Always initiate Regular Expression Validator in the page along with the control.
Make the Regular Expression Validation disable through javascript and make the control's display to none if you don't need the control
presence in the UI.
Make the Regular Expression Validation enable through javascript and make the control's to be appeared if you need the control presence
in the UI.
Good Reference to read for this:
http://msdn.microsoft.com/en-us/library/aa479045.aspx
I'm having problems with a chunk of code that's meant to add a textbox to a Repeater in ASP.
I have the following:
<asp:Repeater ID="uxRolesList" runat="server">
<ItemTemplate>
<div id="<%# GetRolesDivId() %>" class="div_row">
<asp:TextBox ID="uxTxtBoxRole" runat="server" rows="5" columns="100" Text='<%# DataBinder.Eval(Container.DataItem, "RequirementDescription") %>' TextMode="multiline" MaxLength="2000"></asp:TextBox>
<input type="button" style="vertical-align:top;" value="X" class="remove-roles-btn" />
<br /><br />
</div>
</ItemTemplate>
</asp:Repeater>
Which generates a load of textboxes that look like this in the html:
<td id="rolesColumn">
<div id="roles-0" class="div_row">
<textarea name="ctl00$mainContent$uxRolesList$ctl00$uxTxtBoxRole" rows="5" cols="100" id="ctl00_mainContent_uxRolesList_ctl00_uxTxtBoxRole">Cool Job1</textarea>
<input type="button" style="vertical-align:top;" value="X" class="remove-roles-btn" />
<br /><br />
</div>
</td>
I've also added the following button, that should add a textbox to this list when hit:
<asp:Button CssClass="btn" ID="uxAddRoleBtn" runat="server" Text="Add a new role requirement" />
Using the following jQuery code:
$("#ctl00_mainContent_uxAddRoleBtn").live("click", (function (e) {
var rolesCounter = $('#ctl00_mainContent_uxTxtBoxRolesCount').val();
if (rolesCounter < 10) {
var rolesCounterText = "0" + rolesCounter;
} else {
var rolesCounterText = rolesCounter;
}
$('#rolesColumn').append("<div id='roles-" + rolesCounter + "' class='div_row'><textarea name='ctl00$mainContent$uxRolesList$ctl" + rolesCounterText + "$uxTxtBoxRole' rows='5' cols='100' id='ctl00_mainContent_uxRolesList_ctl" + rolesCounterText + "_uxTxtBoxRole' MaxLength='2000' ></textarea><input type='submit' name='ctl00$mainContent$uxRolesList$ctl" + rolesCounterText + "$uxRemoveRoleBtn' value='X' id='ctl00_mainContent_uxRolesList_ctl" + rolesCounterText + "_uxRemoveRoleBtn' class='remove-roles-btn' style='vertical-align:top;' /><br /><span id='ctl00_mainContent_uxRolesList_ctl" + rolesCounterText + "_uxValTxtBoxRole' style='color:Red;visibility:hidden;'>Please complete this role requirement</span><br /><br /></div>");
e.preventDefault();
rolesCounter++;
$('#ctl00_mainContent_uxTxtBoxRolesCount').val(rolesCounter);
}));
So far so good. I hit the add button and the textbox appears, I type something in, everything's great. The html look something like this:
<div id="roles-0" class="div_row">
<textarea id="ctl00_mainContent_uxRolesList_ctl00_uxTxtBoxRole" cols="100" rows="5" name="ctl00$mainContent$uxRolesList$ctl00$uxTxtBoxRole">Cool Job1</textarea><input class="remove-roles-btn" type="button" value="X" style="vertical-align:top;"><br><br>
</div>
<div id="roles-1" class="div_row">
<textarea id="ctl00_mainContent_uxRolesList_ctl01_uxTxtBoxRole" maxlength="2000" cols="100" rows="5" name="ctl00$mainContent$uxRolesList$ctl01$uxTxtBoxRole">Test</textarea><input class="remove-roles-btn" type="submit" style="vertical-align:top;" value="X" name="ctl00$mainContent$uxRolesList$ctl01$uxRemoveRoleBtn"><br><br>
</div>
Then I hit submit and the new values do not come through.
In the C# side I'm trying to access the data using:
foreach (RepeaterItem item in dl.Items)
{
System.Web.UI.WebControls.TextBox rb = item.FindControl(control) as System.Web.UI.WebControls.TextBox;
if (rb.Text.Trim() != "")
{
PositionRequirement pr = new PositionRequirement();
pr.RequirementDescription = rb.Text;
pr.RequirementLevel = new PositionRequirementLevel(level, levelDescription);
pr.OrderNumber = i;
i++;
positionRequirements.Add(pr);
}
}
where dl = uxRolesList
control = uxTxtBoxRole
I'm at an utter loss as to why the new values are not coming through with the uxRolesList Repeater.
What am I doing wrong?
from what i know , the approach used is not going to show the items that are added within the Repeater Datasource, unless they exist before the page is being served to the user, so in the example, only the items that were bound to the repeater before leaving the server (if any) will show.
if you don't want to leave the page and make a trip to the server on every add click,of the top my head i would suggest that you would access them via the request Object using the name instead of the id ( Request[""] ) and keep the name of each textbox similar ("txtbox1","txtbox2")and append the count as you did in your Jquery code, then on the server when the page is submitted loop over the items using the counter that you have stored in uxTxtBoxRolesCount.
There is a website I need to login using HttpRequest. The login form of said website uses POST method. I know how to use HttpRequest for pages with no protection, how can I login to a website using POST?
This example is by the courtesy of http://www.netomatix.com.
The POST is set to the HttpWebRequest.Method property in the OnClick handler in code behind.
Example form:
<form name="_xclick" target="paypal"
action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="me#mybiz.com">
<input type="hidden" name="item_name" value="HTML book">
<input type="hidden" name="amount" value="24.99">
<input type="image" src="http://www.paypal.com/images/sc-but-01.gif"
border="0" name="submit" alt="Make payments with PayPal!">
<input type="hidden" name="add" value="1">
</form>
Code behind:
private void OnPostInfoClick(object sender, System.EventArgs e)
{
string strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST"; // <<--- This is the key word of the day
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}