使用变量asp.net预填充文本框

时间:2010-10-05 15:58:16

标签: asp.net

我想使用以下变量预填充文本字段:

<%=Membership.GetUser().Email%> 

...这样用户就可以将自己的电子邮件地址提交到我的桌面而无需输入。

这是我的代码:

<form id="form1" runat="server">
    <div>
        <!-- This is the code for the form. There is a Text Box to collect the first name, 
            last name and email address. All fields are required and I am validating that the 
            email address is a valid format. -->
        <asp:Table ID="Table1" runat="server">
            <asp:TableRow>
                <asp:TableCell>First Name: <asp:TextBox ID="txtFirstName" 
                    runat="server" Width="60" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                    ErrorMessage="Enter First Name" Text="*" ControlToValidate="txtFirstName" 
                    runat="server" />
                </asp:TableCell>
                <asp:TableCell>Last Name: <asp:TextBox ID="txtLastName" runat="server" Width="60" /> 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="txtLastName" ErrorMessage="Enter Last Name" Text="*" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Email: <asp:TextBox ID="txtEmail" runat="server" Width="80" />
                    <asp:RequiredFieldValidator ID="emailRequired" 
                        runat="server" ControlToValidate="txtEmail" ErrorMessage="Email Needs Information" 
                        Text="*"/>
                    <asp:RegularExpressionValidator ID="emailexpression" 
                        runat="server" ControlToValidate="txtEmail" ValidationExpression=".*@.*\..*" 
                        ErrorMessage="Invalide Email Address" Text="*" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="true" 
                        ShowMessageBox="true" runat="server" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell><asp:Button ID="btnSubmit" runat="server" OnClick="addMember" 
                    CausesValidation="true" Height="30" Width="100" Text="Add Me" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Label ID="lblDuplicate" runat="server" Text=""></asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </div>
</form>

我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

在代码隐藏中的页面加载事件中弹出它?

if (!Page.IsPostBack)
{
    txtEmail.Text = Membership.GetUser().Email;
}