我是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。 有没有人有任何想法?
答案 0 :(得分: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;