I'm not am experienced asp.net user so hopefully this is fairly easy as it is in php.
I am trying to receive modified html of a div that is modified by the user of a webpage and save it back to a database.
When I click the save button the old(previous) html data is being displayed not the new html data that I want saved.
html:
<asp:LinkButton ID="saveButton" type="button" CssClass="clickable" runat="server" Text="Save" OnClick="SaveButtonClicked" />
<div id="formContainer" class="form-container" runat="server">
<div class="form-header">Form Title</div>
<ul class="section-sortable"></ul>
<div class="form-footer">
<input type="submit" value="Submit form" />
</div>
</div>
c#
protected void SaveButtonClicked(object sender, EventArgs e)
{
//Returns old html when it should return the new modified html?
Debug.Write(this.formContainer.InnerHtml);
}
The html modification code is not shown here but the html is being modified..
Only solution I found was to listen to both the Click and ClientClick events copy the Inner HTML from the div to a hidden input control and then the data is visible on the click event.
Related
so I am still new to C# and I currently testing out my project regarding a search function when user key in what they want to find out in a the textbox and press enter and it will redirect them to my search page. However everytime i tried to type things in the search button and press enter what came out is
http://localhost:51182/Default.aspx
to
http://localhost:51182/Default.aspx?ctl00%24search=(whatever I keyed).
As the search function is in the masterpage.aspx i cannot use asp:buttons or what.
This is my code in the masterpage.
<form class="navbar-form navbar-right">
<input type="text" id="search" class="form-control" placeholder="Search..." runat="server">
<button class="btn btn-default" type="submit" id="Button1" onserverclick="Button1_OnClick"><i class="glyphicon glyphicon-search"></i></button>
</form>
This is my c# code for masterpage.
protected void Button1_OnClick(object sender, EventArgs e)
{
//The code was commented out since the search name was not able to be recognized in my webform input id so i tried to deal with the response first.
//String searchtext = search;
//String Search = Server.MapPath("Website\\HTML\\SearchEngine.aspx");
Response.Redirect("~/Website/HTML/SearchEngine.aspx?search=");
}
I mainly put the codes that I think that matters and if required the other part just say so. In addition to it if this is a duplicate to other question, I am sorry as I been trying it out on some other question answer for their fixes to this but i wasn't able to get my working then I submit this question.
Hope you guys can help me out.
<form class="navbar-form navbar-right">... </form>
In ASP.NET WebForm, you cannot use another form tag, since there is a main form tag with runat="server" already.
The easiest way to achieve is to use Panel. When user types inside textbox and press Enter key, it will call Button1_OnClick event.
For example,
<asp:Panel ID="SearchPanel" runat="server" DefaultButton="SearchLinkButton">
<asp:TextBox runat="server" ID="SearchTextBox" CssClass="form-control"
placeholder="Search..." />
<asp:LinkButton runat="server" ID="SearchLinkButton" CssClass="btn btn-primary"
OnClick="SearchLinkButton_Click">
<i class="glyphicon glyphicon-search"></i>
</asp:LinkButton>
</asp:Panel>
So you can't have nested forms, but that's not to say that you can't have multiple forms on the same page, they just can't overlap, and only one can be an ASP.NET Form[runat=server]. Just make sure the search form, which can be a vanilla HTML form, is placed in the master page outside of any Form[runat=server] and format the search form like so:
<form class="navbar-form navbar-right" action="/Website/HTML/SearchEngine.aspx">
<input type="text" name="search" class="form-control" placeholder="Search..." />
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</form>
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
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.
I am populating a ListView with HTML from a database using a Literal with Text='<%#Eval("HTMLData")'%>. When I trigger a PostBack, changes to the loaded HTML are not being reflected in litRowData.Text.
ViewState is enabled for the page, the ListView, and the Literal in the ItemTemplate, and I am making sure to only populate the ListView with initial values from the database when if(!IsPostBack) is true in Page_Load.
<asp:ListView ID="lvForm" runat="server"
DataKeyNames="RowID" ItemPlaceholderID="phRow"
EnableViewState="true">
<LayoutTemplate>
<asp:PlaceHolder ID="phRow" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Literal ID="litRowData" runat="server" Text='<%#Eval("HTMLData")%>'
EnableViewState="true"></asp:Literal>
</ItemTemplate>
</asp:ListView>
I need to be able to capture changes to the contents of the loaded HTML controls. Since this HTML comes from a database table, I can't just use ASP controls inside the ItemTemplate. Can anyone see something I'm missing, or suggest an alternative way to do this?
Edit:
To clarify a little more, I'm trying to load form input elements dynamically from a database, render them as HTML controls on the page, allow the user to modify their contents by entering text or selecting options, then capture the modified HTML and save it back to the database when the user clicks a save button.
The way postback works in .NET is actually a wrapper around the more basic idea of HTML forms. A basic example of HTML forms is:
<html>
<body>
<form action="" method="POST">
<input type="text" value="type here" />
<input type="submit" value="go" />
</form>
</body>
</html>
Roughly, what the .NET abstraction adds is:
<html>
<body>
<form action="" method="POST">
<input type="hidden" name="__VIEWSTATE" value="string-encoded-value" />
<input type="text" name="bob" value="type here" />
<input type="submit" value="go" />
</form>
</body>
</html>
Whereby on postback to your page, all input elements with names are mapped back into properties of your Page object, and the __VIEWSTATE hidden field is deserialized into all properties of objects that do not correspond to values of html input tags. For example, if Page.bob had a DateTime property associated with it, it would be stored in __VIEWSTATE possibly.
ASP.NET Literal tags in Page markup will get printed into the browser exactly as is, meaning that if you have <span>bob</span> as its value, that is how it will appear within the <form> tag. However, in plain HTML world, <form> tags when posted will only contain the values of certain form elements (aka not every div, span, p etc. gets posted back, only input, select, textarea and some others). So if your literal doesn't contain an input then it won't even get posted back meaning __VIEWSTATE will be used to restore the Value property of the Literal back to its initial state.
To fix this, you probably don't want to stick html into a Literal because even if you do it's not clear that it will get associated with the right property of your page. Instead, try a TextBox element or something else that gets written as an input element directly by the ASP.NET webforms code. Alternatively, try using javascript to allow modifications of flat text in divs if you don't need to persist the data.
This answer builds on the prior one now that you have a .NET TextBox control that is correctly posting back the value of edits. Right below it, you can add to code behind:
protected void Page_Load(object sender, EventArgs e)
{
litRowData.Attributes.Add("onKeyUp", "WriteText(this.value)");
}
Html:
<ItemTemplate>
<asp:TextBox ID="litRowData" runat="server" />
</ItemTemplate>
<div id="yourPreview"></div>
<script type="text/javascript">
function WriteText(val){
document.getElementById("yourPreview").innerHTML=val
}
</script>
I have a web form which is posting a few fields to a payment site. The structure the example is using is
<form method="post" action="<%= FormAction %>">
<input type="hidden" name="Hash" value="<%= sHash %>" />
<input type="hidden" name="MID" value="<%= sMID %>" />
......
<div class="submit">
<input type="submit" value="Post Data" />
</div>
Within the example project they have set the same variables within the code behind file (after declaring them) with values. When the button above is clicked it posts the data to the relevant page without errors and the user is redirected to the payment page.
So i am now trying the same under the click event of an image button on my site but doesnt work:
<asp:ImageButton ID="ButtonCheckout" runat="server" Text="Go to payment page" ImageUrl="~/Images/Payment.gif" />
and of course it doesnt work, ive added all the markup as above and added all sample code.
When i run the app it flashes and stops. I look in Fiddler and notice its not going to the payment site at all. The masterpage has declared in it - incase this makes any difference.
Reading around it seems i could use the image button to post the data and do exactly the same as the submit button (submit button is above which was part of the sample application from the payment provider) by using the onClick event in the markup with Javascript but im struggling to understand how to tie it all together and lost to what i could do next to get this working?
You can't just post form data with an image button. You need to submit the form data - otherwise your image button is just a link. I do this a lot with Javascript.
Try adding this.form.submit() to the clientclick. Or name your form and use this.formname.submit()
Either of those should post your form data.
You can such so write:
<form id="Form2" method="post" runat="server" action="<%= FormAction %>">
<input type="hidden" name="Hash" value="<%= sHash %>" />
<input type="hidden" name="MID" value="<%= sMID %>" />
......
<div class="submit">
<asp:ImageButton ID="ButtonCheckout" runat="server" Text="Go to payment page"/>
</div>
</form>
but you must add a few attributes in top of web page,eg:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableEventValidation="false" EnableViewStateMac="false" ValidateRequest="false" ViewStateEncryptionMode="Never" %>