Hi George,
Thanks for posting in the community! My name is Steven, and I'll be
assisting you on this issue.
From your description, you used a RadioButtonList control in a DataGrid's
template column. The RadioButtonList contains two items. "On" and "Off" and
was set as AutoPostBack=True. And you want to do some database
manipulations in the RadioButtonList's SelectIndexChanged postback event,
yes?
If there is anything I misunderstood, please feel free to let me know.
As for this problem, I think you may try the following steps to accomplish
it:
1. Write a event handler function for the RadioButtonList like below:
protected void rblState_SelectedIndexChanged(object sender,
System.EventArgs e)
{
// retrieve the RadionButton's value and the DataGridItem(which contains
the certain RadioButtonList) 's index
// and do some database operations
}
2. Rigister the handler for the RadioButtonList control in the aspx page,
such as:
<asp:RadioButtonList id=rblState runat="server"
OnSelectedIndexChanged="rblState_SelectedIndexChanged" AutoPostBack="True"
SelectedIndex='<%# SetState(DataBinder.Eval(Container.DataItem,"state"))
%>'>
To make the above description clearly, I've made a sample page, you may
refer to it if you feel anything unclear:
-------------------------------------aspx
page------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>RBL</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 align="center" width="500">
<tr>
<td>
<asp:Label id="lblMessage" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<asp
ataGrid id="dgRBL" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<FONT face="ËÎÌå">
<asp:RadioButtonList id=rblState runat="server"
OnSelectedIndexChanged="rblState_SelectedIndexChanged" AutoPostBack="True"
SelectedIndex='<%# SetState(DataBinder.Eval(Container.DataItem,"state"))
%>'>
<asp:ListItem Value="On">On</asp:ListItem>
<asp:ListItem Value="Off">Off</asp:ListItem>
</asp:RadioButtonList></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp
ataGrid></td>
</tr>
</table>
</form>
</body>
</HTML>
--------------------------------code behind page
class------------------------------
public class RBL : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.DataGrid dgRBL;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("state");
for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();
newrow["index"] = index.ToString();
newrow["name"] = "Name" + index.ToString();
if(i%2 == 0)
{
newrow["state"] = true;
}
else
{
newrow["state"] = false;
}
tb.Rows.Add(newrow);
}
dgRBL.DataSource = tb;
dgRBL.DataBind();
}
protected void rblState_SelectedIndexChanged(object sender,
System.EventArgs e)
{
RadioButtonList rbl = (RadioButtonList)sender;
Control parent = rbl.Parent;
while(!(parent is System.Web.UI.WebControls.DataGridItem))
{
parent = parent.Parent;
}
int index = ((DataGridItem)parent).ItemIndex;
if(rbl.SelectedIndex == 0)
{
lblMessage.Text = "Row[" + index + "]'s state is changed to On!";
}
else
{
lblMessage.Text = "Row[" + index + "]'s state is changed to Off!";
}
}
protected int SetState(object st)
{
string state = st.ToString();
if(state.Equals("True"))
{
return 0;
}
else
{
return 1;
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
-----------------------------------------------
Please check out the preceding suggestions. If you need any further
assistance, please feel free to post here.
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.)