Scope of controls in asp.net content blocks - asp.net

Just ran into some interesting problem. I've separated a panel from a asp:Content block to another. In the original asp:Content block still resides a asp:FormView with childs, which references multiple ObjectDataSources. These ObjectDataSource are defined in the same asp:Content block.
The separated panel has child controls which are using the same ObjectDataSources. However, because of the separation, these ObjectDataSources aren't accessible anymore...
Simplified situation:
<asp:Content ID="ContentsContent" ContentPlaceHolderID="ContentsContainer" runat="server">
<asp:FormView DataSourceID="FirstSource">
<asp:DropDownList DataSourceID="SecondSource" />
</asp:FormView>
<asp:ObjectDataSource ID="FirstSource" />
<asp:ObjectDataSource ID="SecondSource" />
</asp:Content>
<asp:Content ID="ModelBoxesContent" ContentPlaceHolderID="ModalBoxesContainer" runat="server">
<asp:Panel>
<asp:DropDownList DataSourceID="SecondSource" />
</asp:Panel>
</asp:Content>
I understand that the scope of this Content blocks doesn't allow it... But does anyone have a nice solution to define the same datasource at one place, but still be able to reference them in multiple Content blocks?
Thanks.

set the datasource id on page load
seconddropdownlist.DataSourceID="SecondSource" ;

Related

Container id structure on controls added to placeholder in repeater

Just ran across a weird thing. We have a page with items rendered in a Repeater. The items in the list have one panel for display and a PlaceHolder for edit mode where controls are added in the item data binding.
We have some client side code working with the fields that got added, and the code was expecting the ids to follow the same container structure you usually see, but for some reason, one level is getting skipped and I don't know why.
With the template below, you'd have the outer panel with an id like ...Repeater_ctl46_ctl100_Display. The PlaceHolder itself doesn't render, nor get included in the id structure in the html, but the id of a HiddenField added to the EditPlaceHolder ends up like ...Repeater_ctl46_ctl100_valueHidden.
The _Display level just doesn't appear. This threw off our client side script because it was expecting the outer panel container to be reflected in all the child controls in the edit template, and it was using that to look them up.
Anyone know why the containing Panel wouldn't be in the id hierarchy?
<ItemTemplate>
...
<asp:Panel ID="Display" runat="server">
<asp:Panel ID="ViewPanel" runat="server"><asp:Label ID="ValueLabel" runat="server" /></asp:Panel>
<asp:PlaceHolder ID="EditPlaceHolder" runat="server" />
</asp:Panel>
<asp:Panel ID="ModButtons" runat="server"><asp:LinkButton ID="EditButton" Text="<%$ Resources:Messages,Edit %>" runat="server" /></asp:Panel>
...
</ItemTemplate>
Okay, after a lot of digging, asp:Panel does not implement INamingContainer, which is why the Panel is not participating in the id naming conventions.
INamingContainer is just a marker interface; it has no properties or methods associated.
So if you want a Panel that can participate in naming conventions, you can make a little derivation and use that instead:
public class NamingPanel : Panel, INamingContainer
{...}

Why can't controls in Templates be referenced via their parent?

I've been wondering to why you have to use FindControl to reference the checkbox in the Login1's LayoutTemplate. Example:
var login1CheckBox1 = (CheckBox)Login1.FindControl("CheckBox1");
I would expect to be able to do something along the lines of:
var login1CheckBox1 = Login1.LayoutTemplate.CheckBox1;
In the case of the Repeater below, it is obvious, because there can be n number of CheckBoxes.
But for the Login control, it doesn't seem to make sense. Why wouldn't this be implemented differently?
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</LayoutTemplate>
</asp:Login>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Does anyone have any light to shine on this?
A control added to a page via markup is defined in the designer partial class, generally, at design time.
A control added to a template is generally instantiated programmatically within the control's collection of controls.
Since the control added to the template does not exist at compile-time in the definition of that control, it would be rather impossible to achieve the syntax you're aiming for.
When creating a page in markup, we're using the IDE's facilities to generate a partial class. When defining a template in markup, we're simply setting the value of the ITemplate for that control.

Reusing ASP.NET Controls in Multiple Views

Is it possible to reuse asp.net controls in multiple views within a MultiView? I would like to provide my customers the option to view and entry form as either a ASP.NET Wizard or as a Form depending on their preference. Most of my research has resulted in numerous hits for MVC, but I'm using WebForms and can't find a definitive answer either way.
My theory is that this should be possible, but since the control is already defined elsewhere on the page, I ought to be able to simple tell it to re-display the same control at a different location.
For Example something like this perhaps:
<asp:MultiView ID="mv" runat="server" ActiveViewIndex="0">
<asp:View ID="WizardView" runat="server">
<asp:Wizard ID="wizzy" runat="server" ActiveStepIndex="0">
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server">
<!-- Wrapped in PlaceHolder goodness :P -->
<asp:PlaceHolder ID="wPH1" runat="server">
<asp:Label ID="MyLabel" runat="server" Text="Hello Stackies"></asp:Label>
</asp:PlaceHolder>
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
</asp:View>
<asp:View ID="FormView" runat="server">
<form action="#" method="post" id="wizzyform">
<!-- I WANT TO REUSE THIS CONTROL HERE -->
<asp:PlaceHolder ID="fPH1" runat="server"></asp:PlaceHolder>
</form>
</asp:View>
</asp:MultiView>
UPDATE WITH ANSWER!!
I simply added some PlaceHolders to my Markup and created a toggle button in my VB.NET Codebehind with the following.
Protected Sub ToggleView() Handles ViewToggleBtn.Click
If RequestWizard_mv.ActiveViewIndex = 0 Then
ViewToggleBtn.Text = "Toggle Wizard View"
RequestWizard_mv.ActiveViewIndex = 1
fPH1.Controls.Add(wPH1)
ElseIf RequestWizard_mv.ActiveViewIndex = 1 Then
ViewToggleBtn.Text = "Toggle Form View"
RequestWizard_mv.ActiveViewIndex = 0
wPH1.Controls.Add(fPH1)
End If
End Sub
WOOT!! :D SO HAPPY!! You have no idea how much pain this saves me :P
Note: I've noticed it doesn't maintain state very well, but super easy fix compared to re-writing double the code >.<
At least, you can have one instance of Label in your code-behind and add it programmatically to the desired place by condition using placeholders in both places.
Also you can make a new user control, and place all the logic that covers your Label there. You will still have 2 instances of this control, but you will design your Label once.

How to use ClientIDMode in ASP.NET 4

The default value of ClientIDMode for a page is AutoID. The default
value of ClientIDMode for a control is Inherit. If you do not set
ClientIDMode for a page or for any controls on the page, all controls
will use the AutoID algorithm.
This is from msdn. But when I created a web application in ASP.NET 4 or even 3.5, all of the ids of the control are what I have written for them. They are not generated by the autoId algorithm. Then I tried to manually add clientIDMode="AutoID" to the controls, it also doesnt work what I was expected. So what is the problem ? Is there any rule to make it available ?
Thanks in advance,
EDITED
This is in .aspx page
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:Panel ID="Panel2" runat="server">
<asp:Panel ID="Panel3" runat="server">
<asp:Panel ID="Panel4" runat="server">
(:
</asp:Panel>
</asp:Panel>
</asp:Panel>
</asp:Panel>
</div>
This is output:
<div id="Panel1">
<div id="Panel2">
<div id="Panel3">
<div id="Panel4">
(:
</div>
</div>
</div>
</div>
The reason you are getting all your Id's coming out the way you are is because there is no reason for the .NET framework to change them.
If you had placed your Panel's within a Repeater control, then they would all change to avoid multiple ID's of the same name.
Example (not correct markup):
<asp:Repeater id="repeater1" runat="server">
<template>
<asp:Panel id="Panel1" runat="server">
<asp:Panel id="Panel2" runat="server">
</asp:Panel>
</asp:Panel>
</template>
</asp:Repeater>
Your HTML which is generated from this would show that the Panel id's have been changed to inherit from the containing control. Something like repeater1_ct100_Panel1
The same happens when you are using Master Pages, Content Holders, and DataBound Controls. .NET updates the ID's to avoid multiple ID's of the same name.
The differences between the different ClientIDMode values can be clearly seen if you nest one control into another; the AutoId mode names the control after each parent naming container, while the Static mode starts a new naming hierarchy. Predictable mode is mostly used with data binding controls, while Inherit mode causes the control to use its parent's ClientIDMode.

UpdatePanel with GridView with LinkButton with Image Causes Full Postback

So this might be a fairly specific issue but I figured I'd post it since I spent hours struggling with it before I was able to determine the cause.
<asp:GridView ID="gvAttachments" DataKeyNames="UploadedID" AutoGenerateColumns="false" OnSelectedIndexChanged="gvAttachments_SelectedIndexChanged" runat="server">
<EmptyDataTemplate>There are no attachments associated to this email template.</EmptyDataTemplate>
<Columns>
<asp:TemplateField ItemStyle-Width="100%">
<ItemTemplate>
<asp:LinkButton CommandName="Select" runat="server"><img src="/images/icons/trashcan.png" style="border: none;" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In the ItemTemplate of the TemplateField of the GridView I have a LinkButton with an image inside of it. Normally I do this when I have an image with some text next to it but this time, for whatever reason, I just have the image. This causes the UpdatePanel to always do a full postback.
Instead of changing the markup, you can goto web.config and specify ClientIDMode="Auto" in the pages tag.
Reason why UpdatePanel behaving like this is because the ClientIDMode is getting generated will be too long for UpdatePanel to register. So the ClientID got truncated in middle and such control will be treated like unregistered Control.
For more information read the following:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Change the LinkButton to be an ImageButton and the problem is solved.
<asp:ImageButton ImageUrl="/images/icons/trashcan.png" Style="border: none;" CommandName="Select" runat="server" />
Above solutions also work, But There is one more thing to check. Check form tag for your page. If id attribute is missing, you will get same issue.
If you form tag is as given below (without id), you will get issue:
<form runat="server">
<!-- your page markup -->
</form>
Please add id, as given below:
<form id="form1" runat="server">
<!-- your page markup -->
</form>
You will not need to update ClientIDMode in web.config or page or control.
You will not need to change your linkbutton in markup.
You will not need to register control for asynch postback from code behind.

Resources