您必须将<%# Bind("") %>语法从用户控件 (ASCX) 移动到包含页面 (ASPX) 并使用用户控件中的属性来获取和设置表单值。我下面的示例改编自http://oudinia.blogspot.co.uk/2007/12/aspnet-20-and-up-c-user-control-within.html给出的解决方案,并提供了更多详细信息以供尝试使其现实:
ASCX 加价:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PaperForm.ascx.cs" Inherits="PaperForm" %>
<asp:ValidationSummary runat="server" HeaderText="<b>Please supply the missing information below.</b>" />
<p class="SectionHeading">Section A: Your Details</p>
<table border="1" width="100%">
<tr>
<td width="220px">Membership number</td>
<td colspan="3"><asp:TextBox ID="txtMemNo" runat="server" MaxLength="9"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtMemNo" Display="Dynamic" Text="Please check your membership number" ValidationExpression="\d{9}"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFirstName" runat="server">First name</asp:Label></td>
<td><asp:TextBox ID="txtForename" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtForename" Text="Required"></asp:RequiredFieldValidator>
</td>
<td width="110px"><asp:Label ID="lblLastName" runat="server">Last name</asp:Label></td>
<td><asp:TextBox ID="txtSurname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtSurname" Text="Required"></asp:RequiredFieldValidator>
</td>
</tr>
...
</table>
<script type="text/javascript" language="javascript">
// perform additional client-side validation in JavaScript
function ValidateForm()
{
// check membership number (via web-service)
...
}
</script>
ASCX 代码隐藏:
public partial class PaperForm : UserControl
{
// **public properties representing form data**
public string MemNo { get { return txtMemNo.Text; } set { txtMemNo.Text = value; } }
public string Forename { get { return txtForename.Text; } set { txtForename.Text = value; } }
public string Surname { get { return txtSurname.Text; } set { txtSurname.Text = value; } }
...
protected void Page_Load(object sender, EventArgs e)
{
// prevent browser caching
...
}
}
ASPX 标记:
<%--register the user control here--%>
<%@ Register TagPrefix="UC" TagName="PaperForm" Src="~/Proposals/PaperForm.ascx" %>
<asp:FormView ID="fvPaper" runat="server" DataKeyNames="PprPropID" DataSourceID="tblPprProp" DefaultMode="Insert" Width="780px">
<InsertItemTemplate>
<%--insert the user control here, **and bind the database fields to its properties**--%>
<UC:PaperForm ID="pf" runat="server" MemNo='<%# Bind("MemNo") %>' Forename='<%# Bind("Forename") %>' Surname='<%# Bind("Surname") %>' ... />
...
<%--can use the JavaScript ValidateForm() function defined in the ASCX file--%>
<asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Submit" BackColor="#C2D9EC" OnClientClick="return ValidateForm();" />
</InsertItemTemplate>
</asp:FormView>
<%--define the data-source here rather than in the ASCX file--%>
<asp:SqlDataSource ID="tblPprProp" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:confConnectionString %>" OnInserted="AfterInsertion"
InsertCommand="INSERT INTO [tblPprProp] ([MemNo], [Surname], [Forename], ...) VALUES (@MemNo, @Surname, @Forename, ...); SELECT @RowID = SCOPE_IDENTITY()"
OldValuesParameterFormatString="original_{0}">
<InsertParameters>
<asp:Parameter Name="MemNo" Type="String" />
<asp:Parameter Name="Surname" Type="String" />
<asp:Parameter Name="Forename" Type="String" />
...
</InsertParameters>
</asp:SqlDataSource>
ASPX 代码隐藏:
public partial class PaperProposal : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AfterInsertion(object sender, SqlDataSourceStatusEventArgs e)
{
// get Proposal ID
string rowId = e.Command.Parameters["@RowID"].Value.ToString();
// get e-mail address from ASCX property
string address = ((PaperForm)this.fvPaper.FindControl("pf")).Email;
// send acknowledgement e-mail
...
}
}