Cannot find control with FindControl [duplicate] - asp.net

This question already has answers here:
find a control in current page
(3 answers)
Closed 7 years ago.
I want to find the control ddlMaster.
Here is the excerpt aspx rah_sync_output.aspx in question:
<%# Page Language="vb" MasterPageFile="~/admin/Admin.master" AutoEventWireup="false" CodeBehind="rah_sync_output.aspx.vb" Inherits="TCDS.Web.admin.RahSyncOutput" EnableEventValidation="false" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick = "ExportToExcel" /><br />
Locations
<asp:DropDownList ID="ddlMaster" runat="server" OnSelectedIndexChanged = "FilterChanged" AutoPostBack = "true" AppendDataBoundItems = "true">
</asp:DropDownList>
</asp:Content>
Here is the masterfile /admin/Admin.master excerpt:
<%# Master Language="VB" Inherits="XXX.Web.admin.AdminAdmin" Codebehind="Admin.master.vb" MasterPageFile="~/fullPage.master" %>
<asp:Content ID="headerContent" ContentPlaceHolderID="headContent" runat="server">
<asp:contentplaceholder id="head" runat="server">
</asp:contentplaceholder>
</asp:Content>
<asp:Content ID="menuContent" ContentPlaceHolderID="tcdsMenu" runat="server">
<ul class="ulMenu" style="">
<asp:Literal ID="ltrMenu" Text='' runat="server" />
</ul>
</asp:Content>
<asp:Content ID="tcdsContent" ContentPlaceHolderID="tcdsContent" runat="server">
<div id="content">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</asp:Content>
Here is the masterfile's masterfile fullPage.master excerpt:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="box">
<div id="divHeader" runat="server">
<asp:Literal ID="ltrAgencyLogo" runat="server" />
<asp:Literal ID="ltrAgencyName" runat="server" />
<a id="xxxicon" class="icnMCLLC" href="http://www.hidden.com" target="mcllc" runat="server">
<img src="/tcds/images/xxx.gif" border="0" alt="Click for technical assistance" width="110" height="36" />
</a>
<div class="bannerIcon"><img src="/tcds/images/icnHelp.gif" border="0" alt="TCDS Help" style='width: 24px;height: 24px;' /></div>
<span class="header"><asp:Literal ID="ltrHeader" runat="server" /></span>
<asp:contentplaceholder id="tcdsMenu" runat="server">
</asp:contentplaceholder>
</div>
<div id="content">
<asp:contentplaceholder id="tcdsContent" runat="server">
</asp:contentplaceholder>
</div>
</div>
</form>
My attempt in rah_sync_output.aspx.vb:
Dim ddl = DirectCast(Master.FindControl("tcdsContent").FindControl("ContentPlaceHolder1").FindControl("ddl" + columnName), DropDownList)
where columnName is "Master" at runtime.
Result is null. I either misunderstand that Master refers to the current master or the master's master. All I want to do is reference controls inside ContentPlaceHolderID="ContentPlaceHolder1"

Master.FindControl("tcdsContent").FindControl("ContentPlaceHolder1").FindControl("ddl" + columnName)
That's one way to do it, but you're probably missing a naming container somewhere in the control hierarchy. It would be easier to find the control recursively.
/// <summary>
/// recursively finds a child control of the specified parent.
/// </summary>
/// <param name="control"></param>
/// <param name="id"></param>
/// <returns></returns>
public static Control FindControlRecursive(this Control control, string id)
{
if (control == null) return null;
//try to find the control at the current level
Control ctrl = control.FindControl(id);
if (ctrl == null)
{
//search the children
foreach (Control child in control.Controls)
{
ctrl = FindControlRecursive(child, id);
if (ctrl != null) break;
}
}
return ctrl;
}

Related

asp:button onclick event causing Internal Error

MasterPage:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
ASPX page:
<%# Page Title="" Language="C#" MasterPageFile="~/NewFMaster.master" AutoEventWireup="true" CodeFile="Home17.aspx.cs" Inherits="Home17" %>
....
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
....
<form id="form1" runat="server">
<div class="form-group">
<div class="col-sm-6">
<asp:TextBox ID="txtFullName" runat="server" CssClass="name" placeholder="Full Name"></asp:TextBox>
</div>
<div class="col-sm-6">
<asp:TextBox ID="txtContact" runat="server" MaxLength="12" CssClass="name" placeholder="Contact Number" />
</div>
</div>
<asp:Button ID="submitButton" runat="server" CssClass="submit-btn" style="width:150px" Text="Submit" OnClick="submitButton_Click"/>
</form>
....
</asp:Content>
CodeBehind:
public partial class Home17 : System.Web.UI.MasterPage
{
....
protected void submitButton_Click(object sender, EventArgs e)
{
Response.Redirect("Home16.aspx");
}
If I remove OnClick="submitButton_Click" from asp:Button line, then the page atleast displays, otherwise I get "Internal Server error"
Web.Config: CustomerErrors are off & have tried with both true/false for debug
UPDATE 24 Apr 2018:
Now Internal Server Error is shown on all pages. Entire site has gone done.
In the error Log last error is:
(I have added the new keys)
<Error>
<Message>Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster,
ensure that <machineKey> configuration specifies the same validationKey and validation algorithm.
AutoGenerate cannot be used in a cluster.
http://go.microsoft.com/fwlink/?LinkID=314055</Message>
<PageName>http://www.***.*******.com/Product.aspx?productID=69584&productID=69584</PageName>
<Date>4/23/2018 8:58:44 PM</Date>
<UTC>4/24/2018 3:58:44 AM</UTC>
</Error>
Note: The hosting provider is BigRock. (have asked them for help but they simply said I'll have to resolve it, its not their issue)

Want to call .ascx file from master page upon button click in asp.net web form application

I have created .Master, .aspx and .ascx pages. I want to call .ascx page upon click on button in .master page. If the button is not clicked then .ascx should not show up.
Currently, Onload of page, .ascx page is calling because i have used <uc1:Account runat="server" ID="Account" />. But i want after click of button not on page load.
Any help is highly appreciated. Thank you in advance.
My master page looks like this:
<%# Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Staff.master.cs" Inherits="Admin_Staff" %>
<%# Register Src="~/Controls/Account.ascx" TagPrefix="uc1" TagName="Account" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h4>Account</h4>
<span class="input-group-btn">
<input type="text" class=" search-query form-control" placeholder="Search" />
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</span>
<div class="col-lg-9">
<uc1:Account runat="server" ID="Account" />
</div>
</asp:Content>
My User Control looks like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Acc.ascx.cs" Inherits="Admin_Controls_Account" %>
<asp:panel id="pnlAcc" runat="server">
<section id="AccForm">
<asp:PlaceHolder runat="server" ID="PlaceHolder1" Visible="false">
</asp:PlaceHolder>
<div class="form-group">
<asp:Label runat="server" CssClass="col-md-2 control-label">Country: </asp:Label>
<div class="col-md-10">
<asp:Label runat="server" CssClass="col-md control-label" >New Zealand</asp:Label>
</div>
</div>
</section>
</asp:panel>
My .aspx page
<%# Page Title="" Language="C#" MasterPageFile="~/Admin/Staff.master" AutoEventWireup="true" CodeFile="Staff.aspx.cs" Inherits="Admin_Staff" %>
<asp:Content ID="Content1" ContentPlaceHolderID="StaffContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent1" Runat="Server">
</asp:Content>
You can set the Visibility of the Control to false and change it on Button Click
<uc1:Account runat="server" ID="Account" Visible="false" />
And then on button click
protected void Button1_Click(object sender, EventArgs e)
{
Account.Visible = true;
}
Or you can add the Controls dynamically
protected void Button1_Click(object sender, EventArgs e)
{
Admin_Controls_Account account = (Admin_Controls_Account)LoadControl("~/Controls/Account.ascx");
PlaceHolder1.Controls.Add(account);
}
Note that for the last option you will have to reload the control each time there is a PostBack so you will have to store the Visibility yourself and recreate the control each time the page is loaded.
Simple way is to make the control invisible on the master page.
<uc1:Account runat="server" ID="Account" Visible="False" />
Make it visible on button click.

jQuery errorContainer practice

I'm trying to be able to place the error message when using jQuery validation to a asp.net label if the textbox is empty.
please advice how to modify my code to get that!!
here is my code:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#aspnetForm").validate({
errorContainer: "#<%=TextBox1 %>",
errorLabelContainer: "#<%=TextBox1 %> #<%=Label1 %>",
wrapper: "li", debug: true,
submitHandler: function() { alert("Submitted!") }
})
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<p style="height: 313px">
<asp:TextBox ID="TextBox1" runat="server" class="required"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>
</p>
</asp:Content>
I think
"#<%=TextBox1 %>"
means write the value of the default property of the textbox, which is the .Text property.
You should use
"#<%=TextBox1.ID %>"
and
"#<%=TextBox1.ID %> #<%=Label1.ID %>"
No to disqualify what you are doing now but I can't wrap my head around why you add a new plugin like JQ validation when you have a simple case of a required field validator which is a built in .net control

Filling in parent master page from child

I'd like to have a page that uses a child master page, fill in a content placeholder of the parent, but I cannot get it to work. Whenever I try I get the error "Cannot find ContentPlaceHolder 'customHead' in the master page '/templates/info.master', verify content control's ContentPlaceHolderID attribute in the content page."
I have a master page (/templates/main.master) defined like this:
<%# Master Language="C#" %>
<head runat="server">
<title>foo</title>
<asp:contentplaceholder runat="server" id="customHead" />
</head>
<body>
<div id="content">
<asp:contentplaceholder runat="server" id="masterContent" />
</div>
Then I have a child master (/templates/info.master) defined like this:
<%# Master Language="C#" MasterPageFile="/templates/main.master" %>
<asp:content id="homeContent" contentPlaceHolderId="masterContent" Runat="server">
<div id="info-container">
<div id="info-content">
<asp:contentplaceholder runat="server" id="infoContent"/>
</div>
</div>
</asp:content>
And finally my page defined like this:
<%# Page Language="C#" MasterPageFile="/templates/info.master" %>
<asp:Content ID="head" ContentPlaceHolderID="customHead" runat="server">
<!-- Custom header area -->
<link rel="stylesheet" type="text/css" href="foo.css"/>
</asp:Content>
<asp:Content ID="content" ContentPlaceHolderID="childContent" runat="server">
This is my child content
</asp:Content>
You didn't define a "customeHead" in your child master page. If you want to expose the root master pages content area, you'll need to expose it in the child master page.
<%# Master Language="C#" MasterPageFile="/templates/main.master" %>
<asp:contentplaceholder runat="server" id="customHead" />
<asp:content id="homeContent" contentPlaceHolderId="masterContent" Runat="server">
<div id="info-container">
<div id="info-content">
<asp:contentplaceholder runat="server" id="infoContent"/>
</div>
</div>
</asp:content>
Are you setting it using this.Page.Master ?

Giving a custom UserControl an ID in rendered HTML

When I use a ASP:Calendar control, and give it an ID:
<asp:Calendar runat="server" ID="MyCal" />
It looks like this in the rendered html:
<table id="NameMangled_MyCal"... />
And I can access the element by ID in javascript like this:
var cal= document.getElementById("<%= MyCal.ClientID%>")
However, When I make a custom user control that has-a calendar:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WeeklyEventsCalendar.ascx.cs"
Inherits="WeeklyEventsCalendar" %>
<div>
<asp:Calendar runat="server" ID="InnerCal" Width="100%" OnDayRender="RenderCell" OnVisibleMonthChanged="ChangeMonth" Height="480px" />
</div>
And then give it an ID when I add it to my page...
<mvs:WeeklyEventsCalendar ID="WeeklyCal" runat="server" />
That ID doesn't show up anywhere in the rendered HTML. all I get is
<div> stuff </div>
When I want
<div id="NameMangled_WeeklyCal"> stuff <div>
What am I doing wrong?
UserControls only render their contents, nothing else. What you could do is
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WeeklyEventsCalendar.ascx.cs"
Inherits="WeeklyEventsCalendar" %>
<div id="<%= this.ControlID %>">
<asp:Calendar runat="server" ID="InnerCal" Width="100%" OnDayRender="RenderCell" OnVisibleMonthChanged="ChangeMonth" Height="480px" />
</div>

Resources