Codebehind and inline on same page in ASP.NET? - asp.net

In ASP.NET, is it possible to use both code behind and inline on the same page? I want to add some inline code that's related to the UI of the page, the reason to make it inline is to make it easy to modify as it outputs some HTML which is why I don't want to add it in the code behind, is this possible in ASP.NET?
Edit: I'm sorry, obviously my question wasn't very clear, what I meant is using a script block with runat="server", this is what I meant by inline code.
Thanks

Yes, you can use inline code just like in classic asp. You can use 'this' or 'me' to get to the code behind fields, methods, etc.

Yes, that will work, make sure the code behind class is declared as a partial class, which is the default these days anyway. Don't think it would work in 1 or 1.1

Related

Is it bad to use runat="server" on <tr>?

I'm supporting an application that uses runat="server" all over the place to show/hide table rows.
For example, in places where there are dependent DropDownLists, the row with the child ddl will be hidden until a value us chosen in the parent ddl.
Is that a bad practice? Is there a better way to do this?
I use runat="server" anytime I need it. So I think you can use it too. :-)
I think that's absolutely terrible practice. First of all you do not need to make the trip to the server to hide and show controls, unless you need new data.
Second, any decent javascript framework will allow you to hide show controls based on the control's id, class name, or whatever css selector. Moreover using a javascript post/get to a generic handler will give you the data that you need without the postback.
I'd suggest using JQuery, or some other alternative.
It depends on how much you care about performance. Anything that is marked with runat="server" goes through more processing than just client side tags.
Personally, I've used them before. Especially in the situation where a table cell or table row is relying on data from the server. You can use Javascript or JQuery with a hidden field but you still have to hit the server for the hidden field, so it doesn't buy much.
It's not bad to use runat="server" with standard HTML controls. Often you'll find the use of PlaceHolders to show and hide content on pages, or in ASP.NET MVC you might see the use of inline code blocks such as <% ... %> within the views. On it's own and not in consideration of other design aspects, it's neither good nor bad.
That's what I do to hide the row containing other server controls. The other options are to use a asp:panel or other container, but that will add more HTML without any gain.
I don't think it's necessarily bad practice. I've done the very same thing plenty of times. I think it's mainly personal preference.
Not at all. ASP.NET supports making any html tag run on the server side. Your alternative is to wrap the tag in a Panel, and hide or show that. If you're not looking for the extra functionality or want to control the output yourself, making an html tag run on the server isn't a problem.

Programmatic accessing a control from base to derived?

In a recent post, I expressed a need to access the properties for the body using declarative syntax, see
Contentplaceholder for replacing attributes?
I thought the first suggestion solved my problem. But, the syntax confuses the editor which is not acceptable.
My hypothesis for a workable solution is to make the change in code. To derive a class from System.Web.UI.Page with extended functionality. However, I want for the designer to be able to still use declarative syntax to set the body tag.
In other words,
I do not want to have to change any code in my aspx web pages except that they derive from base
I want to be able to set these properties using declarative syntax, merely by adding a tag in the derived page
I'm not immediately sure how to go about doing this because it doesn't exactly fit the OOP paradigm and I'm not sure where the changes need to be made.
Initial hypothesis,
I can use a findcontrol in the base to see if the placeholder has been added. But, not sure exactly at what point in the page processing lifecycle that I can use this findcontrol. I need to set the body before it renders but also be able to grab something from the declarative code.
I hope what I'm trying to accomplish is clear(?) Basically, I want to be able to edit a user defined tag in my aspx page that will change the class for the body in a master page.
Why not create a custom server control (called something like BodyAttributeManager) that does not render anything, but just looks at its attributes and programatically adds them to the body element on PreRender?
Nested tags as mentioned in that example isn't going to work. ASP.NET does not support that. #jball's suggestion is a good idea; having a class outside of the body that can programmably affect the body's settings from code (which is the only real solution here) would give you what you are looking for.
HTH.

Linking to ascx file

I am utilizing controls in my asp.net application. I have a register tag the source of which needs to be dynamic. I am using the line below which functions correctly when the full path is specified but when I change it to the variable I get a parser error. Any idea how I can go about doing this?
Thanks
It might be better to use Load Control from the code behind of the aspx page.
If I'm understanding you correctly — you can't use a variable in those directives (Page, Register, etc). They have to be constant expressions.
However, it is possible to dynamically load ASCX controls. You would have to do this in code, though, and it would not involve the Register tag.

Selecting by ID attribute using JQuery in ASP.NET

I've just started using JQuery in VS 2008, and so far I like it! But, I'm confused about how I should be using JQuery in order to select asp.net controls on a webpage.
For example, I have the following code (just a mock-up):
<asp:textbox id="txtSomeData1" runat="server" text="Some Data!!"></textbox>
Now, if I want to use JQuery to select the textbox and change it's text to "Some More Data!!", then I would have to do something like:
$('input#ctl00_ContentPlaceHolder1_txtSomeData1').val('Some More Data!!');
Which, quite frankly, is annoying because I don't want to mess with having to figure out what the id of the control is after it's rendered to the webpage (ctl00_ContextPlaceHolder... blah blah blah).
Is there a way that I can select the textbox without having to use the id of it? Also, I know that you can select by class name, but that doesn't help much if the control that you're selecting doesn't have a class.
Am I just missing something here?
JUST TO REITERATE: I do not want to use a class to select the input tag!! I would like to use the id "txtSomeData1" and not the long id that is rendered to the webpage.
What you want to do is either:
$("input[id$='_txtSomeData1']").val().....
or you could add a class or custom attribute to the textbox that you can select on
if the javascript is on the same .aspx file, you can do:
$('<%= txtSomeData1.ClientID %>').val('Some More Data!!');
This is a big complaint the community has with ASP.Net web forms. In ASP.Net 4.0 you get control of your IDs back so it's like you're wrote it in raw HTML. Alternatively, without .Net 4.0, you could use ASP.Net MVC which, for the most part, doesn't use server controls, so you wouldn't have the issue.
But like happytime harry says, adding a class may be want you want if you're working with web forms and jquery.
If you are going to externalise the JavaScript to its own .js file you are stuck with either hard coding in the id (most efficient), some regular expression selector or using the inefficient by class name selector.
However if it is in the same file as the .NET control you could always use $('<%=txtSomeData1.ClientID %>')
There are lots of selectors to use... maybe adding a class is what you want to do.
http://docs.jquery.com/Selectors
jQuery Wildcard Selectors are what you may be missing here.
You can add 'marker' classes to any control whose sole purpose is for use in a jQuery selector.
That, along with navigating the tree heirarchy (something like "$(element).children('.myClass').show()" might be a good way to do this without IDs.
Btw, http://visualjquery.com/ is a great way to view the jQuery APIs etc.
You can use the element[id$=Identifier] notation, which looks for the id ending with the text you specify (it's the ID you specify in ASPX markup). Here is an example illustrating how you can use it (see slide #30): Building intranet applications with ASP.NET AJAX and jQuery.
Please use the following syntax :
$("[id*=txtSomeData1]") to reference any asp control
For ASP.NET Web Forms, you can use ClientIDMode="Static" to prevent the id from changing:
<div id="MySection" ClientIDMode="Static" runat="server"></div>
which means you can then reference the element in a separate plain JavaScript file if required, without having to embed the JavaScript in the .aspx file:
var mySection = document.getElementById("MySection");
and even add classes:
mySection.classList.add("hidden");
And the CSS used above for reference:
.hidden {
display: none !important;
}

ASP.NET - Inline vs. Code-Behind

I realize that by asking this question, I could have started the apocalypse, but a colleague of mine uses a lot of inline coding in their aspx pages, where as I prefer using the code-behind.
Is there a right and a wrong way here?
Not unless your coding standard says otherwise.
IMO code-behind helps separation of concerns, so I prefer that, but sometimes just dealing with one file is nice too.
Code-behind is the more traditional and logical place. If it works, it works, but I can't stand doing it in the aspx.
I made the exact post a while back:
OnDataBinding vs Inline: pros, cons and overhead
I prefer the code behind. I usually have a #region area for all the databinding stuff. It allows for people skilled in HTML / CSS to tweak the HTML and all they have to know is what basic controls to use and to define the OnDataBinding event in the control's definition. They can move things around and do whatever and have no knowledge of what it actually takes to get the data into that databind as it might not be as simple as just a basic 'Eval("something");.
Well, there's inline code, and then there's inline code. If you have a script block at the top for you page_load, that's just fine. But if you're mixing a lot of bee-stings (<% %>) in with the markup, you'll eventually start to have trouble when those bee-stings don't work as you would like with other server controls.
Personally I prefer compile errors to run-time errors so I put all my logic in code behinds.
Occasionally when I just need a value to show up on the page I'll put <%=SomeValue%> but even then I much prefer to create a label and set it.
I want to make sure i understand the question - by in-line, do you mean snippets like
<a><% some.asp.net.code %></a>
or do you mean one vs. two files for each page:
page.aspx
page.aspx.cs
?
Because I am NOT a fan of what I call the 'embedded' code in the first example but I DO prefer code and markup in the same file with a < script runat="server">
There is no right/wrong with either of this.
You can write some code inline & some of it should be written in code-behind. It can't be absolute. Depends on how readable/maintainable it becomes when you write it on the other side.
EDIT: As it is said, write code for human & incidentally for the compiler.
I tend to use the code-behind since it is the only way for WPF and I try to stay consistant and it feels more natural. But thats subjective.

Resources