Question about the "runat" attribute in ASP.NET? - asp.net

I went through this thread but couldn't understand much. I am very new to ASP/HTML/Server-side programming.
I tried running this code on a .aspx file:
<form id="form1" action="Default.aspx">
<div>
<asp:Label ID="lblName"></asp:Label>
</div>
</form>
And I got an error when I tried to use this in the CodeFile:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = "123";
}
"lblName does not exist".
But if I use runat="server" attribute with the label, then this code works.
Also, is there any concept of nesting the runat attribute. e.g, If I specify runat=server for the form above, will all my controls inside the form automatically be configured to run at server? How does this attribute work?
In which case would I be required to specify runat=server for the and for the tag? How does the server-side know that the label is inside the form if I don't have a form object at server side? Or am I missing something?

Any element marked with runat="server" lets the framework know that this will be a control on the server side. This article has more details:
Exploring the Runat Attribute

Nope, there is no such nesting available in ASP.NET, you have to specify "runat" to every control that you want to use in code behind and that is part of ASP.NET web library.
Because ASP.NET can only recognize difference between client side tag (the html that runs on browser) and server side tag with help of "runat"

Related

When isn't runat="server" used in asp.net?

When don't you need to use runat="server" in ASP.NET?
EDIT: Thanks for all the answers, but I was really thinking of runat="server" inside an <asp: tag.
Use the runat=server attribute when you're using ASP.NET controls, and/or you require programmatic access to those controls in your code-behind.
HTML controls don't require this attribute. It's useful if you have any HTML element like <span> <div>, or <table> when you want the ability to access them in code-behind.
<asp:Label runat="server" id="foo" />
<div runat="server" id="bar />
...
foo.Text = "Hello Label";
foo.Attributes["class"] = "baz";
You need to use runat="server" on any control that you want to be parsed as a server control.
Any element with runat="server" will be parsed into a server control in the Page herarchy. Anything else will be handled as plain text, and put in LiteralControl controls in the Page hierarchy.
The exception is elements that aren't real elements, but special tags within another server tag, for example ContentTemplate tags. They don't need a runat="server" because the containing control will parse them.
When you don't want the server side ASP.NET to render a server side variable against us.
Generally speaking you don't use it when you don't need to manipulate the DOM element at the server side e.g. which are only used for layout purposes.
Without runat="server" there would also be no other way to make html controls server side controls. It does look like an odd thing, because you can't do runat="client".
So in summation you can't leave it out on any ASP .Net controls ever and it was probably the easiets and cleanest way to find all server side controls for the developers who created ASP .Net Web forms.
source: http://mikeschinkel.com/blog/whyrunatserverforaspnetpart2/
Tag runat="server" indicates that the code contained within the script block will run on the server (and not on the client). On execution, ASP.NET will create server-side objects that contain this code as well as an instance of the Page class to contain the controls defined inside the page as instances of their given type (System.Web.UI.WebControls.Textbox, for example). This server-side object will be invoked on user request and will execute code in response to events.
Create Control in Runtime
I need one label in runtime that time don't need runat="Server" is not required
Example
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label lblNew = new Label();
lblNew.ID ="lblnew";
lblNew.Text ="Test";
}
}
this code create label in runtime at page load event

disable aspxdateedit control in javascript

iam using aspx date edit control in my asp.net and c#.net application.
is it possible to disable aspxdateedit control in javascript?if so how acan that be done.becoz ,i tried
using
"document.get elementbyid("datedit").disabled=true;"
but it didn't worked out !!
any suggestion on this?
This can be done using the editor's client side SetEnabled method. I.e.
// JS
dateEdit.SetEnabled(false);
Note, here the dateEdit is the editor's ClientInstanceName property value. It allows you to define the name of the java script client side object. Using it, you will be able to access its client side properties and methods.
First of all set the control's ClientInstanceName property to access the control at client side.
<dx:ASPxButton ID="btnSubmit" runat="server" Text="Save" AutoPostBack="false"
Width="120px" **ClientInstanceName="btnSave"**><ClientSideEvents Click="OnButtonClientClick" />
</dx:ASPxButton>
Now you can do this in two ways as:
ClientSideEvents Click="function(s, e) {document.getElementById('btnSave').enabled = false;}"
Have can try this also:
ClientSideEvents Click="function(s, e) {btnSave.SetEnabled(false);}"
you can also access these controls in javascript code at aspx page either in a callback event or some other client side method.
just access the control with their associated client instance name as i have btnSave for my ASPxButton
btnSave.SetEnabled(false);
get more information about these control's methods and client events etc. see the clientscript namespace of these aspxEditors DevExpress.Web.ASPxEditors.Scripts Namespace
hope you will get your solution or help to solve your problem here..
Happy Coding..

How can you tell if form runat=server has been set?

When coding an Asp.Net page, you generally add a runat attribute in the aspx:
<form id="form1" runat="server">
Is it possible to tell in the code behind if the user hasn't done this i.e. they only did this:
<form id="form1">
Here the form has the id "form1" but in my case I don't know this. Code behind such as this is what I am looking for:
if(Page.HasForm)
{
}
You can only ever have one form tag with "runat=server" on it per .aspx page. All you have to do is to check to see if Page.Form is null or not. If it's null, then there's no form that has been marked to runat server.
if (Page.Form != null)
{
}
It's the runat="server" part that makes the .aspx page process an element and create a corresponding object on the server side. If a component is not running on the server, then it's not added to the page's control hierarchy.
var v = this.Form.TagName; //gets the name of the form that is maked as runat.
Of course if its not maked as runat then your code behind won't run anyway...
When you code in C# or Visual Basic in the code page, you will not have access to the object that do not have the runat=server option set.
You can easily access all the controls from a page using the me.controls page or something of the sort (I don't know the exact code but it's close to this) and check the type of the control to get the form.
Why do you need to know that? If a page does not have a runat=server form, it can't really be used as a server page.
You'd be able to access the form from the codebehind:
Response.Write(form1.Name);
Without the runat="server", you'd just get a compiler error.

ASP.NET invoke ASP.NET buttons server event in javascript

I am having an ASP.NET page with one Asp.net button control and a normal html link (anchor tage) I want to invoke the postbackl event of asp.net button control when someone clicks on the link.
I used the below code
<a href="javascript:myFunction();" class="checkout" ></a>
<asp:Button ID="btnCheckout" runat="server" Visible="false"
onclick="btnCheckout_Click" />
and in my javascript i have
function myFunction()
{
var strname;
strname = "Test";
__doPostBack('btnCheckout','OnClick');
}
But when runnin gthis , i am getting an error like __doPostBack is undefined
Can any one tell me why it is ?
Thanks in advance
This anyway wouldn't have worked. When you make your .NET control invisible by using 'Visible="false"' it isn't rendered, that means not available at the client.
Back to your question.
1- Where is myFunction defined? Between the tag?
2- Are there more .NET controls on the page? If there aren't any other .NET controls, .NET doesn't add all the scripts that are required for postbacks and stuff.
Why not do the following (based on TheVillageIdiot answer):
<asp:LinkButton ID="lbtnCheckout" runat="server" CausesValidation="false" OnClick="lbtnCheckout_Click" CssClass="checkout" />
With the above example you don't need the fake button and make it invisble. You still can do your postback. Way more cleaner approach I would say.
First of all I tried your code and also not get anything like __doPostBack, then I added another button on the page which was visible but it was all the same. Then I added a LinkButton and got __doPostBack method. You can do post back from javascript but then EventValidation is problem, as it does not allow this kind of thing. I had to use the following to overcome it and it worked:
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(
new PostBackOptions(btnCheckout, "OnClick"));
base.Render(writer);
}
I think I'm bit incoherent in answering so I'll mark it as wiki :)

Add packaged javascript to page from ASP.NET server control?

Is it possible to create an ASP.NET server control that packages some javascript that will be emitted to the aspx page at design-time rather than run time? I am trying to create a control that will have "default" javascript. I can add jvascript using RegisterClientScriptBlock, but then a web developer can't modifiy the javascript - it is unavailable a design-time in this scenario. Is there a way to change the ToolBox properties so that when a web developer drops the control onto the page, the javascript is added in a separate script tag as well?
When I need to do this I make a property on the control that will inject the tags, and then the web dev makes a asp:literal tag that has visibility none and viewstate disabled, that has all the JS they need.
then in the page's code behind they inject the literal's text into the server controls properties.
<asp:Literal ID="Literal_HtmlHeader" runat="server" Visible="false" EnableViewState="false">
<script></script>
<style></style>
</asp:Literal>
there are probably better ways... but this is simple and effective for me.
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(this.Literal_HtmlHeader.Text.Trim()))
{
//inject css and js into header.
Page.Header.Controls.Add(new LiteralControl(this.Literal_HtmlHeader.Text));
// or add to your control cause it knows how to add the tags so there is no duplication.
ServerControl c = new ServerControl();
c.HtmlHeaderCode = this.Literal_HtmlHeader.Text.Trim();
}
}

Resources