Server controls null in Page_Load - asp.net

I have a page that has some server controls in it. For some reason these controls are null in the page's Page_Load event handler.
<asp:Content ID="mainContent" runat="server" ContentPlaceHolderID="mainPlaceholder">
<asp:Label ID="userIdLockedLabel" runat="server" EnableViewState="False" ForeColor="Red"
Visible="False">
</asp:Label>
<asp:Panel ID="standardLoginPanel" runat="server">
<asp:Login ID="loginControl" runat="server"
DisplayRememberMe="false"
PasswordRecoveryUrl="?action=lostpass"
TextBoxStyle-Font-Names="verdana"
TextBoxStyle-Font-Size="Small"
DestinationPageUrl="Home.aspx"
OnLoggingIn="OnLoggingIn"
OnLoginError="OnError"
TitleText="" >
<TextBoxStyle Font-Names="verdana" Font-Size="Small"></TextBoxStyle>
<LoginButtonStyle CssClass="btn" />
</asp:Login>
</asp:Panel>
<asp:Panel ID="canadaLoginPanel" runat="server" Visible="false">
<asp:Label ID="userFailureLabel" runat="server"></asp:Label>
<table>
<tr>
<td><%= Utility.RetrieveResource("CompanyNumberLabel") %></td>
<td><asp:TextBox ID="companyNumberTextbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><%= Utility.RetrieveResource("UserIdLabel")%></td>
<td><asp:TextBox ID="userIdTextbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><%= Utility.RetrieveResource("PasswordLabel")%></td>
<td><asp:TextBox ID="userPasswordTextbox" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="rememberMeCheckBox" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center"><asp:Button ID="userSubmit" runat="server" CssClass="btn" /></td>
</tr>
<tr>
<td colspan="2" style="text-align:center"><%= Utility.RetrieveResource("ForgotPasswordLinkText") %></td>
</tr>
</table>
</asp:Panel>
<br />
<div style="font-family: Verdana; font-size: small">
<span class="bold">
<%= Utility.RetrieveResource("TroubleLogginIn") %>
</span><br />
<%= Utility.RetrieveResource("ForgotPasswordText") %>
</div>
The page, login.aspx inherits from LogOnBasePage, and that is where the Page_Load code is. In there, I use the following code:
if (this.Company.SiteType == Ceridian.KB.Entities.SiteType.CAN)
{
this.FindControlRecursive("standardLoginPanel").Visible = false;
this.FindControlRecursive("canadaLoginPanel").Visible = true;
((Button)this.FindControlRecursive("userSubmit")).Text = Utility.RetrieveResource("LoginButtonText");
this.Login = null;
CheckBox rememberMe = (CheckBox)this.FindControlRecursive("rememberMeCheckBox");
rememberMe.Text = Utility.RetrieveResource("RememberMeText");
}
Here is the content of the FindControlRecursive method.
public static Control FindControlRecursive(this Control control, string controlId)
{
if (string.Compare(control.ID, controlId, StringComparison.OrdinalIgnoreCase) == 0)
{
// We found the control!
return control;
}
else
{
// Recurse through ctrl's Controls collections
foreach (Control child in control.Controls)
{
Control lookFor = FindControlRecursive(child, controlId);
if (lookFor != null)
{
// We found the control
return lookFor;
}
}
// If we reach here, control was not found
return null;
}
}
I always get a null reference on the first line inside the if check. I don't see how this is possible.

It might be that you need to wait later in the page lifecycle. try putting it in the page_prerendercomplete event like this
Page_PreRenderComplete(Object sender, EventArgs e)
{
if (this.Company.SiteType == Ceridian.KB.Entities.SiteType.CAN)
{
this.FindControlRecursive("standardLoginPanel").Visible = false;
this.FindControlRecursive("canadaLoginPanel").Visible = true;
((Button)this.FindControlRecursive("userSubmit")).Text = Utility.RetrieveResource("LoginButtonText");
this.Login = null;
CheckBox rememberMe = (CheckBox)this.FindControlRecursive("rememberMeCheckBox");
rememberMe.Text = Utility.RetrieveResource("RememberMeText");
}

Does the page you are inheriting from use a markup file (e.g. LogOnBasePage.aspx)?
If so then I think you are out of luck inheriting from it. The markup page generates a class that inherits from the class defined in the code behind page. One of the responsibilities of this generated class is to instantiate the controls defined in the markup. If you inherit from LogOnBasePage your new page will not have the code necessary to instantiate the controls from LogOnBasePage markup and therefore you will get null references.

Related

How can I retrieve the value of a control within an ASP.Net Dynamic Data Custom Field Template?

Long story short, for an old project I used ASP.Net Dynamic Data and probably did a terrible job with it. One of the Field Templates has multiple controls in it, and now I need to get at the value of one control from the FormView's Submit event because we changed the way that value is stored.
I can find the Field Template itself using FindFieldTemplate... but I can't figure out how to get to the controls inside of the template.
How can I do that without re-engineering the whole thing to pull that one field out? It would probably be more correct to re-engineer it, but this is a quick fix for a website that's going to be scrapped in a couple months.
EDIT: Was asked to show code so here it is. The FormView is pretty standard, just uses an . The Field Template actually has it's own listview and I'm controlling it's mode in codebehind. But I need to get the value of txtTitle.
Ticket_TicketMemo.ascx:
<asp:ListView ID="lvTicketMemos" DataSourceID="ldsTicketMemo"
InsertItemPosition="FirstItem" OnLoad="lvTicketMemo_Load" runat="server">
<LayoutTemplate>
<div style="overflow:auto; height:125px; width:600px;">
<table class="ListViewTable" runat="server">
<tr id="itemPlaceHolder" runat="server" />
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<tr valign="top" class='<%# Container.DataItemIndex % 2 == 0 ? "" : "Alternate" %>'>
<td><asp:DynamicControl ID="dcType" DataField="Type" runat="server" /></td>
<td><asp:DynamicControl ID="dcMemo" DataField="Memo" runat="server" /></td>
<td><asp:DynamicControl ID="dcCreateTime" DataField="CreateTime" runat="server" /></td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr valign="top">
<td colspan="3">
<asp:TextBox ID="txtTitle" Width="99%" Visible="false" OnLoad="txtTitle_Load" runat="server" /><br /><br />
</td>
</tr>
<tr valign="top">
<td colspan="3" width="600px">
<asp:TextBox ID="txtMemo" Text='<%# Bind("Memo") %>' Width="99%" OnLoad="txtMemo_Load" TextMode="MultiLine"
Rows="5" runat="server" />
<asp:RequiredFieldValidator ID="rfvMemo" Text="Must enter notes" ControlToValidate="txtMemo" runat="server" />
</td>
</tr>
</InsertItemTemplate>
I have just simulated your problem in my Dynamic Data project. Based on my research (and search) in order to get control value (not from DynamicControl value) in Dynamic Data you should implement the following method (i am using this method in my project and i have found one on Steve blog, i don't remember full link):
/// <summary>
/// Get the control by searching recursively for it.
/// </summary>
/// <param name="Root">The control to start the search at.</param>
/// <param name="Id">The ID of the control to find</param>
/// <returns>The control the was found or NULL if not found</returns>
public static Control FindControlRecursive(this Control Root, string Id)
{
if (Root.ClientID.IndexOf(Id) > 0)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
Now, my EXAMPLE.
First, my custom Insert.aspx page with FormView and EntityDataSource:
<asp:FormView runat="server" ID="FormView1" DataSourceID="DetailsDataSource" DefaultMode="Insert"
OnItemCommand="FormView1_ItemCommand" RenderOuterTable="false">
<InsertItemTemplate>
<table>
<tr valign="top">
<td colspan="3">
<asp:TextBox ID="txtTitle" Width="99%" Visible="true" runat="server" /><br />
<br />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>
<asp:EntityDataSource ID="DetailsDataSource" runat="server" EnableInsert="true" OnInserted="DetailsDataSource_Inserted" />
Then, Inserted event of EntityDataSource:
protected MetaTable table;
protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{
if (e.Exception == null || e.ExceptionHandled)
{
string strTitle = String.Empty;
Control CtlTitle = FormView1.FindControlRecursive("txtTitle");
if (CtlTitle != null)
{
TextBox TextBoxTitle = (TextBox)CtlTitle;
strTitle = TextBoxTitle.Text;
}
Response.Redirect(table.ListActionPath + "?" + "Department_Id=" + strTitle);
}
}
Finally, i enter the text into txtTitle, for example, 13 and then i get

Client Validation Function for Radio Buttons on a ASPX file

This is an ASPX/CS program with Javascript.
I have inherited this code and I am trying to complete the validation which was not completed. I have found the javascript code which validates of a radio button is clicked but the aspx code does not reference it.
Radio buttons are grouped together such that when one in the group is selected, the others are unselected. But, for starters, I cannot see how to utilize this. It must have something to do with the "GroupName" that each radio button mentions.
<ItemTemplate>
<%# FormatGroup(Eval("Group").ToString()) %>
<tr>
<td> <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'><%# Eval("Question") %></asp:Label></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion4" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion3" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion2" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion1" runat="server" GroupName='<%# Eval("Question") %>' /></td>
</tr>
</ItemTemplate>
So how would I make use of the ClientValidationFunction method in asp for radio button groups?
Try a custom field validator.
Add this bit of JavaScript:
<script language="javascript" type="text/javascript" >
function ClientValidate(source,args)
{
if(document.getElementById("<%= rblEvalQuestion1.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion2.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion3.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion4.ClientID %>").checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
Add this validator to the page:
<asp:CustomValidator id="RadioButtonValidator" runat="server" Display="Dynamic" ErrorMessage="Please select an option." ClientValidationFunction="ClientValidate" OnServerValidate="ServerValidate"></asp:CustomValidator>
And this method in the code behind:
protected void ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = rblEvalQuestion1.Checked || rblEvalQuestion2.Checked || rblEvalQuestion3.Checked || rblEvalQuestion4.Checked;
}
Finally, make sure to check if
Page.IsValid
in your button for submitting the form.
Finally, if you can re-factor this into a RadioButtonList you can simply just use a RequiredFieldValidator which will make your life a lot easier to implement!

Load user control dynamically on Button Click

I have a label and a text box which i have added in the user control.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AddMultiLoc.ascx.cs" Inherits="CRM_Streamline_Forms.UserControls.AddMultiLoc" %>
<table>
<tr>
<td style="width:25%">
<asp:Label ID="lblLocName_UC_G0138" runat="server" Text="Location Name:" />
</td>
<td style="width:25%">
<asp:TextBox ID="txtLocName_UC_G0138" runat="server" Width="200px" />
</td>
<td style="width:25%">
<asp:Label ID="lblLocID_UC_G0138" runat="server" Text="Location ID:" />
</td>
<td style="width:25%">
<asp:TextBox ID="txtLocID_UC_G0138" runat="server" Width="200px" />
</td>
</tr>
I have a link button in one of my aspx pages which is when clicked should populate this user control.
<asp:LinkButton ID="lnkAddLoc_AGBI2_G0138" runat="server" Text="+ Add Another Location" onclick="lnkAddLoc_AGBI2_G0138_Click" />
Code behind, I have written this code for the button click:
protected void lnkAddLoc_AGBI2_G0138_Click(object sender, EventArgs e)
{
AddMultiLoc con = (AddMultiLoc)LoadControl("~/UserControls/AddMultiLoc.ascx");
pnlMultiInvoiceInfo1_AGBI2_G0138.Controls.Add(con);
Panel p = new Panel();
Control uc = (Control)Page.LoadControl("~/UserControls/AddMultiLoc.ascx");
p.Controls.Add(uc);
p.Width = 200;
p.Height = 100;
pnlMultiInvoiceInfo1_AGBI2_G0138.Controls.Add(p);
}
First time the user control is populated as am calling it in the aspx page, but second time when i click on the link button, it is not populating the user control for the second time. Am new to coding, please help :(
The problem I think is that you are losing your controls across postback as the controls are loaded in to the ViewState.
Here is a sample where you can load the controls in to the Session which is not really good practice but it will work for your purposes:
AddMultiLocPage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="lnkAddLoc_AGBI2_G0138" runat="server" Text="+ Add Another Location" OnClick="lnkAddLoc_AGBI2_G0138_Click" />
</div>
<asp:PlaceHolder runat="server" ID="Placeholder1"></asp:PlaceHolder>
</form>
</body>
</html>
AddMultiLocPage.aspx.cs
public partial class AddMultiLocPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadControlsFromSession();
}
private List<AddMultiLoc> ViewStateControls
{
get { return (List<AddMultiLoc>) Session["ViewStateControls"]; }
set { Session["ViewStateControls"] = (List<AddMultiLoc>)value; }
}
private void LoadControlsFromSession()
{
if (ViewStateControls != null)
{
Placeholder1.Controls.Clear();
foreach (var c in ViewStateControls)
{
Placeholder1.Controls.Add(c);
}
}
}
protected void lnkAddLoc_AGBI2_G0138_Click(object sender, EventArgs e)
{
var con = (AddMultiLoc)LoadControl("~/AddMultiLoc.ascx");
con.ID = Guid.NewGuid().ToString();
List<AddMultiLoc> tmpList = ViewStateControls;
if(tmpList == null) tmpList = new List<AddMultiLoc>();
tmpList.Add(con);
ViewStateControls = tmpList;
LoadControlsFromSession();
}
}
UserControl Markup
<table>
<tr>
<td style="width: 25%">
<asp:Label ID="lblLocName_UC_G0138" runat="server" Text="Location Name:" />
</td>
<td style="width: 25%">
<asp:TextBox ID="txtLocName_UC_G0138" runat="server" Width="200px" />
</td>
<td style="width: 25%">
<asp:Label ID="lblLocID_UC_G0138" runat="server" Text="Location ID:" />
</td>
<td style="width: 25%">
<asp:TextBox ID="txtLocID_UC_G0138" runat="server" Width="200px" />
</td>
</tr>
</table>
No need to store control in session, Solution to your problem is :
ASPX Code :
< asp:LinkButton ID="lnkAddLoc_AGBI2_G0138" runat="server" Text="+ Add Another Location" onclick="lnkAddLoc_AGBI2_G0138_Click" />
<asp:Panel ID="Panel1" runat="server">
Code Behind :
protected void lnkAddLoc_AGBI2_G0138_Click(object sender, EventArgs e)
{
Control uc = (Control)Page.LoadControl("~/AddMultiLoc.ascx");
Panel1.Controls.Add(uc);
}
Any time you click on link button lnkAddLoc_AGBI2_G0138 it add new instance of User Control to Panel1.

ASP:Labels not updating on button Click

I know this is probably something so simple that I am just not able to see it. I have an aspx form that has a usercontrol in an updata panel. The user control is a people picker the searches on users from a corporate database.
What I want to see happen is:
The user clicks on a pick user button
The update panel with people picker becomes visible
They search for a user and then select the one they want.
When they make the selection and click Done, I get the user id of the user and look them up in our user table.
The user information should show up in a the form in label fields.
I can step through the code and see that I am getting the user information and that the label text is being set to the values but the page never updates the labels. It is a postback so I would think they would update.
<tr>
<td colspan="4">
<asp:UpdatePanel ID="CollapseDelegate" runat="server">
<ContentTemplate>
<asp:Panel ID="pDelegateHeader" runat="server">
<div style="padding: 10px 0 10px 20px; height:10px; text-align: left; background-color:#ffffff; color:#000000; border: 2px solid #ccc;" >
<asp:Label ID="ShowDelegate" runat="server" /><br />
</div>
</asp:Panel>
<asp:Panel ID="pDelegateBody" runat="server">
<PP:PeoplePicker ID="PP" runat="server" />
<asp:Button ID="btnOk" runat="server" Text="Done" CssClass="Buttons" onclick="btnOk_Click" />
</asp:Panel>
<asp:CollapsiblePanelExtender ID="CollapsiblePanelExtender3" runat="server" TargetControlID="pDelegateBody" CollapseControlID="pDelegateHeader" ExpandControlID="pDelegateHeader" Collapsed="true" TextLabelID="ShowDelegate" CollapsedText="Pick User..." ExpandedText="Close..." CollapsedSize="0"></asp:CollapsiblePanelExtender>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td><asp:Label ID="DelegateNameLabel" runat="server" Text="Name:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegateNameValueLabel" runat="server" CssClass="indentedText" Visible="true"></asp:Label></td>
<td><asp:Label ID="DelegateEmailLabel" runat="server" Text="Email:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegateEmailValueLabel" runat="server" CssClass="indentedText" Visible="true"></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="DelegatePhoneLabel" runat="server" Text="Phone:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegatePhoneValueLabel" runat="server" CssClass="indentedText" Visible="true"></asp:Label></td>
<td><asp:Label ID="DelegateVerifiedLabel" runat="server" Text="Last Verified Date:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegateVerifiedValueLabel" runat="server" CssClass="indentedText" /></td>
</tr>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string PassedDelegateID = string.Empty;
string Mode = string.Empty;
int delegateId = 0;
if (Request.QueryString["Id"] != null)
{
PassedDelegateID = Request.QueryString["Id"].ToString();
}
else
{
PassedDelegateID = "0";
}
if (Request.QueryString["mode"] != null)
{
Mode = Request.QueryString["mode"].ToString();
}
else
{
Mode = "add";
}
if (Mode == "add")
{
pnlUdpateDelegateText.Text = UIConstants.ADDDELEGATETEXT.ToString();
}
else
{
pnlUdpateDelegateText.Text = UIConstants.UPDATEDELEGATETEXT.ToString();
if (int.TryParse(PassedDelegateID, out delegateId))
{
loadDelegateData(delegateId);
}
}
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
TextBox txtSearchValue = (TextBox)PP.FindControl("txtSearchResults");
string LanId = txtSearchValue.Text;
User user = BusinessUtility.GetUser(LanId);
DelegateNameValueLabel.Text = user.Name;
DelegateEmailValueLabel.Text = user.Email;
DelegatePhoneValueLabel.Text = user.Phone;
DelegateVerifiedValueLabel.Text = DateTime.Now.ToShortDateString();
}
Thanks,
Rhonda
Because the labels are outside the update panel, only the content inside the update panel is updated from an ajax post-back, that's the whole point of an update panel.
You will need to either move the labels inside the update panel's content area, or have another update panel for the labels and make it's update mode "always"
Your lables are outside of the UpdatePanel.
Under the hood, ASP.Net performes a full postback, but only the part that pertains to your UpdatePanel is transfered back down to the client. Some JavaScript then takes this bit of HTML and replaces the existing <div> element that is your UpdatePanel.
Since your labels are outside of that <div> they never get updates.

Getting the password value from a Login control in ASP.net page

I've got my login control in my site Master page:
<AnonymousTemplate>
<asp:Login runat="server" OnLoggedIn="Login1_LoggedIn" CssClass="LoginForm" />
</AnonymousTemplate>
In the code-behind page of this Master page I am trying to capture the value entered in the Password form field. The code works in the homepage, but doesn't work in all other pages!
The code used is:
Page page = (Page)HttpContext.Current.Handler;
TextBox tbtemp = (TextBox)page.FindControl("Password");
_password = tbtemp.ToString();
On the homepage, looking at the trace the value of the Text box is:
ctl00$LoginView1$ctl01$Password
On the other pages the value is:
ctl00$ctl00$LoginView1$ctl01$Password
The error that is thrown on the non-homepage pages is:
due to Exception of type
'System.Web.HttpUnhandledException'
was thrown.
Any ideas how to access the value?
Update:
My login form looks like this:
<asp:loginview id="LoginView1" runat="server">
<LoggedInTemplate >
<asp:LoginStatus ID="LoginStatus1" runat="server" OnLoggedOut="LoginStatus1_LoggedOut" /> <%--Displays the text logged in--%>
<asp:LoginName ID="LoginName1" runat="server" /> <%--displays the username--%>
</LoggedInTemplate>
<AnonymousTemplate>
<asp:Login RememberMeSet="true" ID="loginForm" runat="server" OnLoggedIn="Login1_LoggedIn" CssClass="LoginForm" >
<LayoutTemplate>
<table>
<tr>
<td><asp:Label ID="UserNameLabel" runat="server">Username:</asp:Label></td>
<td><asp:TextBox ID="UserName" runat="server" /></td>
</tr>
<tr>
<td><asp:Label ID="PasswordLabel" runat="server" >Password:</asp:Label></td>
<td><asp:TextBox ID="Password" runat="server" TextMode="Password" /></td>
</tr>
<tr>
<td><asp:Label ID="RememberMeLabel" runat="server" >Remember me: </asp:Label></td>
<td><asp:CheckBox ID="RememberMe" runat="server" /></td>
</tr>
<tr>
<td> </td>
<td> <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" /></td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
</AnonymousTemplate>
</asp:loginview>
Why are you getting the password control explicitly? Did you try just getting the password directly from
string password = LoginCtrl.Password //Assuming LoginCtrl is the Id of your control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.aspx
string username = ((Login)this.LoginView.FindControl("LoginControl")).UserName;
// Search recursively a control sub-tree for a specific control.
// It searches every control in the sub-tree, so it potentially
// could be optimized to search only, say, INamingContainers.
public Control FindControlRecursive(Control root, string id)
{
if (root.ID == id) return root;
foreach (Control c in root.Controls)
{
var ctlFound = FindControlRecursive(c, id);
if (((ctlFound != null))) return ctlFound;
}
return null;
}
public T FindControl<T>(string id) where T : Control
{
return FindControl<T>(Page, id);
}
public static T FindControl<T>(Control startingControl, string id) where T : Control
{
T found = null;
foreach (Control activeControl in startingControl.Controls)
{
found = activeControl as T;
if (found == null)
{
found = FindControl<T>(activeControl, id);
}
else if (string.Compare(id, found.ID, true) != 0)
{
found = null;
}
if (found != null)
{
break;
}
}
return found;
}
}

Resources