Newbie question - User Controls and Events

  • Thread starter Mr Not So Know It All
  • Start date
M

Mr Not So Know It All

thanks for taking the time to review my request.

i have a user control with a radiobuttonlist. i would like to click on
one of the buttons and raise the event from the button and catch it
with page (eventually pass the event results to another user control).
i found an article on the web that demonstrated this, but i could get
it to work.

link to article -
http://www.openmymind.net/communication/index.html#3.3

here is my code. again, thanks for the help

page _________________________________________________

public class myRadio:page
{
protected PlaceHolder plh_radioOne;
protected myRadioOne mROne = new myRadioOne();

void Page_Load(object s, EventArgs e)
{
mROne.myRadioChg += new myRadioOne.chgRadio(mROne_myRadioChg);

Control ctl = new Control();
ctl = LoadControl("myRadioOne.ascx");
plh_radioOne.Controls.Add(ctl);
}

void mROne_myRadioChg(object s, EventArgs e)
{
Response.Write("caught radio event");
//throw new Exception("The method or operation is not
implemented.");
}
}

usercontrol ___________________________________________
public class myRadioOne:UserControl
{
public delegate void chgRadio(object s, EventArgs e);
public event chgRadio myRadioChg;

public void chgValue(object s, EventArgs e)
{
RadioButtonList r = (RadioButtonList)s;
Response.Write("Selected Value : " + r.SelectedValue);

if(myRadioChg!=null)
{
myRadioChg(s, e);
}
}
}

again thanks for assistance. any help will be greatly appreciated.
 
M

Mr Not So Know It All

with the help of an o'reilly book, i figure out what i was doing wrong.
here's my attempt to explain the solution

<b>Main ASPX file - </b>
<%@ Page Language="C#" AutoEventWireup="true" Inherits="myRadio" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Radio Button List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeHolder ID="plh_radioOne" runat="server" />
</div>
<br />
<asp:Literal ID="lit_MyChange" runat="server" />
<br />
<asp:placeHolder ID="plh_Results" runat="server" />
</form>
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<b>Main ASPX file code-behind file myRadio.cs</b>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class myRadio:page
{
protected PlaceHolder plh_radioOne;
protected PlaceHolder plh_Results;
protected myRadioOne mROne = new myRadioOne();
protected Literal lit_MyChange;

void Page_Load(object s, EventArgs e)
{
myRadioOne r = (myRadioOne)Page.LoadControl("myRadioOne.ascx");
plh_radioOne.Controls.Add(r);

r.myRadioChg += new myRadioOne.chgRadio(r_myRadioChg);

myResults myR = (myResults)Page.LoadControl("myResults.ascx");
plh_Results.Controls.Add(myR);

r.myRadioChg += new myRadioOne.chgRadio(myR.chg_label);

}

public void r_myRadioChg(object s, EventArgs e)
{
RadioButtonList rbl = (RadioButtonList)s;
lit_MyChange.Text = "Button Clicked from User Control : value =
" + rbl.SelectedValue;
//throw new Exception("The method or operation is not
implemented.");
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<b>First User Control - myRadioOne.ascx</b>
<%@ Control Language="C#" AutoEventWireup="true" Inherits="myRadioOne"
%>

<asp:RadioButtonList ID="rbl_radioOne"
OnSelectedIndexChanged="chgValue"
AutoPostBack="true"
runat="server">
<asp:ListItem Text="Option 1" Value="1" />
<asp:ListItem Text="Option 2" Value="2" />
<asp:ListItem Text="Option 3" Value="3" />
</asp:RadioButtonList>
<br />
<asp:Label ID="lab_radioValue" runat="server" />
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<b>First User Control's code-behind myRadioOne.cs</b>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class myRadioOne:UserControl
{
protected Label lab_radioValue;
public delegate void chgRadio(object s, EventArgs e);
public event chgRadio myRadioChg;

public void chgValue(object s, EventArgs e)
{
RadioButtonList r = (RadioButtonList)s;
lab_radioValue.Text = "Current Option : " + r.SelectedValue;

if(myRadioChg!=null)
{
myRadioChg(object s, EventArgs e);
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<b>Second User Control File - myResults.ascx</b>
<%@ Control Language="C#" AutoEventWireup="true" Inherits="myResults"
%>
<asp:Label ID="lab_Results" runat="server" />
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<b>Second User Control code-behind - myResults.cs</b>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class myResults : UserControl
{
protected Label lab_Results;

public void chg_label(object s, EventArgs e)
{
RadioButtonList r = (RadioButtonList)s;
lab_Results.Text = "Second User Control : Value from 1st User
Control " + r.SelectedValue;
}
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top