0

如何访问LoginView1控制里面Multiview1View2

我在Multiview 中的View 2 的loggedIn 模板中有一些文本框和下拉列表,我需要获取控件并与数据库中的文本绑定。

我试过:

((TextBox)this.LoginView1.FindControl("custEmail")).Text = dr.GetValue(1).ToString();
((TextBox)LoginView1.FindControl("custEmail")).Text = dr.GetValue(1).ToString();
((TextBox)LoginView1.FindControl("custEmail")).Text = "xxxx";

返回“对象引用未设置为对象的实例。”

标记代码

<asp:View ID="View2" runat="server" OnActivate="login_Click">
                <asp:Label ID="tprice" runat="server" /><br />
                <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled">
                            <AnonymousTemplate>
                                Please login to continue.<br />
                                <asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
                                    <LayoutTemplate>
                                        <fieldset>
                                            <legend>Log in Form</legend>
                                            <ul>
                                                <li>
                                                    <asp:Label ID="Label2" runat="server" AssociatedControlID="UserName">User name</asp:Label>
                                                    <asp:TextBox runat="server" ID="UserName" />
                                                    <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
                                                        CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                                                </li>
                                                <li>
                                                    <asp:Label ID="Label4" runat="server" AssociatedControlID="Password">Password</asp:Label>
                                                    <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                                                    <asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                                                        CssClass="field-validation-error" ErrorMessage="The password field is required." />
                                                </li>
                                            </ul>
                                            <asp:Button ID="login" runat="server" CommandName="Login" OnCommand="login_Click"
                                                Text="Log in"
                                                CssClass="btn btn-info" />
                                        </fieldset>
                                        <asp:Label runat="server" ID="FailureText" class="validation-summary-errors" />
                                    </LayoutTemplate>
                                </asp:Login>
                                or
                        <a id="registerLink" runat="server" href="~/Account/Register">Register</a>&nbsp;
                        to continue
                            </AnonymousTemplate>
                            <LoggedInTemplate>
                                <table class="custable">
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label5" AssociatedControlID="title" runat="server" Text="Title : " />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="title" runat="server">
                                                <asp:ListItem Value="0">Select One...</asp:ListItem>
                                                <asp:ListItem Value="Mr">Mr</asp:ListItem>
                                                <asp:ListItem Value="Mrs">Mrs</asp:ListItem>
                                                <asp:ListItem Value="Miss">Miss</asp:ListItem>
                                                <asp:ListItem Value="Ms">Ms</asp:ListItem>
                                                <asp:ListItem Value="Doctor">Doctor</asp:ListItem>
                                            </asp:DropDownList>
                                        </td>
                                        <td>
                                            <asp:CompareValidator ID="CompareValidator1" runat="server"
                                                ControlToValidate="title" ValueToCompare="0" Operator="NotEqual"
                                                Text="Please select the title." CssClass="field-validation-error"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label6" AssociatedControlID="name" runat="server" Text="Full Name As Per IC : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="name" runat="server" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="name"
                                                CssClass="field-validation-error" Text="*Required"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label7" AssociatedControlID="custEmail" runat="server" Text="Email : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="custEmail" runat="server" TextMode="Email" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="custEmail"
                                                CssClass="field-validation-error" Text="*"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label8" AssociatedControlID="email2" runat="server" Text="Confirm Email : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="email2" runat="server" TextMode="Email" />
                                        </td>
                                        <td>
                                            <asp:CompareValidator ID="CompareValidator2" ControlToCompare="email2" ControlToValidate="custEmail"
                                                runat="server"
                                                Operator="Equal" Type="String" Text="Please enter same Email." CssClass="field-validation-error"
                                                ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label9" AssociatedControlID="icPassport" runat="server" Text="IC / Passport : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="icPassport" runat="server" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="icPassport"
                                                CssClass="field-validation-error" Text="*"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label10" AssociatedControlID="mobile" runat="server" Text="Mobile : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="mobile" runat="server" placeholder="60123456789" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="mobile"
                                                CssClass="field-validation-error" Text="*"
                                                SetFocusOnError="true" ValidationGroup="vg1" /><br />
                                            <asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="mobile"
                                                Operator="DataTypeCheck" Type="Double" ErrorMessage="Mobile number must be integer."
                                                CssClass="field-validation-error" ValidationGroup="vg1" Display="None" /><br />
                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="mobile"
                                                ErrorMessage="Mobile Number must within 10 to 18 digits." ValidationExpression="^\d{10,18}$"
                                                CssClass="field-validation-error" ValidationGroup="vg1" Display="None" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label11" AssociatedControlID="street" runat="server" Text="Street : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="street" runat="server" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="street"
                                                CssClass="field-validation-error" Text="*"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label12" AssociatedControlID="townstate" runat="server" Text="Town / State : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="townstate" runat="server" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="townstate"
                                                CssClass="field-validation-error" Text="*"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label13" AssociatedControlID="country" runat="server" Text="Country : " />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="country" runat="server" />
                                        </td>
                                        <td>
                                            <asp:CompareValidator ID="CompareValidator4" runat="server"
                                                ControlToValidate="country" ValueToCompare="Select One..." Type="String" Operator="NotEqual"
                                                Text="*" CssClass="field-validation-error"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label14" AssociatedControlID="postalcode" runat="server" Text="Postal Code : " />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="postalcode" runat="server" />
                                        </td>
                                        <td>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="postalcode"
                                                CssClass="field-validation-error" Text="*"
                                                SetFocusOnError="true" ValidationGroup="vg1" /><br />
                                            <asp:CompareValidator ID="CompareValidator5" runat="server" ControlToValidate="postalcode"
                                                Type="Integer" Operator="DataTypeCheck" ErrorMessage="Postal Code must be integer."
                                                CssClass="field-validation-error" ValidationGroup="vg1" Display="none" /><br />
                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="postalcode"
                                                ErrorMessage="Postal Code, 5 or 9 Digits." ValidationExpression="\d{5}?(\d{4})?$"
                                                CssClass="field-validation-error" ValidationGroup="vg1" Display="None" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="Label15" AssociatedControlID="yearOfBirth" runat="server" Text="Year Of Birth : " />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="yearOfBirth" runat="server">
                                                <asp:ListItem Value="0">Year</asp:ListItem>
                                            </asp:DropDownList>
                                        </td>
                                        <td>
                                            <asp:CompareValidator ID="CompareValidator6" runat="server"
                                                ControlToValidate="yearOfBirth" ValueToCompare="0" Operator="NotEqual"
                                                Text="*" CssClass="field-validation-error"
                                                SetFocusOnError="true" ValidationGroup="vg1" />
                                        </td>
                                    </tr>
                                </table>
                            </LoggedInTemplate>
                        </asp:LoginView>
                    </ContentTemplate>
                </asp:UpdatePanel>
                <asp:Button ID="Button1" Text="Next" CommandName="NextView" runat="server" />
            </asp:View>

C# 代码背后

protected void login_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View2);

        if (User.Identity.IsAuthenticated)
        {
            string userId = (Membership.GetUser().ProviderUserKey).ToString();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            string selectcmd = "select Email from Memberships where userID='" + userId + "'";
            string selectcmd2 = "select custEmail, title, name, icPassport, mobile, street, townstate, postalcode, townstate, yearOfBirth from custInfo where userID='" + userId + "'";

            SqlDataReader dr;
            cmd = new SqlCommand(selectcmd2, con);
            con.Open();
            dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {

                while (dr.Read())
                {
                    ((TextBox)this.LoginView1.FindControl("custEmail")).Text = dr.GetValue(1).ToString();
                    ((DropDownList)LoginView1.FindControl("title")).SelectedValue = dr.GetValue(2).ToString();
                    ((TextBox)LoginView1.FindControl("name")).Text = dr.GetValue(3).ToString();
                    ((TextBox)LoginView1.FindControl("icPassport")).Text = dr.GetValue(4).ToString();
                    ((TextBox)LoginView1.FindControl("mobile")).Text = dr.GetValue(5).ToString();
                    ((TextBox)LoginView1.FindControl("street")).Text = dr.GetValue(6).ToString();
                    ((DropDownList)LoginView1.FindControl("townstate")).SelectedValue = dr.GetValue(7).ToString();
                    ((TextBox)LoginView1.FindControl("postalcode")).Text = dr.GetValue(8).ToString().Trim();
                    ((TextBox)LoginView1.FindControl("townstate")).Text = dr.GetValue(9).ToString();
                    ((DropDownList)LoginView1.FindControl("yearOfBirth")).SelectedValue = dr.GetValue(10).ToString();
                }
                dr.Close();
                con.Close();
            }

            else
            {
                dr.Close();
                cmd = new SqlCommand(selectcmd, con);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    (this.LoginView1.FindControl("custEmail") as TextBox).Text = dr.GetValue(0).ToString();
                }
                dr.Close();
                con.Close();
            }
        }
    }
4

1 回答 1

0

请记住,FindControl 只搜索当前级别 如果您还想在分支中搜索(在 MultiView、Panel、PlaceHolder 等内部),那么您必须使用某种递归搜索。

首先在多视图控件中找到控件并使用 MultiView.FindControl 方法,然后访问找到的控件中的子控件

一些示例帖子

http://www.codeproject.com/Articles/13836/Find-child-controls-inside-a-MultiView-control-in

于 2013-08-17T11:34:00.587 回答