0

我是asp新手。我已经使用asp建立了一个网站。我使用 Artisteer 构建模板。在我尝试创建登录页面之前,一切正常。我会给我的部分代码不要打扰

这是我的 Registration.aspx 页面的一部分

<@ Page Language="C#" MasterPageFile="~/design/MasterPage.master" AutoEventWireup="true"
 CodeFile="Registration.aspx.cs" Inherits="Registration" %>

<@ Import Namespace="Artisteer" %>

<@ Register TagPrefix="artisteer" Namespace="Artisteer" %>

<@ Register TagPrefix="art" TagName="DefaultMenu" Src="DefaultMenu.ascx" %> 

<@ Register TagPrefix="art" TagName="DefaultHeader" Src="DefaultHeader.ascx" %> 

<@ Register TagPrefix="art" TagName="DefaultSidebar1" Src="DefaultSidebar1.ascx" %>


asp:Content ID="PageTitle" ContentPlaceHolderID="TitleContentPlaceHolder" Runat="Server">Once Upon a Cake</asp:Content>

asp:Content ID="MenuContent" ContentPlaceHolderID="MenuContentPlaceHolder" Runat="Server">
    <art:DefaultMenu ID="DefaultMenuContent" runat="server" />
</asp:Content>

asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderContentPlaceHolder" Runat="Server">

art:DefaultHeader ID="DefaultHeader" runat="server" />
</asp:Content>

asp:Content ID="SideBar1" ContentPlaceHolderID="Sidebar1ContentPlaceHolder" Runat="Server">
art:DefaultSidebar1 ID="DefaultSidebar1Content" runat="server" />
</asp:Content>

<asp:Content ID="SheetContent" ContentPlaceHolderID="SheetContentPlaceHolder" Runat="Server">


    <artisteer:article ID="Article2" runat="server"><ContentTemplate>

       <br />

        <table class="art-article" border="0" cellspacing="0" cellpadding="0">
      <tbody>
        <tr>
          <td>Name</td>
          <td>User Name</td>
          <td><asp:TextBox ID="textBoxUser" runat="server" Width="240" MaxLength="15" ></asp:TextBox>
            </td>
          <td>
          <td>

…………

Registration.aspx.cs 文件是

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;


public partial class Registration : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select coun (*) from Register where UserName='" + textBoxUser.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("User Name already Exist!!! </br> Choose another");
            }
        }


    }

问题是 .cs 文件中无法识别 textBoxUser。有人有什么主意吗?

4

1 回答 1

0

请注意,文章(或块)是模板控件。因此,您不能直接从页面引用 ContentTemplate 中的控件。您应该在文章的上下文中找到此控件。

尝试将以下代码添加到 Article 类

public override Control FindControl(string id) 
{ 
EnsureChildControls(); 
if (null != _contentPlaceholder && _contentPlaceholder.Controls[0] is TemplateContainer) 
return _contentPlaceholder.Controls[0].FindControl(id); 

return null; 
} 

然后你可以像这样引用你的控件:

TextBox txt = Article2.FindControl("textBoxUser") as TextBox;

参考:艺人论坛

于 2012-07-03T19:43:56.283 回答