User Control doubt:

C

CSharp

Hello all,

I have in my project this user control I made:
public class PollControl : System.Web.UI.UserControl

I have properties on its code-behind like:

****************************
private string _name;
private ArrayList _choices;

public string Name
{
get {return _name;}
set {_name=value;}
}
public ArrayList Choices
{
get {return _choices;}
set {_choices=value;}
}
**********************

I added the control to a webform:
<%@ Register TagPrefix="CustCtrl" TagName="Poll" Src="Poll.ascx" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="Poll.WebForm1" %>

<form id="Form1" method="post" runat="server">
<CustCtrl:poll id="id1" runat="server"
Name="ProgrammingPoll"></CustCtrl:poll>
</form>

As shown, I can assigne a value to the Name property by specifying a value
in the Name attribute in the HTML. But what if I want to programmatically
pass an ArrayList to the Choices property from the webform's code-behind?

How can I get a reference at the codebehind to the Poll control I added on
the web form HTML interface, so that I can do:

id1.Choices= arrayListXYZ;

Thanks in advance
CSharp
 
A

avnrao

u need to load the control dynamically in the code behind and add it to page
collection.

UControl myControl = Page.LoadControl("~/UControl.ascx");

to place it in a proper position, add a panel in design mode and add your
control to the panel.

Av.
 
C

CSharp

Thanks for the solution.
I found out that it also works like this though:

I can add the control, to the webform html as I said.
In the code behind, I add this declaraion:
protected Poll.PollControl id1;

And now I can access the control in the code behind too.

Thanks anyway
CSharp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top