.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Dynamically showing and hiding controls

Posted By:      Posted Date: May 22, 2011    Points: 0   Category :ASP.Net

Hi All,

I have a requirement to show and hide certain controls based on the values the user has entered in certain other controls. One specific case is a checkbox and a multiline text field. When the checkbox is checked, the text field is visible and when not checked, it is hidden. So I would normally just hang a client side event onto the checkbox, and use that to show and hide the text box as appropriate. In Dynamic Data however, the checkbox is inside a field template, which is totally encapsulated within the whole DynamicControl mechanism, so the question is really how do we implement this kind of functionality within the DynamicData paradigm. At the moment I am assuming I will have to go completely custom and lose all the benefits of dynamic data. 

If anyone has any thoughts, I would like to hear them.




View Complete Post

More Related Resource Links

Dynamically Create Controls in ASP.NET by Using Visual C# .NET

This article demonstrates how to dynamically create a control for an .aspx page. The sample project does the following:
Creates two TextBox controls.
Verifies that the contents (TextBox.text) and the attributes of the TextBox are saved across posts to the server.
Illustrates handling events that are posted by a dynamically-created control

Dynamically Create Controls in ASP.NET with Visual Basic .NET

This step-by-step article describes how to dynamically create controls for an ASPX Web page.

The sample project does the following:
It creates two TextBox controls.
It verifies that the TextBox contents (TextBox.text) and attributes are saved across posts to the server.
It describes how events that are posted by a dynamically created control are handled.

Adding carriage returns between 2 dynamically created controls in an HtmlTableCell


The title pretty much explains it. Here is what i'm trying to do...

 Private Sub LoadGrid_CommentsField(ByVal Row As HtmlTableRow, ByVal BookingNumber As String, ByVal CommentsText As String)
  Dim Txt As TextBox
  Dim Cmd As ClickOnceButton.ClickOnceButton

  Txt = New TextBox
  Txt.ID = "txtComment_" & BookingNumber
  Txt.CssClass = "TFAppControls"
  Txt.TextMode = TextBoxMode.MultiLine
  Txt.MaxLength = 500
  Txt.Width = New Unit(95, UnitType.Percentage)
  Txt.Text = CommentsText

  Row.Cells(cComments).InnerHtml &= "<br/>"

  Cmd = New ClickOnceButton.ClickOnceButton
  Cmd.DisableAfterClick = True
  Cmd.Text = "Save Comments"
  Cmd.ID = "cmdSaveComments_" & BookingNumber
  Cmd.CssClass = "TFAppButtons"
  Cmd.EnableViewState = False
 End Sub

On the line where i try and update the InnerHtml, i get the following error; "Cannot get inner content of  because the contents are not literal."

How to dynamically create user controls ?



can you explain how to create user control dynamically in a web form ?When i am creating server controls of user controls are not instantiated and it is not getting initialized .when i am tyying to initialize its properties error occurs tha object instance not created .

Any suggestion ? 

Hiding and showing the columns depends upon the filter condition

H,   We developed around 70 ssrs reports for our new application.Its like,all the report names are menrioned in the left hand side and if we click it,.Net calls the SSRS reports and shows the filters in the top of Right hand side.   We will select some filters there and then 'GO', it will fetch records. The issue now is, assume we have 6 columns in the report and depends upon the selection in the filter area, the columns should vanish.is it possible?

how to apply validation controls on Gridview which is created dynamically

 hi, i created a gridview with two columns and 8 rows dynamically. All the rows are empty and editable at runtime. I am giving column names for this grid from some other .aspx page by passing column names using querystring.Because i am using this grid for three different pairs of column names.  i would like to know how to apply Required Field Validator, Regular Expression Validator  and Range Validator on Gridview textboxes. So that by using those validators i want to validate user inputs. And i would also like to know how to add rows at runtime for this grid. I am using ASP.net 2.0 and C#.net for this.(Visual Studio 2005) The code i am using to create gridview is below. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoadData.aspx.cs" Inherits="LoadData" %>  <form id="form1" runat="server">  <div> <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False" ForeColor="#333333" BorderColor="GradientInactiveCaption" BorderStyle="Solid" style="z-index: 108; left: 24px; position: absolute; top: 13px">  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  <RowStyle BackColor="#EFF3FB"

How to I apply an XAML declared animation to multiple controls dynamically?

Hello, How to I apply an XAML declared animation to multiple controls dynamically? Here is the XAML declared animation: <Window.Resources> <Storyboard x:Key="TextBlock_SB"> <DoubleAnimation Storyboard.TargetName="TextBlock_1" Storyboard.TargetProperty="(TextBlock.Opacity)" From="1.0" To="0.75" Duration="0:0:0.5" AutoReverse="True" RepeatBehavior="Forever" /> </Storyboard> Here is the code that starts the animation for TextBlock_1: (this.Resources["TextBlock_SB"] as Storyboard).Begin(); I want to apply the same storyboard/animation to TextBlock_2 and TextBlock_3 and perhaps others.  How do I do that without defining more XAML storyboards? Regards, Robert  

Cannot retrieve values of dynamically generated controls

Hi All, I am creating a user control which contains checkboxes and labels that needs to be created on fly. The dynamic generation of these controls depend on the values retreived from a SPlist. I have several panels that display items queried from the list on the basis of a selection criteria. On a button click event I need to capture all the selected values from the controls that were generated dynamically.  When I traverse each dynamic control that I added to the panel, I dont see their values. I tried using recursive retrieval of the control from the page as well but it just behaves crazy, it treats a radio button as a checkbox and returns a value that I am not looking for. I am not using the the page_load() or any other event, I use the button handler on my custom method. I was thinking for ViewState, ControlsState or SessionState. I assume ViewState may not work on this as the dynamically generated controls need to be in the page_load event. Please guide as this getting way more complicated than what I had ever thought of it to be.

Ajax Controls are not showing at all.

Hi,This is my first Ajax testing page and after configuration, Ajax controls are not visible on test page at all. I dont know whats going wrong with it.I have two CalanderExtender and one editor control. Its a very basic page and simple code. I am not getting any compiler warnigns.following is my code.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor" TagPrefix="cc1" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </cc1:ToolkitScriptManager> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <cc1:CalendarExtender ID=&qu

Dynamically adding User Controls -- newbie question!

I am new to ASP.NET (coming from ColdFusion, thank goodness I'm away from that!).  And I am have THE hardest time understanding how to work with forms in ASP.NET.  Here's what I am doing:Dynamically add User Controls to a PlaceHolder based on some int value (which currently is passed in a HiddenField).Fire an event if the WebControls inside of the User Control change, then handle that event in the Main Page (i.e. save the input to DB)When the user clicks the "Next/Continue" button, the next controls for the next page are loaded.And I guess maybe I just can't seem to get how things work in ASP.NET, because I am really struggling with the general idea of how ASP.NET flows.  Should I be dynamically loading the controls in Page_Init, or Page_Load?  Or should I load the controls when the buttonclick event is fired?Do I have to add the axCtrl.OnDataChange += new EventHandler(HandleAxControlDataChange); line on Page_Load?  Or should I put this in When I first load the UserControl, I need to initialize some WebControls within the UserContorl.  Is there a way to know if I have loaded new user controls into my placeholder?  I can't use Page.IsPostback, because after the first page, every page is a postback from the buttonclick.Any help for this confused newbie would be GREATLY appreciated!!Here's what I have: pu

Permissions:disable controls by roles(access pages too)(dynamically :each new role has special grant

HelloI have an application of many pages of consultation and same time I have a module of adding users by roles and creating accounts by role.(the application is ready but i cant manage the roles)the tutorials that i found are very useful to manage roles but its roles fixed before(administrators,supervisors)(http://www.asp.net/security/tutorials/creating-and-managing-roles-vb) if I add a new role (agent) i want to grant to this new role a new access to pages(different access than admin and supervisor) and disable new controlswhy not using checkbox to every new role checking a field related to databaseis it possible?if yes ,how can i begin?Thanks

Rearrange dynamically visible controls on a page

Hello,Here is what I am trying to do.I have a drop down list along with a 'Add' button (which are always visible). There are some other 'Label', 'TextBox', 'Button' in the page (which is not visible on load). Depending on the user selection from the first dropdown (and when click on 'Add' button), I would like to dynamically set some of the Controls visible.For example (as per below code): When User selects 'Email' ... Label3, TextBox3 and Button3 will be visible. Again when user will click Button3, all controls will be invisible. I was able to Show and Hide those controls as per user selection and button click. What I would like to do is, rearrange the controls. For example, If user selects 'Email' and click 'Add' button (Button5), It would display 'Label3', 'TextBox3', 'Button3'. Then if user selects 'Name' from the dropdown and click 'Add' button again, it would display 'Label1', 'TextBox1', 'Button1' below the Email controls. As of now it is apperaing on sequence as listed in html (Name controls always come first when visible, then 'Phone', then 'Email'). I would like to display the controls as sequesnce as user selects.And next is when we will click the Button3 (which will hide the email controls), All other controls below email co

Retrieve Dynamically Created Controls

I am building a webform dynamically but when I go to get the values the placeholder has no controls.  Every control added has an unique ID.  Does anyone know what I am doing wrong (language is C#).      aspx page <asp:UpdatePanel ID="upShoppingCart" runat="server" UpdateMode="Conditional" >                    <ContentTemplate>                        <asp:PlaceHolder ID="phShoppingCart" runat="server" />                        <br /><asp:Button ID="btnUpdateCart" runat="server" Text="Update" OnClick="btnUpdateCart_OnClick" />                    </ContentTemplate>                </asp:UpdatePanel>   .cs page         private void CreateShoppingCart()     &nb

Creating (hiding/showing) admin menu for my Website?

Hi there!I am currently developing a website for a friend of mine who is a dj. I have a login page, which works fine however i want to inplement a menu that only users with the Administrator role can view. I have done alot of research and i have found ways to stop a user from visiting a page, when they click on it it takes the to the login page. But i want to completely hide the menu from Non Admins. Any suggestions would be greatly Appreciated.Thanks in advanced,Jordan.

How do I dynamically change and display the group of controls/content in wpf

The requirement for me is, based on some condition I need to show different group of controls on the screen. Gow do I achive this?

Dynamically created controls not accessible after post back.

Dear Friends, pls give the solution. code given below.. dynamically HtmlInputCheckBox created and add in PlaceHolder(panel). I can't get the created control values after post back..Dynamic Control Creationprivate void GetPhotos() { HtmlImage img; HtmlInputCheckBox ic; panel.Controls.Clear(); DataTable dtPhotos = DLayer.Qry_WithDataTable("ADM_GET_HotelPhotos"); foreach(DataRow drPhotos in dtPhotos.Rows) { if ( File.Exists(Server.MapPath("../Photos/Hotel/" + drPhotos["FileName"].ToString()))) { ic = new HtmlInputCheckBox(); ic.Value = drPhotos[0].ToString(); if (drPhotos["PStatus"].ToString() == "1") ic.Checked = true; else ic.Checked = false; panel.Controls.Add(ic); img = new HtmlImage(); img.Src = "~/Photos/Hotel/" + drPhotos["FileName"].ToString(); img.Alt = drPhotos[1].ToString(); img.Attributes.Add("title", drPhotos["PhotoCaption"].ToString()); panel.Controls.Add(img); } } }Get checkbox values foreach (Control ctbl in panel.Controls)        {            if (ctbl is HtmlI

Enable/Disable controls dynamically based on User settings

In my project I have a requirement that all controls in each window shall be enabled or visible dependent on user and the admin shall be able to change is a user will be able to use or see the control. I made a very simple test application that creates a user and gives him a list (will be loaded from db in real app) of the name of the controls in the window and also his settings. Then after initializing the window I use reflection to find the controls and set their isenabled/visibility properties. This solution does the thing but I'm wondering if there is a better solution. The main issue here is that each control must be set per user and thus roles wont work appropriate and also that I need to be able to change the settings at runtime.  
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend