C#-在MultiView中查找LoginView控件
本文关键字:LoginView 控件 查找 MultiView C#- | 更新日期: 2023-09-27 18:27:57
如何访问Multiview1
、View2
内部的LoginView1
控件?
我在日志中有一个文本框和下拉列表。在Multiview中View 2的模板中,我需要获得控件并与数据库中的文本绑定。
我尝试过:
((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";
return"Object引用未设置为Object的实例。"
标记码
<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>
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();
}
}
}
请记住,FindControl只搜索当前级别。如果您也想在分支(MultiView、Panel、PlaceHolder等内部)中进行搜索,则必须使用某种递归搜索。
首先在多视图控件中找到控件,并使用multiview.FindControl方法,然后访问找到控件中的子控件
一些样本张贴
http://www.codeproject.com/Articles/13836/Find-child-controls-inside-a-MultiView-control-in