1

I am very new to asp.net and c# and haven't written anything in the language till now. So, I am unable to detect a button click.

Here is my code:-

contact.aspx

<%@ Page Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="true" CodeFile="contact.aspx.cs" Inherits="contact" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title>Sugarsnooper | Contact Us</title>
<meta name="keyword" content=""/>
<meta name="Description" content=""/>
<meta name="key-phrases" content=""/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<section class="content-page"><div class="container"><div class="row">
<div class="col-md-12"><h1>Contact Us</h1></div>
<div class="col-md-8">
<h4>Enquiry Form</h4>
<form id="form1" runat="server">
<div class="col-lg-12 form-group">
 <div class="row">
 <input name="name" runat="server" type="text" id="name2" class="form-control" placeholder="Enter Your Name">
 </div>
 </div>
<div class="col-lg-12 form-group">
 <div class="row">
 <input name="website" runat="server" type="text" id="cont2" class="form-control" placeholder="Enter Mobile Number">
 </div>
 </div>
<div class="col-lg-12 form-group">
 <div class="row">
 <input name="email" runat="server" type="text" id="email3" class="form-control" placeholder="Enter Your Email ID">
 </div>
 </div>
<div class="col-lg-12 form-group">
 <div class="row">
 <textarea name="message" runat="server" id="msg2" class="form-control" placeholder="Enter Your Message.." rows="6"></textarea>
 </div>
 </div>
<div class="col-lg-12 form-group">
 <div class="row">
 <input type="button" runat="server" value="Send" id="enquiry_post" onClick="enquiry_post_click" class="btn btn-primary"> 
 </div>
 <asp:Label ID="Label1" runat="server" Text="User Name"/>
 </div></form>
</div>
<div class="col-md-4"><h4>Contact Info</h4>
<table class="table">
<tr>
<td>Email</td>
<td><a href="mailto:[email protected]">[email protected]</a></td>
</tr>
<tr>
<td>Helpline No.</td>
<td>xxx xxx xxxx</td>
</tr>
</table>
</div>
</div></div></section>
</asp:Content>

contact.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class contact : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 if(!IsPostBack){
 //Label1.Text = ""; 
 }
 }
 protected void enquiry_post_click(object sender, EventArgs e){
 Label1.Text = "Hi";
 }
}

Can anyone point out my error? Note:- I have seen other questions but can't seem to figure out what I am doing wrong, so don't point to another question.

Thanks in advance

asked May 27, 2020 at 12:41

1 Answer 1

2

Replace

<input type="button" runat="server" value="Send" id="enquiry_post" 
 onClick="enquiry_post_click" class="btn btn-primary"> 

with

<asp:Button runat="server" Text="Send" ID="enquiry_post" 
 OnClick="enquiry_post_click" CssClass="btn btn-primary"/>

or

<input type="submit" class="btn btn-primary" id="enquiry_post" value="Send" 
 runat="server" onserverclick="enquiry_post_click"/>
answered May 27, 2020 at 12:44
Sign up to request clarification or add additional context in comments.

4 Comments

It is causing my page to refresh upon click, Is there any other way?
To avoid a full page refresh you can use a asp:UpdatePanel or a manually written ajax call.
Do I need to cover the label with the update panel?
You have to put it around - something like <asp:UpdatePanel><ContentTemplate><asp:Label><asp:Button></ContentTemplate></asp:UpdatePanel>

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.