The pagemethod works, however when I use the webmethod it refreshes the page destroying the reason to use angular 2 in the first place.
How do I prevent the form from refreshing the page?
index.aspx
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManagerMain"
runat="server"
EnablePageMethods="true" >
</asp:ScriptManager>
<my-app>Loading...</my-app>
</form>
</body>
index.aspx.cs
[WebMethod]
public static string getString()
{
return "Test";
}
app.component.html
<div>
<Button (click)="btnSubmit_Click">test</Button>
</div>
app.component.ts
btnSubmit_Click()
{
var test = window['PageMethods'].getString(this.onSucces, this.onError);
}
note:
At this moment I'm trying to use angular 2 with aspx for the company if it works it might become a standaard for small frontend api's and since its quite limited on information I appreciate any help.
You should make button type as button, because by default button type is submit. button with type submit may lead to post back a page.
<div>
<Button type="button" (click)="btnSubmit_Click()">test</Button>
</div>
Related
I'm really new to ASP and have a project where I need to capture the login of the person logged into Sharepoint. I have created the following code and am able to get the login. I am having trouble pass the parameter to a form. What is the best way to do this: I know the 2nd part the POST is wrong, I don't know what to do here. Thanks for your help. Scott
<script language="C#" runat="server">void Page_Load(Object sender, EventArgs e)
{
string userName = "NA";
userName = HttpContext.Current.User.Identity.Name;
string userWithoutDomain = userName.Substring(userName.IndexOf('\\') + 1);
myuserid.Text = userWithoutDomain;
}
</script>
<html>
<body>
<form method="POST" action="http://srs.xxx.edu:9001/signon/authenticate.asp" id="myuserid" name="myuserid">
<input type="hidden" name="COOKIEPATH" value="/SRS/">
<input style="width:70px;font-size:8pt;" size="8" name="myuserid" >
<input style="width:70px;font-size:8pt;" size="8" name="myuserid" >
<input class="button" type="submit" value=" Log On " style="display:none;">
</form>
<form id="form1" runat="server">
<asp:label id="myuserid" runat="server" />
</form>
</body>
</html>
You can pass values from the codebehind to html page using for example hidden values, see here.
Usually though values are placed into form elements in the codebehind directly. If the element has the "runat=server" attribute then you can access the element in the Page_Load method using c# and just set the text value directly.
myuserid.Text =
i have following problem, i am using a popup jquery dialog with asp:formview .
the purpose of this popup is for user to enter a hyperlink which is placed then in textbox control in formview
the popup dialog div is located outside a formview just after body tag
<body style="background-color: #FFFFFF; font-family:Lucida Console;">
<div id="dialog-form" title="sdfdfsdf" style="font-size:14px; ">
<form>
<fieldset>
<label for="link">sdfdf</label>
<input type="text" name="sdfsdf" id="link" size="32" />
</fieldset>
</form>
</div>
<form id="form1" runat="server" style="margin-top:50px;" >
<div>
<asp:FormView ID="FormView1"
.......
<InsertItemTemplate>
...
<sometextbox ...../>
<button id="create-user" class="ui-state-default ui-corner-all">Create link</button>
...
</InsertItemTemplate>
After clicking a button a popup window is shown BUT the page starts to refresh immediately
and of course the popup is then hidden.
If I relocate the button outside the formview - the page is not refreshed, but i need it in formview..
Any idea what to do?
add the following attribute to the button:
onclick="javascript: return false;"
this behavior should not come out because it is a button not submit button.
it seems when it is inside the form view a submit action is attached to it, check your jQuery scripts maybe you mistakenly added onclick submit while attaching the dialog.
I found my answer:
clientId must be used:
FTB_API['<%=FormView1.FindControl("AdminCommentTextBox").ClientID%>'].SetHtml(...)
I have a asp page where i have a button.When i click on the button i should display a gif image.When the process is completed ,again the images has to be hidden.
Please find my below code behind
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="display: none" id="dvloader">
<img src="Images/process_status.gif" /></div>
<asp:Button ID="btnLoad" runat="server" Text="Load" OnClick="btnLoad_Click" />
</form>
</body>
Please find the below code behind
protected void btnLoad_Click(object sender, EventArgs e)
{
// Show the gif image
System.Threading.Thread.Sleep(5000);
// hide the gif image
}
How can i achive this ?
Thanks.
The method in your example is server-sided. What you want to do is on the client side and must be done in javascript therefore.
You could use the client-sided onclick-event to show the GIF after the button has been clicked. You can set the client-sided onclick-event somwhere in the Page-Load method for example:
btnLoad.Attributes.Add("onclick", "alert('JavaScript to show GIF goes here...');");
After the PostBack, the GIF will be hidden again automatically...
You could actually apply the event to the element without using the inline event handler.
Using jquery you could say:
$(document).ready(function() {
$('#btnLoad').click(function(){alert('JavaScript to show GIF goes here...');});
});
This assumes your button has an id of btnload.
I'm trying to figure out a way to create a page that can be used for generic forms. What i had in mind was someone here at our office would use the obout edit to create html that would look like a form. This html would be saved to a database. When the user wanted to fill out the form and mail it, they would click on the correct form retrieval and this html would be put into a label control. So now the page has a label control on it with some html as the text of the label. The user could type in the values that they wanted. So the html might look something like this:
<p style="margin: 0px; text-align: center;">
Quarterly Report of Employees Serverd</p>
<br />
<br />
Employee:
<input type="text" style="width: 300px; height: 22px;" />
<br />
<br />
Address:
<input type="text" style="width: 300px; height: 22px;" />
<br />
<br />
<br />
The user would type in the input type="text" a value of say John Smith and then type into the address input type as well.
Now I want to grab all this html and mail it off. I know how to do the mailing portion, but the grabbing the html I'm not getting. I can grab the html in the label, but the text that that user typed in is not included in that text. So how do I get that html and the text the user typed in. Any ideas.
thanks
shannon
You need to encode the html to store them securely.
Use literal control instead of label or text controls.
Check this link for the first option.
Thanks :)
and you can use a way like this :
<input id="textField" type="text" runat="server"/>
C# - Code Behind
Mail.Send(textField.Value);
and don't forget to use ValidateRequest="false" in the header section of asp.net..
:)
You can iterate through the Request items to get the submitted values. If you want to send of an email which substitutes the entered values for the HTML input fields, you'll have to parse out the form HTML to find your input fields:
<input type="text" id="firstName" name="firstName"
style="width: 300px; height: 22px;" />
And then replace it with
Request.Form["firstName"]
Notice that the input fields will need identifiers so you can retrieve them from the HTTP request.
Hope this helps!
For one thing, you're really going to want to get a little more information into your html - at the very least some name attributes on the form elements:
Employee: <input type="text" name="employee" />
This way, you could create an aspx page like:
<%# Page language="C#"
autoeventwireup="true"
codebehind="DynamicForm.aspx.cs"
inherits="TempWebApp.DynamicForm" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal id="m_HtmlForm" runat="server"></asp:Literal>
<asp:Button id="m_SubmitForm"
onclick="SubmitForm_OnClick"
runat="server"
text="Submit" />
</div>
</form>
</body>
</html>
Then in your code you read your html from the database into the .Text of the literal control:
using System;
using System.Web.UI;
namespace TempWebApp
{
public partial class DynamicForm : Page
{
protected void Page_Load(object sender, EventArgs e)
{
m_HtmlForm.Text = getFormData();
}
protected void SubmitForm_OnClick(object sender, EventArgs e)
{
// Just showing we've picked up the form data.
// You'll also see ViewState and others in here.
foreach (string key in Request.Form.AllKeys)
{
m_HtmlForm.Text += string.Format("<br />Key: <b>{0}</b><br />Value:<br />{1}<br />", key, Request.Form[key]);
}
}
private string getFormData()
{
// Excluded for brevity
}
}
}
Obviously, in your button/postback handler, you would need to ensure you have the original HTML from the database, and then either modify the input elements to add a text attribute to them with the value, or more likely, replace them with the actual values - otherwise you'll have to do lots of additional parsing of the form if your users start saving forms with radio buttons, check boxes, select lists, etc.
I have a set of websites that basically do the exact same thing in terms of functionality, the only thing that varies is the css and how the html is laid out.
Is there a way to generate the html from a database (ie retreiving the html from a table) and then catching the controls (like buttons, textboxes etc) from the code behind?
UPDATE:
Here is an example of what I want to acheive:
<html>
<div id="generatedContent" runat="server">
<!-- the following content has been generated from the database on the page load event -->
<div>
This is a textbox <input id="tb1" runat="server" type="text" />
This is a button <input type="submit" id="btn1" runat="server" value="My Button" />
</div>
</div>
</html>
This is the code behind
var tb1 = new HtmlControls.HtmlInputText();
tb1 = FindControl("tb1");
var btn1 = new New HtmlControls.HtmlInputSubmit();
btn1 = FindControl("btn1");
Now obviously since the html has been generated from the database (ie after the page has already been initialised) the FindControl method will return null.
For static HTML content, you can easily add your content to your form by using LiteralControl like that :
Page.Controls.Add(new LiteralControl("<b>content from db</b>"));
EDIT : Your code-behind code (FindControl) should work if you put them in a form tag with runat=server attribute. But beware, your content should be added in every postback. Also you can get the values of your form elements by Request["FormElementId"].