Saturday, March 24, 2012

Text on the button does not display

Hello,

I am new to Dot Net. In the following code, the text on the button does not display. However, if I add the following for the button control, it does display.

Text ="Confirm"

Also, instead of using <asp:/> I tried using the following and the text still did not display and I get the error message: "'Click' is not a member of 'ASP.SimpleForm_aspx'"

<p><button id="mybutton" OnServerClick="Click" runat="server"> Confirm</button></p>

<html>
<head>
...
</head>
<body>
<form runat="server">
<h2>Take the Survey!</h2>
<p>Name:<br />
<input type="text" id="txtName" runat="server" /></p>
<p>Email:<br />
<input type="text" id="txtEmail" runat="server" /></p>
<p>Which server technologies do you use?<br />
<select id="servermodel" runat="server" multiple="true">
<option>ASP.NET</option>
<option>JSP</option>
</select></p>
<p>Do you like .NET so far?<br />
<select id="likedotnet" runat="server">
<option selected>Yes</option>
<option>No</option>
</select></p>
<asp:button id="mybutton" OnServerClick="Click" Confirm runat="server" />
</form>
</body>
</html>

Thanks in advance

Number one: The asp button has a field called text. This is what sets the text that appears on the button. This value has to be set for the text to display.

Number two: you have onserverclick as your click command. This should just be onclick="Click".

Hope this helps


When you tell your button, onServerClick="Click" your telling it, to execute a sub or function called "Click" which doesnt exist in your example.

To get the text of your button to show up, do this.

<asp:ButtonText="MyButton"runat="server"ID="Button1"/>


Msamford and Freakyuno,

Thanks a lot for your response. I tried playing with the code.

As requested by you, when I try any one of the following within the aspx code the display works fine and when I click on the "Confirm" button I see "Done" at the bottom of the screen.

<asp:button id="mybutton" Text="Confirm" OnServerClick="Click" runat="server"/>

<asp:Button Text="Confirm" runat="server" ID="Button1" />

However, when I use the following line of code, I see that the output is exactly the same but when I click on the "Confirm" button, I see an "Error on Page" at the bottom of the screen instead of "Done". I tried the following Code Behind for the event handler.

<p><button id="mybutton" OnClick="Click" runat="server">Confirm</button></p>

CodeBehind

<script runat="server" language="C#>
void Click(Object s, EventArgs e){
Response.Write(mybutton.ID);
}
</script>

Code Behind

//First off we import some useful namespaces
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

//All code-behind classes generally inherit from Page
public class SimpleForm3 : Page
{
// Declare the presentation elements on the ASPX page
protected Button mybutton;

//Here's the Click Handler just as it appeared before
public void Click(Object s, EventArgs e){

}
}

I am not sure why. Thanks in advance!!!


Hello,

I added the C# code inside a server-side script block within the header tag of the aspx page. Also, s I am trying to grasp html controls and web forms, I believe the OnServerClick specifies the asp dot net handler for clicks on the button for html controls?

SimpleForm.aspx

<html>
<head>
<script runat="server" language="C#">
void Click(Object s, EventArgs e){
Response.Write("Your name is: " + txtName.Value + "<br />");
Response.Write("Your e-mail is: " + txtEmail.Value + "<br />");
Response.Write("You like to work with: " + servermodel.Value + "<br />");
Response.Write("You like NET: " + likedotnet.Value);
}
</script>
</head>
<body>
<form runat="server">
<h2>Take the Survey!</h2>
<p>Name:<br />
<input type="text" id="txtName" runat="server" /></p>
<p>Email:<br />
<input type="text" id="txtEmail" runat="server" /></p>
<p>Which server technologies do you use?<br />
<select id="servermodel" runat="server" multiple="true">
<option>ASP.NET</option>
<option>JSP</option>
<option>PHP</option>
<option>CGI</option>
<option>ColdFusion</option>
</select></p>
<p>Do you like .NET so far?<br />
<select id="likedotnet" runat="server">
<option selected>Yes</option>
<option>No</option>
</select></p>
<p><button id="mybutton" OnServerClick="Click" runat="server">Confirm</button></p>
</form>
</body>
</html>

Thanks

dotnetnew123


dotnetnew123 wrote:

However, when I use the following line of code, I see that the output is exactly the same but when I click on the "Confirm" button, I see an "Error on Page" at the bottom of the screen instead of "Done". I tried the following Code Behind for the event handler.

<p><button id="mybutton" OnClick="Click" runat="server">Confirm</button></p>

the problem here is you have no asp: in your tag. Therefore when you click the button it is not accessing your servercode. The only way to access the click event is to have the servercontrol properly written with <asp:button ...></asp:button>


Other folks have given you the answer on this, but I'll try since you don't seem to be doing what they advise. Some of my advice might be moot since it could be just a cutting and pasting error, but here we go:

You don't have the @.Page declaration at the top of your document (above even the <html> tag). I just tried your code as-is but with the added page tag and it works just fine.


Thanks all. It works fine now!

0 comments:

Post a Comment