Triggering Events on <asp: RadioButton Help.

J

jason burkett

Please help…I saw your posts and even found one directly linked to
what I am having problems with…however, it seems as though I am doing
everything the same…and I cannot get my events to fire.

Thanks so much for any advice.

jason



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 ControlsTest_Schedule

{

public class ScheduleList : System.Web.UI.Page

{

protected System.Web.UI.WebControls.TextBox TEMPDate;
protected System.Web.UI.WebControls.DropDownList
cboDocList;
protected System.Web.UI.WebControls.Label lblChosenDoc;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label myLabel;
protected System.Web.UI.WebControls.RadioButton sStatus1;
protected System.Web.UI.WebControls.RadioButton
RadioButton3;
protected System.Web.UI.WebControls.RadioButton
RadioButton2;
protected System.Web.UI.WebControls.DataList
List_Schedule;


private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here
if (! IsPostBack)
{
string ChosenDate;
int DocID = 0;

ChosenDate = "6/29/04";
DocID=1;

List_Schedule.ItemCreated += new
DataListItemEventHandler(this.Item_Created);

List_Schedule.DataSource =
LIST.GetSchedule(ChosenDate, DocID);
List_Schedule.DataBind();

}

}


public bool IsChecked(int nButton, int sStatus)
{
if(nButton == sStatus)
{
return(true);
}
return(false);

}




void Item_Created(Object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
//This function will set the Radio Buttons CHANGE
events.
if(e.Item.ItemType==ListItemType.Item)
{
RadioButton rb=e.Item.FindControl("sStatus1")
as RadioButton;
rb.CheckedChanged+=new
EventHandler(this.sStatus1_CheckedChange);
}

}

protected virtual void sStatus1_CheckedChange(object
sender, EventArgs e)

{
int x = 0;
}




Problem is that I cannot get the event to fire… Any help would be
appreciated...
 
S

Steven Cheng[MSFT]

Hi Jason,

From the detailed code you provided, I think the cause of the problem you
met is that the DataGrid's ItemCreated event handler not be registered
correctly. In your code, you register the ItemCreated event handler in the
if(!IsPostBack)
{

}
block in the page's Page_Load event, this make the event handler be
registered only the first time the page is requested and loaded. Then, at
the sequential request(post back), the event handler is not registered so
the ItemCreated handler won't be called. You can add a break point in the
ItemCreated event 's handler and use F5 debug to have a check.

I suggest that you move the following code
================
List_Schedule.ItemCreated += new
DataListItemEventHandler(this.Item_Created);
=================

into the OnInit method(or in the InitializeComponent method) of the page
and don't put it into the
if(!IsPostBack)
{

}

block so that register it evetytime the page is request. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
M

mojoman896

Thanks so much for the help, but that didnt fire the event either...

could it be because the RadioControl is embedded in the Datalist? Do i need to get into the bubbling of the events?

This is the last hurdle i need to get really moving on a project, so i appreciate any advice...lete me know if i need to supply more code.
 
S

Steven Cheng[MSFT]

Hi Jason,

Thanks for your followup. One thing should be notice is that we should set
some certain webcontrol's AutoPostBack as true so as to make it
autopostback when its state changed. So make sure the radiobutton's
autopostback is set to true. In addition, I'm abit confused that you use a
single radio button in a grid cell? Generally wo use radio button at least
pair based. Anyway, I've made a simple demo page using a checkbox added in
a datagrid cell, you may have a test on your side to see whether it works.
Below is the page's source

===================aspx page====================
<HTML>
<HEAD>
<title>ListGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgList" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Selected">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" Runat="server" AutoPostBack="True"
OnCheckedChanged="chkSelected_CheckedChanged" Checked='<%#
((System.Data.DataRowView)Container.DataItem)["selected"]
%>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>

===========code behind class=====================
public class ListGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgList;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind_Data();
}
}


protected void Bind_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index",typeof(int));
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("selected",typeof(bool));

DataRow row = null;
for(int i=1;i<=15;i++)
{
row = tb.NewRow();
row["index"] = i;
row["name"] = "Name_" + i;

if(i%3==0)
{
row["selected"] = true;
}
else
{
row["selected"] = false;
}

tb.Rows.Add(row);

}


dgList.DataSource = tb;
dgList.DataBind();


}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void chkSelected_CheckedChanged(object sender, System.EventArgs
e)
{
CheckBox chk = (CheckBox)sender;
Response.Write("<br>" + chk.ClientID + chk.Checked );
}
}
======================================

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top