How to pass parameters of the controller to the .aspx view? - asp.net

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.

Related

UpdatePanel not working in default document

My simple application has one page Default.aspx.
If I hit the page at /app/default the page loads and everything works. If i go to /app/ with no file name the page loads but my button does not work.
looking in fiddler I can see a working request starts like
1|#||4|219|updatePanel|MainContent_upDemo|
but a bad request returns the page starting with and the updatepanel javascript can't parse it.
Turning off friendly URLs has the same issue but I now have to go to /app/Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CasualFrontEnd._Default" EnableEventValidation="true" %>
<%# Register TagPrefix="cc1" Namespace="EAV" Assembly="EAV" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="upCasual" runat="server">
<ContentTemplate>
<asp:Label ID="lblTesting" runat="server" Text="Label"></asp:Label>
<asp:Button ID="BtnApply" CausesValidation="true" runat="server" Text="Apply" OnClick="BtnApply_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
It looks like a bug in the way the default path is handled with no route.
my solution:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("nofile", "", "~/Default.aspx");
//var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Temporary;
//routes.EnableFriendlyUrls(settings);
}

Find property of sub child usercontrol in aspx page

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);

error when change master page with code in asp.net

i have 2 master page
"Main.Master" and "MainAr.Master"
and i have page "Default.aspx" inherit from baseclass
public class BaseClass:Page
{
protected override void InitializeCulture()
{
if (Session["Lang"] != null)
{
if (Session["lang"].ToString() == "ar")
{
this.UICulture ="ar";
this.Culture = "ar-EG";
this.Theme = "ar";
}
else if(Session["Lang"].ToString()=="en")
{
this.UICulture = "en";
this.Culture = "en";
this.Theme = "en";
}
}
else
{
this.UICulture = "en";
this.Culture = "en";
// this.Theme = "en";
}
}
protected override void OnPreInit(EventArgs e)
{
if (Session["Lang"] != null)
{
if (Session["lang"].ToString() == "ar")
{
this.Page.Master.MasterPageFile = "~/MainAr.Master";
}
}
}
}
in onPreinit void master page of default page must be changed in runtime this is the code of inhertnce
public partial class Default : Utilities.BaseClass
but in change master page code i got this error
Content controls have to be top-level controls
in a content page or a nested master page that references a master page.
2 master page code is
<%# Master Language="C#" AutoEventWireup="true"
CodeBehind="Main.master.cs" Inherits="MotahedaWeb.Main" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
and the default.aspx page code is
<%# Page Title="" Language="C#" MasterPageFile="~/Main.Master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MotahedaWeb.Default" %>
<%# MasterType VirtualPath="~/Main.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
any ideas? ,thanks
It seems to be correct. You only need to make sure that the ContentPlaceholderIds are consistent across the pages you are swapping between (that's what causing that error message)

Access to MVC Models from standard ASCX UserControl

I've got a simple ASCX user control (non-MVC). The user control has a property on it (let's call it Title or something).
I've also got an MVC Partial View which contains the user control.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeModel>" %>
<%# Register TagPrefix="tag" TagName="Control" Src="~/path/to/ascx/control.ascx" %>
... snip ...
<tag:Control ID="myControl" runat="server" />
What I'd like to be able to do is something like this:
<tag:Control ID="myControl" runat="server" Title="<%= Model.Title %>" />
... so that I can access some property of the model inside my Control.
Unfortunately this method doesn't work, as I get a message saying "This is not scriptlet. Will be output as plain text".
Is what I'm trying to do even possible? If so, does anyone have any ideas how I can try and do it?
If it's relevant, this is a .Net 4 project, with MVC 2 components.
Thanks!
Neil
Is what I'm trying to do even possible?
Yes but it would be extremely ugly and it involves using a code behind (in an ASP.NET MVC view!!!):
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="AppName.Views.Home.Index"
CodeBehind="Index.aspx.cs"
%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Txt" runat="server" />
</asp:Content>
and in the corresponding code behind Index.aspx.cs (beurk..., I will vomit):
public partial class Index : System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>
{
protected void Page_Load(object sender, EventArgs e)
{
Txt.Text = Model.Title;
}
}
which could be improved like this to avoid the code behind nonsense:
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>"
%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Txt.Text = Model.Title;
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Txt" runat="server" />
</asp:Content>
I wouldn't recommend you doing something like this if you intend to migrate to ASP.NET MVC 3 and the Razor view engine one day.
I found a solution that's a little cleaner. Still not to my liking, but good enough to wash my hands of this annoying issue.
<% ViewBag.Item = item; %>
<gc:Ribbon runat="server">
<Content>
<strong><%= ViewBag.Item.DataItem.Name %></strong>
</Content>
</gc:Ribbon>
Instead of passing an object into the user control to use within, assign it to the ViewBag (for each iteration if you're looping), and use it globally. If memory serves me right, I believe MVC 2 uses ViewData["loosely typed name"] instead of the dynamic ViewBag.

locate label on content page

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");
}
}
}

Resources