ClientIDMode="AutoID" in Asp.net - c#

Hello Everyone I am a newbie in asp.net,
I have a checkbox inside a repeater which is inside an update panel,the page flickers whenever I click the checkbox, finally found that adding the ClientIDMode="AutoID" controls the flicker and everything works fine.
I am wondering about ClientIDMode what this has to do with the flicker,
went through msdn and found it generate the value of the ClientID property.

First distict the asp.net controls, from the final rendered html controls.
When you make asp.net controls you give them an ID, with this id you can address them and make change on them on code behind and with programming in general.
For example with this code
<asp:CheckBox id="check1" Text="option a" runat="server" />
you can use the check1 to get their value, change the text, and many others.
Now, asp.net must render this check box on the html page. When you make any control on an html page you must set to him a unique ID and name, and heres come the automatic id assignment.
asp.net take care to avoid conficts between final rendered controls and make automatic ids base on the structure that you have use on asp.net side.
For example, if this control is inside a custom control, and this control is inside a master page, asp.net will add also this names on the final id, to avoid conflicts.
Conflicts can exist for example on a repeated control, where you render the same control many times, so there you need to change each rendered id.
Conflicts can exist when you use many times the same user control.
Conflicts can exist when you use the same id on different user controls on the same page.
All that and many others asp.net comes and solve with the automatic generated id on client side render.

Yes, it does have something to do with the flicker.
See this related question for another example of the fact that Repeaters don't handle client ids well, causing exactly this problem because they end up doing a full postback instead of a partial postback.
Setting ClientIDMode="AutoID" is the workaround and allows the Repeater to succeed in doing the partial postack it is supposed to be doing from within the UpdatePanel.
Because of this Repeater bug, without ClientIDMode="AutoID" you were experiencing a full postback and full page load. A full page load does cause a flicker compared to a partial page load.

Related

asp.net runat="server" c#name does not exist in the current context

I have the following asp.net div setup on my page:
<div runat="server" id="ImageContainer">
<img src="images/<%# Eval("Image") %>" class="ArticleImage"/>
</div>
and I know this is being created at the server as when I view it in developer tools its id is:
ctl00_body_ArticleInfoRepeater_ctl00_ImageContainer
which is being created by the server.
I now want to edit the Css under certain conditions by doing the following:
ImageContainer.CssClass = "invisibleClass";
My problem is, in the C# code I am getting the following error:
The name 'Image container' does not exist in its current context.
I have no idea why this is happening, as it clearly does exist.
Any ideas?
Are you using some databinding control such as GridView, Repeater or some other?
The syntax <%#....%> represents data binding which works if it is placed inside some data binding control.
In such case you cannot access "ImageContainer" control directly. You have to search through parent control.
Since you haven't mentioned what is parent control of "ImageContainer" it's hard to give code sample here... Though here is example how it can be done in GridView
Incase, if you haven't used DataBindingControl then I would recommand to check yourpage.aspx.designer.cs and you should be able to find control name there!
Hope this will be helpful.
That's because you're trying to reference a dynamically created object (obiviously inside a repeater). Firstly, you shouldn't set ID for dynamic objects created during runtime. IDs have to be unique, or else you'll run into problems.
Secondly, you need to get the parent object (probably the repeater itself or the container?) and then traverse the collection of child controls to find the one you're after. There are numerous answers about how to find a control in an asp.net webforms, so just google for a while and I'm sure you'll get the code.
For instance find control
Controls placed on data list,repeater,grid-view are not accessed directly like other server controls placed on the page. If you want to access it through code behind you can access it on Data-bound or Item_command event of the repeater control because these controls itself act as containers for other controls placed on them.
you can use
e.Items.findControl("controlID") to access a particular row controls.
I recommend you to study these two events of repeater control.
if you want to change class of all div with name imagecontainer , you can use javascript or jquery for doing it with few lines of code.

Use one control multiple times in ASP.NET

What I have is few dropDownLists in few different templates of FormView.
And of course I can't get access to any of those.
What I want to do is get the same behavior for every DropdownList.
Is there any way to declare that DropDownList globally and reuse it or it should be done somehow with FindControl?
User control declarative syntax is very similar to syntax used to create an ASP.NET Web page.
The primary differences are that the user controls use an # Control directive in place of an # Page directive, and that the user controls do not include the html, body, and form elements around the content.
MSDN/creating usercontrol

How to ensure viewstate is disabled on a large number of checkbox controls?

I have an ASP.NET web form that has several thousand checkbox controls (in a parent/child hierarchy). The page takes about 4 seconds to load. I'm trying to increase the performance so that the page only takes ~1-2 seconds to load. After doing some digging, it looks like the checkbox controls are generating a massive viewstate object (almost 1MB in the HTML file). If I use raw HTML checkboxes, the page renders very quickly (and I think it's due to the fact that the viewstate isn't being created). I tried going down the raw HTML chechbox route, but am having trouble calling ASP.NET methods from JS using dopostback. Anyway, my main goal is to eliminate the viewstate on the checkboxes. I've tried doing this by disabling viewstate on the checkbox controls as well as on the ASPX page. Regardless of what combo I use, the viewstate object is still being created (and as I said, it's massive). I've even tried to clear the viewstate object in the code behind at all the major events that fire. Still no luck at clearing out the viewstate. Any suggestions on what I can do to disable viewstate on the checkboxes to avoid the overhead of what looks like is causing a performance bottleneck?
One way to keep the page as it is, is to warp your check boxes, with a place holder and set EnableViewState="false" for this one.
<asp:PlaceHolder runat="server" EnableViewState="false">
<asp:CheckBox runat="server" ID="cbOne" />
</asp:PlaceHolder>
Other way is to place them all inside a custom control and set EnableViewState="false" for this control.

Enabling ViewStateMode doesn't work for the child controls of a child control

I am working on .net 4.0 application. I am trying to load a bunch of custom controls that has a "gridview" control along with several text boxes and dropdowns. As the viewstate is really slowing down the system in order to improve the performance I Disabled the ViewStateMode of the parent control (i.e User control) and Enabled the ViewStateMode for dropdowns. But the problem I have is the dropdowns inside the gridview and panel thrw an error even though their ViewStateMode is Enabled. If I Enable the ViewStateMode of gridView or Panel it works fine but that's increasing the size of Viewstate.
Can somebody please make me understand why it is happening and what is the solution for this?
Thanks
BB
If you are getting tons of viewstate.Then I would suggest you two ways that worked for me too
Compress Viewstate - http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx, http://www.hanselman.com/blog/ZippingCompressingViewStateInASPNET.aspx
Trace the layout of the rendering area in html, using div , ul, li wahtever. Now instead of placing grids and other server controls, get the datasource(datatable or dataset), itereate through the items and make the html in the codebehind and write the final html tag on page using a literal. using html controls you can get rid of the view state issues. Also on that html tag you can apply jquery to make it better too.

Set CMS content in .NET

What would be the best approach to set dynamic content from the database to controls in an aspx page?
My database consists of pages (index.aspx, home.aspx and so on) which consists of controls (DivStart, LabelDescription, and so forth).
The first technique that came to my mind was looping through all the controls in the page, looking for controls that have a certain class, e.g. "Cms_DivStart", and would then set the inner html for that control from the database control called "DivStart". The problem is only runat server controls shows up, and I don't want to make all controls server side.
I could store all the dynamic texts for a page in hidden variables and set it with jQuery when the page has loaded, but that would make the text not show up directly..
Any other ideas would be greatly appreciated.
Thanks
/Andreas
You could use asp:PlaceHolder tags, these don't add any html unless they are utilised.
You can write a custom class, I'll call it "CustomPage", that inherits the System.Web.UI.Page class. Then your pages can inherit "CustomPage". In this class, add methods to retrieve data, set custom properties and display your content in the available controls.

Categories

Resources