Newbie question - User Controls and Events

Discussion in 'ASP .Net' started by Mr Not So Know It All, Jan 12, 2007.

  1. 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.
    Mr Not So Know It All, Jan 12, 2007
    #1
    1. Advertising

  2. 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;
    }
    }
    Mr Not So Know It All, Jan 15, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. newbye
    Replies:
    0
    Views:
    567
    newbye
    Jul 6, 2006
  2. Grey Alien

    Newbie question (asp controls and events)

    Grey Alien, Jul 9, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    353
    Dariusz Tomon
    Jul 9, 2007
  3. SevDer
    Replies:
    2
    Views:
    1,276
    Walter Wang [MSFT]
    Nov 13, 2007
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    327
    Chris
    Aug 30, 2005
  5. Replies:
    1
    Views:
    143
    Peter Michaux
    Apr 27, 2007
Loading...

Share This Page