I have a grid and on each row of grid i generate a radiobuttonlist with 2 items dynamically in rowdatabound. On the save button event i want to access the data of the row and save the data in DB with which ever radio button is selected.
I tried different things but all in vain. I tried to use hidden field so that whenever any radio button is selected in a row then in javascript i will update the hidden field but user can select multiple rows so this is not a good solution.
Here is my code of dynamic radio button list generation in rowdatabound event.
what you can do is create the controls as it is said in the link i.e on Row Created event & then u can access those controls in the Row Updating event & get the selected value. Here u can have the selected value in a hidden field wich would be accessible
in the save button click event.
Well if you want to access Dynamic Control on post back then it's really important that you re-create them during postback i.e.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// Create Radio Button list here
//foreach(GridViewRow in GridView.rows)
//{
//}
}
}
This is necessary as the Dynamic Controls are not saved in Page's View State and thus they are lost on PostBack, so you need to Re-create them everytime the page posts back. You can do the same in PAGE_INIT event as well.
After that when your click event wont have any problem finding the RadioButtons and thus will be executed suxxessfully.
I have to recreate just the radiolist or do i have to create all the items as well ? I think yes, i will have to. But if i recreate the items as well and fill them with the database values.(The value of items comes from the database.). Here is what i have
implemented.
This function is called from PreLoad event of the page and PreRender event of the GRID. Now i can access the values but the comes that arises now is that if i change the selected item of the row then on the Save event i get the old values. I tried to drill down the problem and i think that my above function is not correct and i am doing something wrong there. Can you figure out any problem ??
The usual flow to do what you describe is like this:
Bind GridView in Page_Init event.
Create RadioButtonList in RowDataBound event of GridView.
Handle the button to loop the GridView to access the selected value from RadioButtonList to do saving or other process.
Finally i found the solution. I am posting the solution here in case anybody is searching for the solution.
On you PreLoad you will have to regenerate your radiobuttons so that you can access then. You should not use Pre_Init event because the grid data is not loaded at the time (I found this info on the internet). Here is my PreLoad event
Remember one thing. If you are using MasterPage then you will have to get the controls before regenerating them. See the above event which contains a function GetMasterPageControls() function.
Here is my AddRadioButton() function:
private void AddRadioButton()
{
foreach (GridViewRow row in gvMain.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (GetRadioButtonList(row) == null)
{
row.Cells[11].Controls.Add(CreateRadioList(row));
}
}
}
}
row.Cells[11] <-- 11 is the index of my grid cell which contains the radiobuttonlist.
Here is my CreateRadioList() function.This function includes my own logic:
rogerz123456...
Member
138 Points
122 Posts
Acessing dynamically generated radiobuttonlist of grid
Mar 22, 2011 07:10 AM|LINK
Hello All,
I have a grid and on each row of grid i generate a radiobuttonlist with 2 items dynamically in rowdatabound. On the save button event i want to access the data of the row and save the data in DB with which ever radio button is selected.
I tried different things but all in vain. I tried to use hidden field so that whenever any radio button is selected in a row then in javascript i will update the hidden field but user can select multiple rows so this is not a good solution.
Here is my code of dynamic radio button list generation in rowdatabound event.
RadioButtonList rb = new RadioButtonList(); rb.RepeatDirection = RepeatDirection.Horizontal; ListItem liGood = new ListItem(); //liGood.Attributes.Add("id", "good"); liGood.Text = "Good"; liGood.Attributes.Add("name", _activitySeqNo); liGood.Value = "G|" + _sourceSeqNo; //liGood.Attributes.Add("onclick","return SetHiddenField('" + hdField.ClientID + "," + gvMain.ClientID + "');"); ListItem liBad = new ListItem(); //liGood.Attributes.Add("id", "bad"); liBad.Text = "Bad"; liBad.Attributes.Add("name", _activitySeqNo); liBad.Value = "B|" + _sourceSeqNo; //liBad.Attributes.Add("onclick", "return SetHiddenField('" + hdField.ClientID + "," + gvMain.ClientID + "');");Any help will be greatly appreciated.
Regards,
Behroz Sikander
Behroz Sikander
DreamBig
Participant
1339 Points
622 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 22, 2011 07:47 AM|LINK
Solved here - http://forums.asp.net/p/1559513/3846467.aspx
My Blog
rogerz123456...
Member
138 Points
122 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 22, 2011 08:41 AM|LINK
thank you for your reply. but i want to access them in the Save Button click event.
Behroz Sikander
DreamBig
Participant
1339 Points
622 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 22, 2011 09:16 AM|LINK
what you can do is create the controls as it is said in the link i.e on Row Created event & then u can access those controls in the Row Updating event & get the selected value. Here u can have the selected value in a hidden field wich would be accessible in the save button click event.
Hope this helps..
My Blog
hiren.sharma
Participant
1460 Points
311 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 22, 2011 12:22 PM|LINK
Well if you want to access Dynamic Control on post back then it's really important that you re-create them during postback i.e.
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { // Create Radio Button list here //foreach(GridViewRow in GridView.rows) //{ //} } }This is necessary as the Dynamic Controls are not saved in Page's View State and thus they are lost on PostBack, so you need to Re-create them everytime the page posts back. You can do the same in PAGE_INIT event as well.
After that when your click event wont have any problem finding the RadioButtons and thus will be executed suxxessfully.
Hope this Helps !!!
Happy Coding
Try out My Blog | Visit My Website
rogerz123456...
Member
138 Points
122 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 22, 2011 01:00 PM|LINK
I have to recreate just the radiolist or do i have to create all the items as well ? I think yes, i will have to. But if i recreate the items as well and fill them with the database values.(The value of items comes from the database.). Here is what i have implemented.
private void AddRadioButton() { ListItem liGood = new ListItem(); liGood.Text = "Good"; liGood.Value = "G"; ListItem liBad = new ListItem(); liBad.Text = "Bad"; liBad.Value = "B"; foreach (GridViewRow row in gvMain.Rows) { RadioButtonList rb = new RadioButtonList(); rb.RepeatDirection = RepeatDirection.Horizontal; //rb.ID = "rb" + row.RowIndex; if (row.RowType == DataControlRowType.DataRow) { if (row.Cells[7].Text != "Y") { liGood.Selected = true; if (row.Cells[6].Text == "B") liBad.Selected = true; } else { liGood.Enabled = false; liGood.Selected = true; if (row.Cells[6].Text == "B") liBad.Selected = true; liBad.Enabled = false; } rb.Items.Add(liGood); rb.Items.Add(liBad); row.Cells[11].Controls.Add(rb); } } }This function is called from PreLoad event of the page and PreRender event of the GRID. Now i can access the values but the comes that arises now is that if i change the selected item of the row then on the Save event i get the old values. I tried to drill down the problem and i think that my above function is not correct and i am doing something wrong there. Can you figure out any problem ??
My PreLoad is:
protected override void OnPreLoad(EventArgs e) { GetMasterPageControls(); AddRadioButton(); base.OnPreLoad(e); }and my Grid PreRender event is :
protected void gvMain_PreRender(object sender, EventArgs e)
{
AddRadioButton();
}
Regards,
Behroz Sikander
Behroz Sikander
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 24, 2011 07:53 AM|LINK
Hi,
The usual flow to do what you describe is like this:
Bind GridView in Page_Init event.
Create RadioButtonList in RowDataBound event of GridView.
Handle the button to loop the GridView to access the selected value from RadioButtonList to do saving or other process.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
hiren.sharma
Participant
1460 Points
311 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 24, 2011 08:07 AM|LINK
Well in Pre_Init, you cannot access the view state or anything
If your radio button ius dependent on some value in Datatable which is kept in ViewState
den do it in protected override void OnPreLoad(EventArgs e)
dats all
Problem Solved
Try out My Blog | Visit My Website
rogerz123456...
Member
138 Points
122 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 28, 2011 09:10 AM|LINK
<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; width: 100%; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; margin: 8px;">
Hello All,
Finally i found the solution. I am posting the solution here in case anybody is searching for the solution.
On you PreLoad you will have to regenerate your radiobuttons so that you can access then. You should not use Pre_Init event because the grid data is not loaded at the time (I found this info on the internet). Here is my PreLoad event
protected override void OnPreLoad(EventArgs e) { GetMasterPageControls(); AddRadioButton(); base.OnPreLoad(e); }Remember one thing. If you are using MasterPage then you will have to get the controls before regenerating them. See the above event which contains a function GetMasterPageControls() function.
Here is my AddRadioButton() function:
private void AddRadioButton() { foreach (GridViewRow row in gvMain.Rows) { if (row.RowType == DataControlRowType.DataRow) { if (GetRadioButtonList(row) == null) { row.Cells[11].Controls.Add(CreateRadioList(row)); } } } }
</div>Behroz Sikander
hiren.sharma
Participant
1460 Points
311 Posts
Re: Acessing dynamically generated radiobuttonlist of grid
Mar 28, 2011 10:11 AM|LINK
This is what i explained to you
Try out My Blog | Visit My Website