E
Eric Legault [MVP - Outlook]
I'm having a hard time with RadioButtonList controls, not to mention that
I'm pretty new to ASP.NET. I need to create all controls on the page
dynamically because I'm building a SharePoint Web Part. For testing
purposes, I'm designing the page using a Web Form to see if the layout is
correct and to verify that I can access all of the controls.
What I want to do is create a table with X rows, each having an identical
RadioButtonList containing the same three values. My Web Form has only one
control on it in design time, a PlaceHolder control which must serve as the
container for my RadioButtonList controls (I don't need the PlaceHolder
control in the Web Part).
Questions:
1) Do I have to create multiple RadioButtonList controls, one for each row?
Or is there a better way? DataRepeater? Control arrays?
2) The important one - how the heck do I grab the selected value for my
RadioButtonList in the Button1_Click event? The FindControl method for the
Page and PlaceHolder controls (which I assume "owns" it) returns nothing.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm3.
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
ListItemCollection listBoxData = new ListItemCollection();
// Add items to the collection.
listBoxData.Add(new ListItem("apples"));
listBoxData.Add(new ListItem("bananas"));
listBoxData.Add(new ListItem("cherries"));
RadioButtonList RadioButtonList1 = new RadioButtonList();
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
RadioButtonList1.DataSource = listBoxData;
RadioButtonList1.DataBind();
Table Table1 = new Table();
Table1.ID = "Table1";
Table1.BorderWidth = 5;
TableRow TableRow1 = new TableRow();
TableCell TableCell1 = new TableCell();
TableCell1.Controls.Add(RadioButtonList1);
TableRow1.Cells.Add(TableCell1);
Table1.Rows.Add(TableRow1);
HtmlButton Button1 = new HtmlButton();
Button1.InnerText = "Submit";
Button1.ServerClick +=new EventHandler(Button1_Click);
PlaceHolder1.Controls.Add(Table1);
PlaceHolder1.Controls.Add(Button1);
}
private void Button1_Click(object sender, System.EventArgs e)
{
RadioButtonList R1;
//Find the RadioButtonList
R1 = (RadioButtonList) PlaceHolder1.FindControl("RadioButtonList1");
//R1 = (RadioButtonList) Page.FindControl("RadioButtonList1");
//Get the RadioButtonList value
if(!(R1 == null) ) //this is always null!!!
{
if (R1.SelectedIndex > -1)
{
string X = R1.SelectedItem.Text;
this.Context.Response.Write(X);
}
}
}
}
}
I'm pretty new to ASP.NET. I need to create all controls on the page
dynamically because I'm building a SharePoint Web Part. For testing
purposes, I'm designing the page using a Web Form to see if the layout is
correct and to verify that I can access all of the controls.
What I want to do is create a table with X rows, each having an identical
RadioButtonList containing the same three values. My Web Form has only one
control on it in design time, a PlaceHolder control which must serve as the
container for my RadioButtonList controls (I don't need the PlaceHolder
control in the Web Part).
Questions:
1) Do I have to create multiple RadioButtonList controls, one for each row?
Or is there a better way? DataRepeater? Control arrays?
2) The important one - how the heck do I grab the selected value for my
RadioButtonList in the Button1_Click event? The FindControl method for the
Page and PlaceHolder controls (which I assume "owns" it) returns nothing.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm3.
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
ListItemCollection listBoxData = new ListItemCollection();
// Add items to the collection.
listBoxData.Add(new ListItem("apples"));
listBoxData.Add(new ListItem("bananas"));
listBoxData.Add(new ListItem("cherries"));
RadioButtonList RadioButtonList1 = new RadioButtonList();
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
RadioButtonList1.DataSource = listBoxData;
RadioButtonList1.DataBind();
Table Table1 = new Table();
Table1.ID = "Table1";
Table1.BorderWidth = 5;
TableRow TableRow1 = new TableRow();
TableCell TableCell1 = new TableCell();
TableCell1.Controls.Add(RadioButtonList1);
TableRow1.Cells.Add(TableCell1);
Table1.Rows.Add(TableRow1);
HtmlButton Button1 = new HtmlButton();
Button1.InnerText = "Submit";
Button1.ServerClick +=new EventHandler(Button1_Click);
PlaceHolder1.Controls.Add(Table1);
PlaceHolder1.Controls.Add(Button1);
}
private void Button1_Click(object sender, System.EventArgs e)
{
RadioButtonList R1;
//Find the RadioButtonList
R1 = (RadioButtonList) PlaceHolder1.FindControl("RadioButtonList1");
//R1 = (RadioButtonList) Page.FindControl("RadioButtonList1");
//Get the RadioButtonList value
if(!(R1 == null) ) //this is always null!!!
{
if (R1.SelectedIndex > -1)
{
string X = R1.SelectedItem.Text;
this.Context.Response.Write(X);
}
}
}
}
}