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

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.

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"

Dynamic file upload controls not working in asp.net webforms page

I have a web page where I dynamically generate lots of elements from code behind and inject them into the page, as such:
input type="file" style="width:480px;" id="fileUploadLogo345">
I also have buttons for submitting, like this:
input id="Submit1" type="submit" value="Substituir Logo" onclick="return jsSubmitLogo(345);">
(The javascript function always returns true...)
No matter what I try, I am unable to get any of the uploaded files from code behind.
I tried adding enctype="multipart/form-data" to the form, I tried adding runat="server" to the file upload controls, I have no idea what else I could try. No matter what I do, in code behind Request.Files.Count equals zero.
Any idea what I could be doing wrong?
If it makes any difference, I am using a master page...
Thanks
You are using "id" attributes in your html... you should instead use "name" attributes, and everything will show up fine in code behind.

When working in Visual Studio, can the ' asp: ' portion of an element be omitted?

Situation: I have been creating webpages in HTML5/CSS3 & Javascript using Sublime 2 text editor for a year, however a college course now requires me to use Asp.Net and Visual Studio 2010. I do not use the designer because I am proficient at doing things by hand, however I find that writing asp: inside every element is time consuming and causes syntax errors when applied to some HTML 5 tags and not others.
Example HTML 5: <button id="btn" type="submit" value="Button"/>
Example Asp.net: <asp:Button ID="Button1" runat="server" Text="Button" />
Question: Can the asp: portion be omitted without effecting anything or is it required for IIS or the C# back-end functionality? What about runat="server" can that be omitted?
Google has come up dry regarding my inquiry, so any help is appreciated.
you simply cannot remove either of the two
but hear me out why, because I have a feeling you are not familiar with ASP and therefor are mistaking the meaning of the asp: and the runat="server" syntax.
first: runat="server"
this property on an element, tells the the compiler that this is actually a server side control
so a <button/> is not the same as an <button runat="server"/>
the first one is pure html, while the second one is a control, which can be bound to on the server side. .Net will give it a clientID (not to be mistaken by the ID you have to give it yourself).
second: asp:
this is a prefix, on certain elements, that tells the compiler these are ASP controls (the default controls given by the ASP.net framework). These include Buttons, TextBoxes, DropDownLists, ...
do not mistake 1 of these with a html element.
an <asp:Button id="myAspButton" runat="server"/>
is not the same as a <button id="myHtmlButton"/>
the first, is a server side control, which can be bound to (see it's runat="server" attribute), and this control renders to the browser as a <input type="submit"/> for example.
you could alter the rendering of the asp.net button class to make it return something entirely differnt if you wish.
and you are also not limited to using asp.net classes.
you can create your own controls, and put them in a custom created library
you could give those your own prefix.
if I created such a custom control, I could register a prefix for it in the web.config file,
and thus I could create a custom button extending from the original one (but with a default label in front...
<myc:CustomButton ID="myButton" Text="myButton" Label="myLabel" runat="server"/>
which could render into:
<label>myLabel</label>
<button ID="*******">myButton</button>
the asterisks are symbolizing the Unique ID it will get from the .net framework
if you want to know more on custom controls, or extending default controls
here is a step by step explanation to create custom controls, or extend from a TextBox control.
it also shows how you add a custom prefix for your controls (in the this case 'cc')
you can find more info here
The runat="server" part is required to tell .NET that it will have to render a button there (which will contain .NET specific ID for processing upon POST). Not too familiar with web forms (I started with MVC), but I would assume that the asp: part is to help distinguish between server controls and standard HTML markup.
Why not try removing it and if it breaks something, then you know it's needed. For instance if the button doesn't show up after removing it, then obviously the .NET markup parser needs it to be there in order to know that it is a place holder for a server control.

Editing a Database Entry

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>

ASP.NET events symbol (yellow lightning bolt) missing

I created a new C# ASP.NET Web Application project in .NET 3.5
Then I dragged a button in my form.
In the property window the events symbol (yellow lightning bolt) does not show. When I double click the box next to 'OnClientClick' in the properties Window no default event handler is created. When I double click the button in design view no event handler is created either.
Any ideas on how I can get the events symbol and the automatic generation of a default event handler?
Perhaps you didn't have the option checked for "Place code in a separate file" when adding the webform? Also "OnClientClick" isn't a server side event, it is just a string property so it wouldn't ever map to a server side event handler.
Please check the tag that is being used for your control. If it looks like this:
<input type="submit" runat="server" value="OK" id="btn_1" />
then there will never be an Events symbol. Adding runat="server" to a regular HTML tag turns it into a rather basic Server'ified HTML control, with properties and events, yet Visual Studio does not recognize it as a control with events. (So events do exist, but you'll have to write all the necessary code and declarations yourself)
If the above is the case, rewrite it to this:
<asp:Button runat="server" Text="OK" id="btn_2" />
and then the Events symbol will be there. <asp:Button> creates a full featured Server Side Web Control, which in the end renders similar output, while offering a much richer set of features (such as Design mode support).

Resources