HTML button onserverclick is not working in asp.net

若如初见. 提交于 2020-01-07 06:45:15

问题


I had a problem in the event onserverclick. I have created an HTML server-side button "btnsubmit" in tab_1:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="Skripsi.Home" %>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Home</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

    <div class="container-fluid bg-1 text-center">
        <img src="/Image/fti-untar.png" alt="untar" />
        <h2>Layanan</h2>
        <br />
    </div>

    <div class="container">
        <ul class="nav nav-pills nav-stacked col-md-2">
          <li class="active"><a href="#tab_1" data-toggle="pill">Home</a></li>
          <li><a href="#tab_2" data-toggle="pill">Biodata</a></li>
          <li><a href="#tab_3" data-toggle="pill">Konsultasi</a></li>
          <li><a href="#tab_4" data-toggle="pill">Hasil Studi</a></li>
        </ul>
        <div class="tab-content col-md-10">
            <div class="tab-pane active" id="tab_1" runat="server">
                <div id="panel1" class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Biodata</h3>
                    </div>
                    <div class="panel-body">
                        <label for="id">ID</label>
                        <input type="text" id="txtid" class="form-control" runat="server"/>
                        <br />
                        <label for="nama">Nama</label>
                        <input type="text" id="txtnama" class="form-control" />
                        <br />
                        <label for="email">Email</label>
                        <input type="text" id="txtemail" class="form-control" />
                        <br />
                        <label for="alamat">Alamat</label>
                        <input type="text" id="txtalamat" class="form-control" />
                        <input type="button" id="btnsubmit" runat="server" class="btn btn-default" onserverclick="btnsubmit_ServerClick">Submit</input>
                    </div>
                </div>
            </div>

            <div class="tab-pane" id="tab_2">
                                <div class="col-xs-3">
                    <input type="text" id="txtname" name="Name" value="" class="form-control"/>
                </div>
                <h4>Pane B</h4>
            <p>Pellentesque habitant morbi tristique senectus et netus et malesuada famesac turpis egestas.</p>
            </div>
            <div class="tab-pane" id="tab_3">
                <h4>Pane C</h4>
                <p>Pellentesque habitant morbi tristique senectus et netus et malesuada famesac turpis egestas.</p>
            </div>
            <div class="tab-pane" id="tab_4">
                <h4>Pane D</h4>
                <p>Pellentesque habitant morbi tristique senectus et netus et malesuada famesac turpis egestas.</p>
            </div>

        </div><!-- tab content -->
    </div><!-- end of container -->
</body>
</html>

And the code behind is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Skripsi
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsubmit_ServerClick(object sender, EventArgs e)
        {
            Response.Write("Success Horray");
        }

    }
}

When Pressing the button I get the following Error:

asp runtime error(the base class includes the field 'btnsubmit', but it's type is not compatible with the type of control)

Update: After cleaning the solution and rebuilding it I had the following error:

'content is not allowed between the opening and closing tags for element input'.


回答1:


it's asp runtime error(the base class includes the field 'btnsubmit', but it's type is not compatible with the type of control)

From Solution Explorer window, right click on your solution and choose Clean solution and then rebuild solution

and format the element to be like this

<input type="button" id="btnsubmit" runat="server" class="btn btn-default" onserverclick="btnsubmit_ServerClick" value="Submit" />



回答2:


Can you try "input type='button' " instead "button" ?

<button id="btnsubmit" runat="server" class="btn btn-default" onserverclick="btnsubmit_ServerClick">Submit</button>


来源:https://stackoverflow.com/questions/40734663/html-button-onserverclick-is-not-working-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!