asp.net网络表单中的代码无法访问Asp.net控件 [英] Asp.net controls are not accessible in code behind in asp .net web forms

查看:93
本文介绍了asp.net网络表单中的代码无法访问Asp.net控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后台代码无法访问Asp.net控件

Asp.net controls are not accessible in code behind

<asp:TextBox id="txtname" class="form-control"  runat="server"></asp:TextBox>

推荐答案

看看这个例子,看看你是否遗漏了什么:



index.aspx

check out this example and see if you have something missing:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <%--<input runat="server" type="text" id="txtName" />--%>
            <asp:TextBox runat="server" ID="txtusername" />
            <button type="submit">submit</button>
        </div>
    </form>
</body>
</html>





中的服务器代码index.aspx .cs



and the server code in index.aspx.cs:

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

namespace ProjectName
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RegisterError(txtName.Value.Trim());
            RegisterError(txtusername.Text.Trim());
        }

        private void RegisterError(string MsgError)
        {
            if (MsgError.Length > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowErrorMessage", "alert('" + MsgError.Replace("'", "").Replace("\r\n", "") + "');", true);
            }
        }
    }
}


这篇关于asp.net网络表单中的代码无法访问Asp.net控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆