Editing a Database Entry - asp.net

I'm creating a website with ASP.net that, if you are logged in as an admin, you can edit the current pages text (this way I don't have to update the website all the time just for small changes, I can just edit the page myself while its online). All the page data is stored in a SQL Database table. Each entry is simply "ID" and "PAGECONTENT". Now, I used a SQLDataSource to get the data and put it into a FormView on my page. This all works. I made a button that is only visible if I'm logged in as the site-admin. I need to make it so when I click the button, the textlabel displaying the page content turns into a TextArea for me to edit, then I can click a "Done" button and have it update it to the Database table. I can do the updating part, I just don't know how I'm supposed to display the text in an editable manner...

You need something called a Rich Text Editor control. This will give you a Microsoft Word style interface with a toolbar etc and it will parse the html for you that you have stored in the database.
There are lots of free ones out there. A commonly used one is the one included in the Ajax Control Toolkit.
You should use nuget to install the latest Ajax Control Toolkit into your project.
There are lots of tutorials out there which explain how to use this control:
http://www.google.co.uk/search?q=asp.net+ajax+control+toolkit+htmleditor

The POST scenario: Just like you have a button that is visible only when you're logged as the site admin, have a textarea only visible when you click the edit button and have it saved when you click the done button.
A good practice would be to have a dedicated page for the edition of your db entry unless you're working with an ajax interface. It can get complicated and unsafe to POST when changing from a detail view to an edit view and to POST again when saving. That's just an idea.
The GET-then-POST scenario: have a dedicated edit page for your db entry. Let's say: /editpage.aspx?id=1. Have a done button on that page that postback and save your work.
Good luck.

http://www.w3schools.com/aspnet/control_htmltextarea.asp
maybe this is what you are looking for.
<script runat="server">
Sub submit(sender As Object, e As EventArgs)
p1.InnerHtml = "<b>You wrote:</b> " & textarea1.Value
End Sub
</script>
<html>
<body>
<form runat="server">
Enter some text:<br />
<textarea id="textarea1" cols="35" rows="6" runat="server" value="PUT_YOUR_TEXT_HERE_to_prepopulate_the_Data" />
<input type="submit" value="Submit" OnServerClick="submit" runat="server" />
<p id="p1" runat="server" />
</form>
</body>
</html>

Related

How to redirect to another page with using a function on that page with asp link

I am new to asp.net. So just learning everything step by step.
I have created a web project with the template.
On Page one, I have a button and a textbox with no action defined yet.
On Page two I have a button and a textbox and I also have db connections works from the button.
What I want to do is, if user writes something and click button on first page, I want to go page 2 and acts as clicked button.
I want to run some action on page two with parameter from page on.
I think this is possible with redireciton mechanism
I will post the code here. can anyone help me . I may be wrong as I am new to this language.
I can do it with a button but I want to do it with a asp link
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="BringPage2" />
Right now this is a link but I want to make it a button
<p>Bring Page 2 ยป<input id="txtBoxAlan" name="txtBoxAlan" type="text" />
Instead of redirect, I just used href as href="Default1.aspx"

Double-clicking on a form control in Visual Studio 2010 design view inserts a script instead of inserting an event handler

The title pretty much precisely asks the question, but I shall repeat;
When I double click on a form control while in the design view in a Web Application project within Visual Studio 2010, say a 'button' or a 'submit' for example, it inserts a javascript function into my .aspx file. When I do this at work it automatically creates an event handler for the control in the code-behind.
How do I change this to that setting? I have used '/resetsettings' already, and other answers to similar questions do not solve my problem. I have reinstalled, gone through every menu I can find (though I may have missed something) and I'm pulling my hair out.
I don't want to type those event handler subroutines every time! Halp meee!
Thanks in advance for any helpful replies.
What it looks like is going on with this is that you are double clicking on a regular HTML element. For example, if you have
<input type="button" value="Test" id="Test" name="Test" />
and double click on it in design view it will create a JavaScript function on the page for it and an onclick event in the element. If you want to create an event handler in the code behind, you need to have an Asp.Net control like
<asp:Button runat="server" ID="Test" Text="Test" />
and then double click that to create a code event in the code behind.
Also, make sure that your page directive at the top has the correct filename for its CodeFile attribute to correctly map to an existing code behind file.

userControl repeated with Form tag disappearing

I have a Contact userControl that have "save contact" as submit button and fields inside form tag we repeat this userControl with Code 20 times in one page
My problem is the Form Tag in the first userControl is hiding somehow --- i checked the userControl with developer Tool IE9 , firebug Firefox7 and the Form is not appearing in the first userControl and its appearing with the rest 19 Controls
I tried to View Source and take html copy in new html file in VS -- i found form is exist
i dont know if iam clear enough but please advice if you need more info
If you're running a form tag at the server, inside a user control, and then displaying this user control more than once on the page, then you will have multiple form tags running at server on one page, which is not allowed by ASP.NET
Take the form tag outside of the user control like:
<form runat="server">
<uc:Contact />
<uc:Contact />
<uc:Contact />
</form>

changing the PostBackUrl in ASP.net after a file download

I have a ASP.net which allows users to select a number of inputs and dropdown lists to filter the report and to download an Excel report after pressing a button on that same form.
The page can be used mulitple times, by changing the inputs and pressing the download
button each time.
The button should be posting back to the hidden IFRAME to a separate webpage, but I am
unable to change the target for the form on the fly as needed.
<form runat="server" method="post">
<asp:Button runat="server" id="DownloadToExcel" TabIndex="-1" Text="Download To Excel" UseSubmitBehavior="False" PostBackUrl="report.aspx" />
</form>
<iframe style="hide" id="output" src=""></iframe>
Additonally, when I download the file when pressing the "DownloadToExcel" button, the file downlodads as expected. However, any additional postbacks including updating the screen are posting back to report.aspx and not filter.aspx.
How do I correct the postback so it only postbacks once to download the file, and then change it back to the original page so the screen can continue to be used to filter and download a second report.
This may also eliminate the need of the IFRAME as well.
I solve the problem my adding PostBackURLs to all the submit buttons directly.
One for the Excel download that goes to report.aspx and the other for the postback on the page for filter.aspx.
It appears that if the postback is changed, in this case to report.aspx, it reuses the pathname for any subsequent postbacks, unless defined explicity, for each time you wish to postback.

Form tag on ASP.net page

I have a web application that has a page that loads the content from the database. I want to be able to put a form in the dynamic content, but .net doesn't let the inside form perform it's action. Is there a way to allow this or some other way I can get a form on a dynamic content page?
--EDIT--
I think I need to clarify something. This is an aspx page that loads content from the database. As far as I know, the text I pull from the db and stick in the Label is never compiled or processed by the .net wp, thus I can't use the code behind to fix this issue.
This is a common problem, when you want to have a non-postback form to a 3rd party site (like a PayPal button, for example).
The problem occurs because HTML doesn't let you have form within a form, and most ASP.NET pages have a <form runat="server" /> "high up" in the HTML (or in the Master page).
My favorite solution is to hide the "high up" form tag, while still showing all of the content. Then you can feel free to dump any tags you want in the body. If you do this dynamically you can choose on a page-by-page basis which pages have custom forms.
I created a class called GhostForm.cs to handle this. You can read all about it here:
http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html
There can only be one form on the page (the asp form); you have to use that form somehow.
To clarify, there can only be one form processed.
Not with webforms, no. You have to work within the one, full page form by using an event handler connected to a Button to LinkButton. Fortunately, it's pretty easy to do:
foo.aspx:
...
<asp:TextBox id="txtFoo" runat="server" />
<asp:Button id="btnFoo" runat="server" onclick="btnFoo_Click />
...
foo.aspx.cs:
...
protected void btnFoo_Click(object sender, EventArgs e)
{
string s = txtFoo.Text;
// do something with s
}
...
Dino Esposito has an article from MSDN magazine that covers handling multiple forms or "simulating" sub forms in ASP.Net that might just answer all your questions.
http://msdn.microsoft.com/en-us/magazine/cc164151.aspx
Any work around would be hacky and very ugly. By design asp.net uses a form tag to post and get data. This is why they call it a Web Forms Application. Html does not allow nested forms. What you want to do is use a WebRequest in your code behind.
If you are trying something like a paypal button you could simply use something like this.
Markup:
<div id="PayPalButtonContainer" runat="server"></div>
Code Behind:
public static string GetPayPalButtonMarkup()
{
const string markup = #"https://www.paypal.com/cgi-bin/webscr
?cmd=_xclick&business={0}
&item_name=Widget
&amount={1}
&currency_code=USD";
return markup;
}
PayPalButtonContainer.InnerHtml = string.format(GetPayPalButtonMarkup,"YOUR PAYPAL USER NAME", "YOUR PRICE VALUE");
you either have to deal with the postback by adding a server side click event handler to what you want to be the "sub forms" submit button (this is how web formas deals with multiple submit type buutons on the same page) or do soemthing clever with AJAX if you dont want a full post back
I've run across this issue before. One workaround that I have done is to place my code that I want my action to be done upon inside of an asp:Panel. With the panel you can set the attribute of "DefaultButton" to a button inside of the panel, and clicking the button (or pressing "enter") will fire that button's click event. I've found this quite handy when wanting to submit a "form" by pressing enter when I have a master page that contains the only allowable asp:Form.
Hope this helps.
When I first came across this problem, I found the simplest solution for me was to simple COPY and PASTE the Master page and give it a slightly different name, something like:
SiteNameMasterPage 'Default page with FORM tag
SiteNameMasterPageNF 'No Form tag
And then depending on wether I wanted a FORM tag or or not, simply change the masterpage link at the top of my CONTENT-PAGES, like this
<%# Page Title="" Language="VB" MasterPageFile="~/SiteName.master" %>
<%# MasterType VirtualPath="~/SiteName.master" %>
<!-- This masterpage has the default FORM tag -->
or
<%# Page Title="" Language="VB" MasterPageFile="~/SiteNameNF.master" %>
<%# MasterType VirtualPath="~/SiteNameNF.master" %>
<!-- This masterpage does NOT have the default FORM tag -->
and then in the content page, wherever I want to place my form I can include the <form> tag

Resources