There is a User Control - ChildUC.ascx with Property "FirstName"
ChildUC.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ChildUC.ascx.cs" Inherits="SampleDevExpress14._2.UserControls.ChildUC" %>
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
<asp:Button runat="server" ID="btnClick" Text="Click" OnClick="btnClick_Click"/>
ChildUC.ascx.cs -
This got a property FirstName
public partial class ChildUC : System.Web.UI.UserControl
{
public string FirstName { get; set; }
}
This ChildUC.ascx user control used in another user control ParentUC.ascx-
ParentUC.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ParentUC.ascx.cs" Inherits="SampleDevExpress14._2.UserControls.ParentUC" %>
<%# Register src="ChildUC.ascx" tagPrefix="uc1" tagName="ChildUC" %>
<uc1:ChildUC runat="server" ID="ucChildUC"/>
The ParentUC.ascx used in a ParentPage.aspx
ParentPage.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ParentPage.aspx.cs" Inherits="SampleDevExpress14._2.UserControls.ParentPage" %>
<%#Register src="ParentUC.ascx" tagPrefix="uc1" tagName="ParentUC" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<div>
<uc1:ParentUC runat="server" ID="ucParentUC"/>
</div>
</form>
</body>
</html>
ParentPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
UserControl ucChild = (UserControl)ucParentUC.FindControl("ucChildUC");
TextBox txtNameOfChildUC = (TextBox)ucChild.FindControl("txtName");
txtNameOfChildUC.Text = "From Parent Page";
}
I am able to find the textbox control of ChildUC.ascx in ParentPage.aspx which i showed above in Page_Load event
But how can I find the FirstName Property of ChildUC.ascx in ParentPage.aspx and set a value to it.
Thank you.
Trying using dynamic,
dynamic u = (UserControl)ParentControl.FindControl("childcontrol");
u.firstname = "fawad";
Response.Write(u.firstname);
Related
i have a view in .aspx
the SIGAMVC.Models.Detalle
is generated by the framework, I should never touch it
a solution that I found and it does not work here
VIEW
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SIGAMVC.Models.Detalle>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h4><b><%:Session["TEST"] %></b></h4> <%-- i tried por session but not found --%>
<h1><%=MyValue%></h1> <%-- too --%>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MenuContent" runat="server">
</asp:Content>
CONTROLLER
protected string MyValue { get; set; }
[HttpPost]
public ActionResult Create(Detalle detalle, int id)
{
Session["TEST"] = "RODRIGO ALEX"; //first tried
this.MyValue = "Some Value"; // second tried
return View(detalle);
}
Take a look at using ViewData and ViewBag.
ViewBag.detalle = detalle;
Alternatively:
ViewData["detalle"] = detalle;
For more information on when to use which one this blog post is a great reference.
I have a property x in my code-behind file in a web application project. It gives me compile time error while it does not give any error in website project
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="test.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= x2 %>
<%# x2 %>
</div>
</form>
</body>
</html>
Codebehind file :
using System;
namespace test
{public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
It is working fine when I use the same technique in my website project
Kindly help
Thanks
Check if there are any other errors in your code behind WebForm1.aspx.cs.
like missing reference or any other compile error.
This works for me. Are you sure there's a value in ViewState? On form load add a value to test.
ViewState["FormMode"] = "ViewApplicant";
lblMessage.DataBind();
Also make sure the label has some text to view. It could be showing but is empty.
<asp:Label ID="lblMessage" runat="server"
Text="some text"
Visible='<%# FormMode == "View" || FormMode == "ViewApplicant" %>'>
</asp:Label>
Your code is perfectly okay. While accessing the variable/property initillay, the red underline may appear under the expression indicating the compile error, but when you build the solution, there should be no error.
Your original post has changed so here's a new answer.
Try removing the namespace from the code behind and change 'inherits' on the page to Inherits="WebForm1".
page directive:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebForm1" %>
code behind:
using System;
public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
'Inherits' must match the partial class name.
I need to create a radiobuttonlist containing a "other" option, and i have to put a textbox next to it. Here is what i did.
if(!ispostpage){
PlaceofWork.DataSource = workplace;
PlaceofWork.DataBind();
PlaceofWork.DataTextField = "WorkPlace1";
PlaceofWork.DataValueField = "WorkID";
PlaceofWork.Items.Add(new ListItem("Other - Specify<input name=\"OtherWorkPlace\" type=\"text\" value=\"test\" id=\"OtherWorkPlace\"/>", "-1"));
This radiobuttonlist is in a usercontrol.
at the first I didn't add if(!ispostpage) so when i try Request.Form["OtherWorkPlace"], it didn't return anything
after i add the code if(!ispostpage), it pop up the error "Object reference not set to an instance of an object"
So how can I get the value of the input textbox?
Not sure if this would help you out, but wouldn't it help if you place a asp:TextBox control in the page and retrieve the value from it only if 'Other' radio button is checked? This text box can be made visible in the client side once the 'Other' radio button is checked. The text box can be placed next to the radio button using CSS.
You could use a hidden field control on the asp .net side.
<asp:HiddenField runat="server" ID="rbOtherText" />
The set the value of the control using a javascript onblur event.
PlaceofWork.Items.Add(new ListItem("Other - Specify<input name=\"OtherWorkPlace\" type=\"text\" value=\"test\" onblur=\"document.getElementById('" & rbOtherText.ClientID & "').value = this.value;\" />", "-1"));
Then in the code behind just reference the HiddenField.
The runat="server" is not buying you anything, it's probably just being rendered directly into the output. If you can see it when you View Source on the page, then it's not being processed as a server side control. I would get rid of that.
Add an ID attribute with the same value as the name attribute:
PlaceofWork.Items.Add(new ListItem("Other - Specify<input id=\"OtherWorkPlace\" name=\"OtherWorkPlace\" type=\"text\" value=\"test\"/>", "-1"));
Remember that IDs must be unique, so you can only have one of these per page without changing the naming convention. Then you are able to see it in the Form collection, as you previously tried.
ASPX
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%# Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
ASPX Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string result = Request.Form["OtherWorkPlace"].ToString();
}
}
ASCX
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
ASCX Code behind
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
if (!IsPostBack)
{
RadioButtonList1.DataSource = list;
RadioButtonList1.DataBind();
//RadioButtonList1.DataTextField = "Value";
//RadioButtonList1.DataValueField = "Key";
RadioButtonList1.Items.Add(new ListItem("Other - Specify<input id=\"OtherWorkPlace\" name=\"OtherWorkPlace\" type=\"text\" value=\"test\" />", "-1"));
}
}
If your RadioButtonList is in a UserControl, it sounds like you want to re-use this in multiple places, possibly even on the same page. If so, you are definitely going to have to come up with a way to manage the IDs so they don't conflict.
I have two User Controls. And one aspx page which is loading the controls. Code is given Below
Home.aspx:
<%# Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="MYApp.Home" %>
<asp:Panel ID="pnlTabs" runat="server"></asp:Panel>
Home.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
base.LoadUserControl(UC_Tabs, pnlTabs);
}
Tabs.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC_Tabs.ascx.cs" Inherits="MyApp.UserControls.UC_Tabs" %> <asp:Button ID="btnPhotoTab" runat="server" Text="Photos" CssClass="tab_style" style="border-right:none;" onclick="btnPhotoTab_Click" />
<asp:Panel ID="pnlPhotos" runat="server"></asp:Panel>
Tabs.ascx.cs:
protected void btnActivityFeed_Click(object sender, EventArgs e)
{
Home HomePage = (Home)this.Page;
LoadUserControl("UC_PhotoSearch.ascx", pnlPhotos);
}
public Control LoadUserControl(string ControlName, Control Container)
{
Control UControl = null;
if (ControlName != null)
{
UControl = this.LoadControl("~/UserControls/" + ControlName);
UControl.ID = UControl.GetType().Name;
Container.Controls.Add(UControl);
}
return UControl;
}
UC_PhotoSearch.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC_PhotoSearch.ascx.cs" Inherits="MyApp.UserControls.UC_PhotoSearch" %>
<input id="txtSearch" name="txtSearch" type="text" runat="server" />
<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click" />
<span id="PhotoSearchedData" runat="server"></span>
UC_PhotoSearch.ascx.cs:
protected void btnSearch_Click(object sender, EventArgs e)
{
//Photo Search Code here
}
The above is all my code.
Now I am loading the Tabs control on Home page and
Photo search Control on Tabs control
Now I click on Search Button But the
btnSearch_Click in not firing and even the page load event is also not firing.
How can I able to Access the btnSearch_Click event from home page.
If I used the following on Tabs control then the button click is working fine.
<%# Register Src=”UC_PhotoSearch.ascx” TagName=”PhotoSearch” TagPrefix=”uc1″ %>
<uc1:PhotoSearch ID=”PhotoSearch″ runat=”server” />
So what is wrong with dynamic loading of controls.
Please Help me
Not sure if I understand you quite, but I think if you load your controls in the Page's PreInit event and not Page_load your events will fire.
I am trying to locate a label on a content page from within User Control(ascx)
Page p = this.Page;
//this line causes application to unload with no exception
ContentPlaceHolder cp = (ContentPlaceHolder)p.Master.FindControl("Content2");
Label label = (Label)cp.FindControl("SomeLabel");
It just unloads itself with no exception mesage. Why does it happen?
I was able to get this to work so that it finds a Label from within a ContentPlaceHolder where my user control resides:
Site1.Master:
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" MasterPageFile="~/Site1.Master" %>
<%# Register Src="WebUserControl1.ascx" TagName="stuff" TagPrefix="uc" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="Label1" Text="Label" runat="server" />
<uc:stuff ID="test" runat="server" />
</asp:Content>
WebUserControl1.ascx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.Master != null)
{
Control c = Page.Master.FindControl("ContentPlaceHolder1");
if (c != null)
{
Label l = (Label)c.FindControl("Label1");
}
}
}