Adding a property to a user control, setting property at design time, then using value in control markup

 I have made a custom control that outputs a bunch of html. I have added a property through the code behind like this: 

    private string sectionID;

public string SectionID
{
get
{
return sectionID;
}
set
{
sectionID = value;
}
}
 This property is now showing up in Intellisense, and I can specify the value when adding the control to my page like this: 
<ccl:MyControl runat="server" SectionID="2" />
 I now want to use this value in the markup that the control outputs. I think I have the wrong syntax. Can someone please check? This doesn't build: 
<div id="section<%# SectionID; %>" >
 

Disco Patrick writes web applications using:

Visual Studio 2008
ASP.NET 3.5
C#
SQL Server 2008
0
Disco
2/25/2009 3:28:24 PM
asp.net.custom-server-controls 3498 articles. 0 followers. Follow

6 Replies
1662 Views

Similar Articles

[PageSpeed] 38
Get it on Google Play
Get it on Apple App Store

I'm no expert in fact I am waiting for an anwer to one of my questions, but

in your case what I would probably do is create your div at runtime then you will be able to get the value which can be added as the id

 


jnc
0
jnc
2/25/2009 5:37:27 PM

 I appreciate you can create these things at runtime.

However, if it is possible to do in a way that is similar to what I am trying to do, i.e. by injecting the value into the markup, I would like to know about it.

If it is not possible, I would like someone to confirm this so I can move on!

Thanks.


Disco Patrick writes web applications using:

Visual Studio 2008
ASP.NET 3.5
C#
SQL Server 2008
0
Disco
2/25/2009 8:14:54 PM

I understand, this builds on mine, I have not attempted to reference the new id in the div which I presume is what what you want to do 

you might try passing the value to a hidden field then getting the value from the field using getelementbyid("hiddenfield").innettext
 <div ID='<%# DataBinder.Eval(Container, "DataItem.AppData") %>' >
   <asp:Button ID="Button1" runat="server" Text="Button" />
</div>

you might try passing the value to a hidden field then getting the value from the field using a javascript function, can I ask why you want to do this?

 


jnc
0
jnc
2/26/2009 12:06:09 PM

 Hi jnc,

Here's the full story... I tried to create a complex form using the Wizard web control. However I soon discovered that this control outputs its HTML in tables. I wasn't happy with this, so set about creating my own page to reflect the functionality of the Wizard.

So I created a div for each section of the form, in the same way that  the Wizard has a WizardStep for each section. The code inside each of these divs was the same, so I decided to create a user control that I could re-use. Each div contains a number of questions that are taken from the database. Each question belongs to a specific section, which is why I added the SectionID property to the control, so the control would only fetch the questions related to that section.

Then I created my own sidebar to switch between sections - just like the Wizard sidebar. The buttons call a javascript function, passing a section number to the function. The function then un-hides the correct div by looking for the div that has an id of "sectionN". And that is why I need to give each div a specific ID!!!

This is what I need to understand:

jnc:

<%# DataBinder.Eval(Container, "DataItem.AppData") %>' >



Can you explain this syntax to me? Did you have to set up anything in the code-behind to make this work? What is DataItem?

Disco Patrick writes web applications using:

Visual Studio 2008
ASP.NET 3.5
C#
SQL Server 2008
0
Disco
2/26/2009 12:42:25 PM

 I found this to work: 

<%# DataBinder.Eval(Container.DataItem, "columnName") %>
 Turns out you don't need to set up any additional stuff.

Disco Patrick writes web applications using:

Visual Studio 2008
ASP.NET 3.5
C#
SQL Server 2008
0
Disco
2/26/2009 1:06:26 PM

good news

I did once do a database driven page which collected the data in code behind then built a grid using a <table>, I added IDs to each cell similar to spreadsheet coordinates but all, as I said in code behind

I used javascript to control the editing and extract the values

 


jnc
0
jnc
2/26/2009 1:25:02 PM
Reply:

Web resources about - Adding a property to a user control, setting property at design time, then using value in control markup - asp.net.custom-server-controls

Property - Wikipedia, the free encyclopedia
This article may require copy editing for grammar, style, cohesion, tone, or spelling . You can assist by editing it . In abstract, property ...

Property - Wikipedia, the free encyclopedia
In abstraction, property is that which belongs to or with something, whether as an attribute or as a component of said thing. For the significant ...

Thousands of dollars of property stolen from Port Augusta home during Christmas break-in - AdelaideNow ...
DEFENCE medals and unique jewellery are among thousands of dollars of property stolen during a break-in at a Port Augusta home over Christmas. ...

Facebook's Mark Zuckerberg faces trial over Silicon Valley property deal
... broke a promise to introduce him to Silicon Valley's elite. Facebook boss Mark Zuckerberg is headed toward a trial after all as the property ...

Canberra's rental yields on the rise, despite sluggish property market
Canberra has experienced the lowest annual shift in rental prices in Australia over the past decade.

Mount Isa Police crackdown nets 20 people over property theft - The Courier-Mail Search Search
ABOUT 20 people have been charged, including 17 juveniles, after a series of break and enters and thefts in Mount Isa.

Bomb squad called to Qld property
TWO men have been charged after police found explosives at a property in Helidon.

Warning of 'unfair' process unheeded as government forges ahead with property resumptions
Private property is being forcibly resumed by the NSW government for major infrastructure projects such as WestConnex using a system it was warned ...

Property damage, power out in Waroona fire
At least one old property has been destroyed by intense fires that hit Waroona and Preston Beach. Vision courtesy of 9 News Perth.

Spiking property assessments may leave thousands without Home Owner Grant
A spike in property assessments in Metro Vancouver means thousands no longer qualify for the Home Owner Grant, unless the provincial government ...

Resources last updated: 1/15/2016 6:23:28 AM