How do I access sub-control's HtmlControl.Style in user control? - c#

I have a user control that contains the following text (among other things) in the .ascx file:
<div id="divLine" runat="server">
<-- Stuff -->
</div>
In the C# code, I have this property:
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CssStyleCollection LeftStyle
{
get { return divLeft.Style; }
}
This is pretty much copied from System.Web.UI.HtmlControls.HtmlControl.cs. However, when trying to use my custom control in .aspx pages, I can't seem to access <uwc:UserControl LeftStyle="stuff"> like I could access it directly in <div style="stuff">. Is there any way to?

No answer, so I took a different approach. divLine only contained an asp.net Label, which I exposed as a public property of the control. Apparently you can do things like <asp:Label runat="server" style="stuff"/>. Intellisense will not indicate that style is a valid attribute, but it can still be used. Just like other asp.net controls, attributes the IDE doesn't recognize will still be written to HTML. Heck, once you get style=" written, you'll even get the typical drop-down guide for it.

Related

How to get html control by ID that has hyphens?

I have a front end written in html that I am converting to asp, and many of the controls have names with "-" in them. This is causing crazy headaches, as there is no time to rename everything, and the ctrl-f and replace somehow breaks my css. Is there any way to access these controls in the code behind while they have the dashes? I have tried the code below.
//Can find when there is no dash in it, but that breaks the css after find/replace of full solution
HtmlGenericControl body = (HtmlGenericControl)this.Page.FindControl("page-list");
body.Attributes.Add("class", GlobalVariables.webAppSkin);
//I have also tried this, but logout stays null
WebControl logout = (WebControl)Page.FindControl("logout-link");
This is the html control:
<body id="page-list">
Sorry, that's not gonna happen.
You cannot have an element with an id containing "-", and still be a runat="server" ASP.NET control.
Microsoft's docs about the control's IDs states:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.id.aspx
Only combinations of alphanumeric characters and the underscore character ( _ ) are valid values for this property. Including spaces or other invalid characters will cause an ASP.NET page parser error.
If you tried adding runat="server" to the body tag you showed: <body id="page-list">, it would give you the following line in aspx.designer.cs:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl page-list;
Which is obviously throwing an exception on C# syntax.
<body id="page-list"> is not a HTML Control (i.e. an instance of (a subclass of) System.Web.UI.Control because it doesn't have the runat="server" attribute. If you were to add runat="server" then you would get a JIT compile-time error message informing you that "page-list" is not a valid identifier.
ASP.NET Web Forms 4.0 added the ClientIDMode attribute, however it doesn't allow you to set a completely custom ID attribute value. There's no easy solution for this without using <asp:Literal> or implementing your own control (or my preferred option: switching to ASP.NET MVC).
You can access controls in code behind with their ID if you write runat="server". Here is the example for your case
<body runat="server" id="testID">
In code behind you can access it like this:
body.Attributes.Add("class", value);

Rendering WebControl with Empty Attribute

Is it possible to build a control that derives from System.Web.UI.WebControls.WebControl that allows 'empty' attributes?
I.e. I need to output <div helloworld></div>
Ive tried overriding RenderEndTag() and this.Attributes.Add("helloworld",null) and neither work correctly.
No. The syntax used for control parsing in ASP.NET web forms requires that attributes must have a value.
The best you could do is to use inner content:
<asp:Label runat="server" id="fooLabel" Text="Hello, world!"/>
is the same as
<asp:Label runat="server" id="fooLabel">Hello, world!</asp:Label>
Looked a bit deeper, the following code works;
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute("helloworld", null);
}
Reflecting System.Web.UI.HtmlTextWriter.RenderBegin() midway down where attributes are written shows the line:
...
this.writer.Write(' ');
this.writer.Write(renderAttribute.name);
if (renderAttribute.value != null)
{
this.writer.Write("=\"");
...
Which shows why the above works, and this.Attributes.Add doesn't (I believe a filter is run to exclude any null attribute values).

Set a property of a custom user control in the code behind

I found this question that shows properties on a custom user control (ascx) can be assigned inline as an HTML attribute: Custom attribute in UserControl (*.ascx)?
This works great but what about if I register a custom user control on my page and want to set/get attributes from that control in my code behind?
ASPX:
<%-- I can assign ActivePage inline and this works fine --%>
<wd:NavBar ID="MyNavBar" runat="server" ActivePage="navbarItem1" />
ASPX.CS:
// I need to change the ActivePage
if (what == "internal")
{
RunInternalComparison();
MyNavBar.ActivePage = "navbarItem1";
}
else if (what == "external")
{
RunExternalComparison();
MyNavBar.ActivePage = "navbarItem2";
}
That's what I want to do but it doesn't work. Is this possible?
Do'h! This actually works. I guess Visual Studio wasn't auto generating controls. Simply adding protected NavBar MyNavBar; to the top of my Page solved the problem. I hope someone else finds this useful.

Need stack views in Razor that allows me to hide the views or the fields

I have several partial views that I need to stack them on my web page.
pv1 pv2
pv3 pv4
pv5 pv6
I also need to be able to hide any of them or even labels within each view.
I tried using a table but I cannot get them to show up right.
Here is an example of what I did:
<h2>AccountScreen</h2>
<table>
<tr>
#Html.Partial("CustomerInfoPartialView")
</tr>
<tr>
#Html.Partial("BalancePartialView")
</tr>
</table>
That didn't turn out anything like I was hoping for, it was a mess.
What should I use to stack my partial views and also be able to hide/show them programmatically?
As people have said, you should use <div> elements. You should also give them unique identifiers and use jQuery to hide/show as necessary. If you have the criteria available at the time the model is rendered, it might be simpler to add a visible/hidden style as needed to each element.
#
{
Html.Partial("CustomerInfoPartialView")
...
Html.Partial("CustomerInfoPartialView")
}
Then in each partial:
#{
var classToUse = (myVisibleCriteriaBool) ? "visibleClass" : "hiddenClass";
}
<div id="UniqueNamePartialView" class="stackableDivs #classToUse">
Your html stuff here
</div>
As long as the class stackableDivs (I just made this up) includes a float style, they should stack appropriately. That would probably need to be adjusted depending on how you wanted them to stack, but it should still be a matter of css. If you don't know at the time the view is rendered, you could use jQuery to hide the individual elements:
$($('#UniqueNamePartialView").hide());
or
$($('#UniqueNamePartialView").show());
I would stay away from toggle() since it's deprecated.
If you want to hide/show them in C# within the View, all you have to do is to render them or not.
If you want to hide/show them in JavaScript, just put them inside a div and use $("your_div").toggle();

C#.NET coming from PHP

I am coming from PHP so bear with me with my noobness...
I want to know a few things.
I know PHP, javascript and MySQL very very well now and I understand that browsers understand a few things.. html and javascript.
I ran through a tutorial of c#.net and found that it had pre-made "user controls" and i thought, oh my it's completely different from PHP... Then i realised that in the end it ends up with a bunch of javascript I could have written myself (not saying i want to ;), just saying).
My questions....
1.
If I have a table with some input fields:
<form id="form1" runat="server" method = "post" action = "validate_entry.aspx">
<table>
<tr>
<td>Name: </td><td><input type = "text" name = "name" /></td>
</tr>
<tr>
<td colspan = "2"><input type = "submit" value = "submit" /></td>
</tr>
</table>
</form>
and use the post and action methods, and then in validate_entry.aspx on Page_Load class I call something like:
c.Request["name"];
and do whatever with it.. Is this still a professional way of using c# or am i wanting it to be too much like PHP. Am i missing out on some execute speed or something even though it's running javascript.
2.
If I wanted to simply output the word "arse" in a div... If I do something like this, am I defeating the object of c#.net
<div id = "the_butt_holder">
<% Response.Write("arse"); %>
</div>
or a better example is that in my validate_entry.aspx page I do:
<div id = "the_response">
<% Response.Write(c.Request["name"]); %>
</div>
I think in your case, you would benefit greatly from looking at ASP MVC. It gets much closer to the grain and will probably be much closer to what you're used to working with as you have a great deal of control over the html.
Well James, I think you could start using asp.net mvc. It will be so easier to you than asp.net webforms. WebForms is based on Server Controls and Events, you cannot set a page in tag, instad of it you can create a , double click in it and write your code on .cs file respective of your webform, then, the .net framework will do a thing called PostBack and executes this event. There's a desnecessary long life cycle when a PostBack runs.
In Asp.Net webForms you don't have control of your output HTML code. The SErver Controls will do it for you, and sometimes it is not you want (or need).
With ASP.NET MVC you have a Route Tables and you have a MVC Architecture Pattern, so, if you use a mvc framework in PHP, it would be easy for you. With ASp.NET MVC you have control of your HTML, and you can create Typed Views. Check this link: http://www.asp.net/mvc
I hope it helps you.
Your way is by no means wrong, however there is another way in ASP.NET, which is to use actual TextBox (which renders input field) control instead of input field directly. That way, you will be able to access value as a property of the control.
<asp:TextBox id="tb1" runat="server" />
var text = tb1.Text;
Why not just write literal directly instead of outputting it like this?
<div id = "the_butt_holder">
arse
</div>
You may feel much more at home using ASP.NET MVC, and Razor layout engine. It's much more like PHP and Smarty (or whatever similar templating engine replaced it nowdays :) )
You can find more than enough tutorials right there on the site to get you started.

Categories