james henderson
asked on
reference a control that has been dynamically created
I have a web project created with vs2008 and .net 3.5. I have created a radiobuttonlist control in a table cell dynamically by adding the control to the tablecell.controls collection. The problem is that I can't retrieve this control from code behind on a button click, the tablecell.controls collection returns 0 as the count.
How can I do this? I need to read the radiobuttonlist selection to perform an action.
How can I do this? I need to read the radiobuttonlist selection to perform an action.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Forgot to add the event code:
private void rlb_SelectedIndexChanged(o bject sender, EventArgs e)
{
sOption = ((RadioButtonList) sender).SelectedValue;
}
sOption is a class level string.
private void rlb_SelectedIndexChanged(o
{
sOption = ((RadioButtonList) sender).SelectedValue;
}
sOption is a class level string.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
here is the code behind for the page. it's only for a single page, so there's nothing missing.
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml; //namespace to deal with XML documents
using System.Xml.Linq; //namespace to deal with LINQ to XML classes
using System.Xml.XPath;
using System.Xml.Serialization;
using System.IO;
using TMSSI.BipiDTC;
using System.Web.UI.HtmlControls
namespace TMSSI.BipiDTC.Pradaxa.Bran
{
public partial class branded : System.Web.UI.Page
{
private static string pageName = null;
private string xmlTranslations = HttpContext.Current.Applic
private string xmlPages = HttpContext.Current.Applic
RadioButtonList rl = null;
private string sOption = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pageName = "script";
loadPage();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
fetchNextPage();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
private void fetchNextPage()
{
switch (pageName)
{
case "script":
System.Diagnostics.Debug.W
break;
}
}
private void fetchLastPage()
{
}
private void loadPage()
{
XElement xml = XElement.Load(xmlPages);
foreach (XElement child in xml.Elements())
System.Diagnostics.Debug.W
TableRow tr = null;
TableCell tc = null;
string val = "/webPage[@name='" + pageName + "']";
var pages = xml.XPathSelectElements(va
foreach (XElement item in pages.Elements())
{
System.Diagnostics.Debug.W
switch (item.Name.ToString())
{
case "formTitle":
XAttribute title = item.Attribute("title");
tr = TableSurvey.Rows[0];
tc = tr.Cells[0];
tc.Text = title.Value;
break;
case "narration":
XAttribute loc = item.Attribute("location")
System.Diagnostics.Debug.W
loc.Value += "/eng";
XElement trx = XElement.Load(xmlTranslati
var xlat = trx.XPathSelectElement(loc
tr = TableNarration.Rows[0];
tc = tr.Cells[0];
tc.Text = xlat.Value;
break;
case "control":
XAttribute controlType = item.Attribute("type");
XAttribute controlName = item.Attribute("name");
switch (controlType.Value)
{
case "radio":
rl = new RadioButtonList();
rl.ID = controlName.Value;
rl.SelectedIndexChanged += new EventHandler(rlb_SelectedI
var options = item.XPathSelectElement("o
foreach (XElement opt in options.Elements())
rl.Items.Add(new ListItem(opt.Attribute("te
tr = TableSurvey.Rows[2];
tc = tr.Cells[0];
tc.Text = "Please select an option";
tc = tr.Cells[2];
tc.Controls.Add(rl);
//form1.Controls.Add(rl);
break;
}
break;
}
}
}
private void processPage()
{
}
private void rlb_SelectedIndexChanged(o
{
sOption = ((RadioButtonList) sender).SelectedValue;
}
}
}
ASKER
figured it out. needed to put the code that draws the control in the page_load method.
thanks to all of you for your help.
thanks to all of you for your help.
ASKER
<control name="OptionList" type="radio">
<options>
<option text="Option1" code="OPT1" />
<option text="Option2" code="OPT2" />
<option text="Option3" code="OPT3" />
<option text="Option4" code="OPT4" />
<option text="Option5" code="OPT5" />
</options>
</control>
Here is the code that write the control on the page:
switch (controlType.Value)
{
case "radio":
rl = new RadioButtonList();
rl.ID = controlName.Value;
rl.SelectedIndexChanged += new EventHandler(rlb_SelectedI
var options = item.XPathSelectElement("o
foreach (XElement opt in options.Elements())
rl.Items.Add(new ListItem(opt.Attribute("te
tr = TableSurvey.Rows[2];
tc = tr.Cells[0];
tc.Text = "Please select an option";
tc = tr.Cells[2];
tc.Controls.Add(rl);
break;
}
This displays the control perfectly and the control works as expected, but no event fires and I don't know how to reference the control correctly to try to read it's selection.
I really appreciate you all taking a stab at this.