PW2
PW2

Reputation: 791

prepopulate textbox with variable, asp.net

I'd like to prepopulate my textfield with the following variable:

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

... so that users can submit their email address to my table without having to type it in.

Here is my code:

<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>

How can I accomplish this?

Upvotes: 0

Views: 1321

Answers (1)

Nellius
Nellius

Reputation: 3114

Pop it in the page load event in the code-behind?

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

Upvotes: 1

Related Questions