Generating Control Markup Based On A Dynamically Generated Control?

Dec 9, 2010

What I'm trying to do is take an instance of a control, and figure out what the markup would look like based on the property values set in the code behind. We've built a web-based web form designer. At the end of the design process we save the markup. For simple controls with supported types, generating the markup was easy. For complex controls with nested properties or unsupported types, we're faced with creating a custom markup generator for each custom control. I was hoping to find info out there that would jump start this development effort.

In simple terms, I have this:

Dim Ctl as Control = CType(Asm.CreateInstance("MyNameSpace.MyControl"), Control)
Ctl.Name = "Name of my control"

And I want to end up with this:

<cc1:MyControl ID="MyControl1" runat="server" Name="Name of my control" />

View 1 Replies


Similar Messages:

Web Forms :: Client ID Of A Dynamically Generated Control?

Feb 23, 2010

I've recently added a Master Page to my site,but realized that it's caused some problems in my existing JavaScript setup.I am able to use the following code in most places without any issue to get the ClientID: '<%=myControl.ClientID%>'

The problem I have encountere is that I have several pages that add controls dynamically,and then execute events based on an ID that's passed in,where I can't use this line,because I won't know the ID of the control I'm trying to access until the Javascript executes.Here's a simplified version of the problem:

When the page runs,it creates n textboxes dynamically,and appends a relevant ID from the database to the end of the ID.Let's say the page has added 3 textboxes with the following IDs:

myTextBox1

myTextBox7

myTextBox115

Later,a javascript event fires,and I need to be able to access the control through JavaScript like this:

myJavaScriptFunction (controlID)

{
myvar = document.getElementById('myTextBox' + controlID);
}

View 1 Replies

Dynamically Generating The Nodes In Treeview Control And Displaying The Results On Selecting The Node

Mar 29, 2011

I am using a treeview control and just want to add nodes dynamically. On selecting a node the results based on that node must be shown below.

View 1 Replies

How To Use The FindControl Function To Find A Dynamically Generated Control

Jun 10, 2010

I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:

TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);

I am using the following code to retrieve the values that have been entered into the TextBox:

foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}

But it never finds the control. I've looked at the source code for the page and the control i want has the id:

ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres

For some reason when I look at the controls in the ListViewDataItem it has the ClientID:

ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00

Why would it be changing Numberofacres to ctl00? Is there any way to work around this?

UPDATE:

Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what @Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?

If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?

UPDATE 2:

So i changed the code i had in my Page_Init event from:

protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}

to:

protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}

And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.

View 1 Replies

Forms Data Controls :: Dynamically Generating Update/delete Statements For A DetailsView Control?

Dec 1, 2010

In my Admin page, I have a drop down list containing all current user tables in the database. The user chooses one.

My DetailsView below uses that parameter to populate itself with data from that table. The stored procedure used to select the data passes a string to sp_executesql. The string is concatenated from a select statement and the tablename parameter. Can I do something similar for Update/Delete statements? I'm not sure of the best approach, given that I won't know which/how many fields will be in the DetailsView at runtime, and which of these are keys - the choice of table depends on the user. I think Insert will be straightforward - am I right? I would like to streamline things as far as possible. Is it possible to dynamically pass in the values in, say, a string, and get my stored procedure to understand them? And what's the best method of grabbing the values from the DetailsView in this situation?

View 4 Replies

AJAX :: Control Based Control Doesn't Work - How To Implement It On Control Based Control

Jan 14, 2010

i'm trying to develop a ASP.Net control. If it based on System.Web.UI.Control id doesn't work correctly with Ajax.Net updatePanel. If I change it to System.Web.UI.WebControls.WebControl the it works fine with Ajax controls. What is the difference between them and how I implement it on Control based Control?

View 2 Replies

Sql - Dynamically Generating Buttons Based On Query Results

Dec 20, 2010

I am trying to add some filters to a gridview that I am working on, and I'm wondering if it is possible to generate links or buttons above the gridview based on the years returned in the dataset. For example, if my dataset contains dates from 2001, 2009, and 2031 in my date column, I would like to be able to take that data from my SQL query (getting a distinct list of years is not an issue), and generate the buttons. From there, I would filter the data in the gridview based on the user clicking the buttons.

Is dynamic generation of buttons possible in ASP.NET? I do have some other ideas of how to accomplish the same functionality, but I would prefer to do it this way.

View 1 Replies

Web Forms :: Populating A Dynamically Generated Checklist Control In Code-behind Not Working?

Feb 5, 2011

(The following is a complete re-edit of the original post which was rambling, confusing etc...)

I have a form with a dynamically generated checklist:

<asp:CheckBoxList ID="cblGames" runat="server" DataSourceID="sqlGames" DataTextField="Game" DataValueField="Id" </asp:CheckBoxList>
<asp:SqlDataSource ID="sqlGames" runat="server" ConnectionString="<%$ ConnectionStrings:Games %>"
SelectCommand="SELECT [Id], [Game], [ShowWaitingList] FROM [Aanbod] WHERE ([ShowWaitingList] = @ShowWaitingList)">
<SelectParameters><asp:Parameter DefaultValue="true" Name="ShowWaitingList" Type="Boolean" /></SelectParameters>
</asp:SqlDataSource>

The following code-behind is supposed to check those boxes that correspond to values stored in a database. It fails and I don't know why.

[Code]....

View 3 Replies

Forms Data Controls :: Programmatically Creating Chart Control And Adding It To Dynamically Generated Table?

May 21, 2010

I am stuck in chart controls...Here is my prob... There is a ListBox containing some items...when user selects multiple items from the list box I want to generate a dynamic table with the number of columns same as that of the number items selected in the listbox. And for each selected item I want to show a seperate chart in the columns...Currently I want the same chart control for every selected item (i.e. a static hard coded chart that i will replace later by dynamic values)....I am using a method that draws a chart control using a sample dataset... I am calling it each time when a new column is created..Also the DrawChart method executes for the first column only and throws an index out of range exception! after the first execution...my code is not working...here

my code...

[Code]....

[Code]....

View 1 Replies

Web Forms :: How To Render Control Dynamically Based On Configuration

Mar 25, 2010

I have come across a requirement to dynamically show or hide asp.net web controls , html server controls. I want to do this through a configuration XML (as it would be easier to config). So each page /aspx will have a configuration xml.

Would it be possible to create a control (may be a custom control , to which i can attach the xml file). The control (lets says - call it sequencer) should decide which control should be ordered in which order and if the control has to be shown or not. Ideally this sequencer control should use the page events and take decision on rendering the child controls.

Some thing like this

aspx -

<Sequencer XmlPath=".." Id="S1" Runat="server">
<Textbox id="t1" runat="server" />
<Textbox id="t2" runat="server" />
<Textbox id="t3" runat="server" />
</Sequencer>

This is hiw xml config may look like -

<Sequencer Id="S1">
<control id="t1" sequence="2" show="true" />
<control id="t2" sequence="1" show="true" />
<control id="t3" sequence="3" show="false" />
</Sequencer>

So in this case t2 should be rendered first then t1. The control id in the xml and the aspx has to be same.

t3 should not be rendered at all on the screen.

Which page load event should I use.

how I can create this kind of a custom control that does the above for me.

View 1 Replies

Forms Data Controls :: Dynamically Load User Control Based On The Count?

Mar 17, 2010

Im working with asp.net web application,I need to get multiple gridviews dynamically based on the count (which i get from database) for that i took a web user control and i added a gridview there . iwant to load that usercontrol in aspx.cs based on the count..

but im able to bind only one grid view...

View 14 Replies

Generating Clean Markup With Literal And String.Format

Jun 2, 2010

In order to generate clean markup, I often resort to using code similar to this:

<asp:Literal ID="ltItem" runat="server">
<li class="{0}"><a href="{1}">{2}</a></li></asp:Literal>

And in the codebehind:

[code]....

Therefore my question:

Is this a common way to generate clean markup? Are there better alternatives? Is the databinding syntax approach preferable?

View 1 Replies

C# - Selecting And Building A Control From A Control Library Dynamically, Using The Control Name From A Table?

Feb 1, 2010

I have a custom DDL that lives in my Server control library. Currently I add this control to a table by strongly typing the name of the control, the customary way.

Control_Library.Report_NumberDDL newDDL = new Control_Library.Report_NumberDDL();


What I want to be able to do is dynamically create that control by pulling the control name from a table. So I would have the name of the control in my code, in this case "Report_NumberDDL", and I would like to then create the control without having to strongly type it.

Something like this, though I know this doesn't work:

string controlName = "Report_NumberDDL";

Control_Library."controlName" controlNum1 = new Control_Library."controlName"();

Edit:I tried to do this:

Type type = Type.GetType("Control_Library.Report_NumberDDL");
object control = Activator.CreateInstance(type);

But on the CreateInstance(type) I get a null value exception. So the Type isn't getting created correctly.

View 1 Replies

ASP.NET Websites Are Compiled But With More HTML Markup Generated!

Nov 21, 2010

Hi,

Everytime I view the source of ASP.NET website I found alot alot of HTML markup generated. I even tried Visual web Developer and design a simple page and then do the same thing with PHP and I found ASP.NET generates more HTML!

Now, how can ASP.NET be faster if it is generating HTML in this way!

View 2 Replies

VS 2008 - Remove CellSpacing / CellPadding / Border From Generated Markup?

May 6, 2010

I'm using a <asp:Login> control and it's automatically putting CellSpacing, CellPadding, and Border attributes into the generated HTML table. How can I stop ASP.NET outputting these attributes (without moving to ASP.NET 4)?

View 7 Replies

Ajax.ActionLink Generated Markup Lacks Onclick Attribute?

Mar 16, 2011

I've got problem with generating ajax anchors. I'm using simple

Ajax.ActionLink("test", "Test", new AjaxOptions { UpdateTargetId="test", HttpMethod="GET" }) and the generated markup is:
<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#test" href="/Home/Test">test</a>
which, obviously lacks the onclick="Sys.Mvc.AsyncHyperlink.handleClick(...)" attribute.

View 1 Replies

Binding Related Control From Markup?

Nov 14, 2010

I have a control that looks along the lines of this:

public class MyControl : Control
{
[Bindable(true)]
public MyControl MyControlParent { get; set; }
// snip
}

In my markup I want the following:

<tag:MyControl ID="foo" runat="server">
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
</tag:MyControl>

The reason for me wanting to be able to name a parent like this is because I would also like to be able to do something like this:

or

<tag:MyControl ID="foo" runat="server">
<tag:MyControl ID="foobar" runat="server">
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
</tag:MyControl>
</tag:MyControl>

or

<tag:MyControl ID="foo" runat="server"></tag:MyControl>
<!-- snip -->
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>

I've tried adding a type converter, but it fires before HttpContext.Current.Handler is set which means I can't search the page controls for the desired MyControl instance. Parent-child relationship in this control imparts certain inheritance of property values and doesn't (read shouldn't) necessarily imply a parent-child relationship in the control tree.

View 1 Replies

Forms Data Controls :: Unhiding Control In FormView Based On Another Control's Value?

Feb 8, 2010

I am attempting to show/hide a control (txtInpatientType) when a user views individual records on a FormView (frmData) depending on the value stored in another control (txtPatientType). For example, if txtPatientType.Text = "Inpatient" then txtInpatientType.Visible needs to be True. Very simple code follows.

[Code]....

txtPatietType and txtInpatientType are both ReadOnly. txtInpatientType.Visible = false by default.

View 17 Replies

.net - Markup Code Inside Custom Server Control?

Dec 29, 2010

I'm new to custom server controls and I'm hoping to use them to build various 'modules' for different sites I build to cut down on duplicate code.I'm familiar with custom user controls in which I can create an .ascx & .ascx.vb file which can then be imported into an .aspx page and used freely.However, with custom server controls I cannot find a way of using markup/html code. Is this possible at all or must all code be created programmatically?

View 1 Replies

How Does The Child User Control Inherit The Markup From The Base Class

May 25, 2010

I have a user control (uc1) which inherits from uc2. uc2 has a user control (uc3) declared in the markup. I am trying to access uc3 from uc1 but I get NullReferenceException. I thought due to inheritance uc3 would instantiate but looks like I am missing a step.

Clarification:

How does the child user control inherit the markup from the base class? The server controls in the base user control are null in the code behind of the base user control. Why?

View 4 Replies

Security :: Displaying Role-Specific Markup With The LoginView Control?

Oct 14, 2010

I want to know how can I display role-specific markup using loginview?

I have these three roles, Admin, Editor, Moderator

Each and every user will belog to only one of the following role groups

1. Admin, Editor, Moderator (Some user/s will have all these three roles)
2. Admin, Editor (Some user/s will have all these two roles)
3. Admin, Moderator (Some user/s will have all these two roles)
4. Editor, Moderator (Some user/s will have all these two roles)
5. Admin (Some user/s will have all only this role)
6. Editor (Some user/s will have all only this role)
7. Moderator (Some user/s will have all only this role)

Now I want to display some links that will be specific for each role group to which one or more users will belong. So the links I want to display for these role groups are following

1. Admin, Editor, Moderator => Only Links of all the pages of Admin, Editor and Moderator
2. Admin, Editor => Only Links of all the pages of Admin and Editor
3. Admin, Moderator => Only Links of all the pages of Admin and Moderator
4. Editor, Moderator => Only Links of all the pages of Editor and Moderator
5. Admin => Only Links of all the pages of Admin
6. Editor => Only Links of all the pages of Editor
7. Moderator => Only Links of all the pages of Moderator

I have tried doing this using RoleGroups in Loginview but couldn't succed.

Can anybody let me know the correct way of going this?

View 13 Replies

Accessing User Control Properties Defined In Code Behind In Markup?

Feb 9, 2011

I have a public property defined in code behind of a user control. I want to toggle visibility of controls in the markup based on the property's value.

However, using the following syntax:

<td style="display:<%#(Container.PageControlMode == PageControlMode. PageMode.Wizard) ? "none" : "inline" %;">

I have the following property in my code behind:

public PageMode PageControlMode { get; set; }

Does not work and generate errors.

View 1 Replies

Create ASP User Control With Inner Properties Used With Markup Similar To UpdatePanel?

Nov 5, 2010

I am creating a user control that I wish to have inner properties as well as normal attributes. Although the actual function of the control has nothing to do with, say, UpdatePanel, I am trying to create something with similar ASPX markup. The developer designing the page should be able to use my control like:

<ns:MyControl ID="someID" runat="server" SomeOtherAttribute="true">
<ContentTemplate>
<asp:Label ID="someLabel" runat="server" Text="Normal page markup and controls should go here" /><br />
<p>This should be OK too.</p>
</ContentTemplate>
<ControlEvent ControlName="idOfOtherControl" Event="Click" />
<ControlEvent ControlName="idOfSomeOtherControl" Event="MouseOver" />
</ns:MyControl>

Also acceptable would be wrapping the ControlEvent tags in some other tag, more like what happens when using UpdatePanel:

<ns:MyControl ID="someID" runat="server" SomeOtherAttribute="true">
<ContentTemplate>
<asp:Label ID="someLabel" runat="server" Text="Normal page markup and controls should go here" /><br />
<p>This should be OK too.</p>
</ContentTemplate>
<ControlEvents>
<ns:ControlEvent ControlName="idOfOtherControl" Event="Click" />........................

View 1 Replies

C# - Give A Custom Control Ability To Parse XML Markup To A Collection?

Apr 1, 2010

I'm writing a custom ASP.NET webcontrol and would like it to have a collection of custom items which can also be specified in the XML markup. Something like this:

class MyControl: WebControl
{
public IList<MyItemType> MyItems { get; private set; }
}

And in the markup:

<asd:MyControl runat="server" id="mc1">
<MyItems>
<MyDerivedCustomItem asd="dsa"/>
<MyOtherDerivedCustomItem asd="dsa"/>
</MyItems>
</asd:MyControl>

How do I do this? I though this was all about implementing some interface on the collection or adding some special attributes to the property, but nothing I do seems to work.

View 2 Replies

Event Handler For The Activity Control In The Markup Of The Aspx Page?

Jun 24, 2010

I have created a web user control called Activity. I have defined a public-facing event on that web user control called OnActivityDelete. There is a delete button in the Activity control. When the delete button is clicked, the Activity control fires the OnActivityDelete event. I am using this web user control in a repeater. I assign an event handler to the OnActivityDelete event on the repeater's item data bound event. When I click the delete button for the Activity control, the event fires from the Activity control, but it never hits the event handler in the page that's using the control. (I have stepped into the code with the debugger and confirmed this behavior).

My suspicion is that this behavior has something to do with the fact that the event handlers are added in code behind when I bind the repeater to a datasource, which I only do if the page is not posting back.Is it possible to define the event handler for the Activity control in the markup of the aspx page? If so, will this solve my problem?If not, do I have to bind the repeater and hook up to the events every page load in order to solve my problem (this works, I just tested it), or is there some viewstate trick to getting the events to persist? Markup of Repeater on page:

<asp:Repeater ID="rptrActivites" runat="server" EnableViewState="true">
<ItemTemplate>
<div class="activity">[code]...

Page's code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
GetActivities() [code]....

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved