Checkbox inside a gridview change after page load in asp.net C# - c#

I have a radiobutton list named rdbModules and a GridView named dgvMenu.dgvMenu contains five CheckBoxes name chkSelect,chkAdd,chkUpdate,chkDelete,chkReport.
I have populated dgvMenu from the database depends on the selection of rdbModule.
The problem in when I am selecting checkbox inside the gridview it is working fine.
Suppose I have selected first item from radiobuttonlist and then selected some checkboxes from gridview. After that if I select the second or any other other options from the radio button list i am not able to get the selected checkbox details inside the gridview for the selection of first item of the radio button list.

rdbModules_SelectedIndexChanged(object sender, EventArgs e)
{
//Loop through your gridview here
foreach (GridViewRow row in dgvMenu.Rows)
{
if (((CheckBox)row.FindControl("chkboxid")).Checked)
{
//do what you want
}
}
if (rdbModules .Items.Cast<ListItem>().Any(item => item.Selected))
{
foreach (ListItem item in rdbModules.Items)
{
if (item.Selected)
{
string selectedValue = item.Value;
dt = objSec.ShowSubMenuModuleWise(selectedValue);
dgvMenu.DataSource = dt; dgvMenu.DataBind();
}
}
}
}

Related

how to get the rows property in gridview of devxpress with a button to edit?

i have a gridview with devexpress my gridview contain a id ID="GriviewLV1" and i have a button in the row of the gridview to edit the record , in the event click of the button im trying give click in the button to edit but cannot get the property rows or something like that. im trying something like this but cannot do rows property because dont exist the rows property in grid view devexpress
button_Edit_click(object sender, EventArgs e)
{
foreach (GridViewRow row in GriviewLV1.Rows)
{
}
}
And for what exactly do you need row collection?
As far as I know, you can only manipulate with records (DataSource property object). Also it is possible to get selected row. Some my code examples with explanation:
private List<ImageSetMember> imageSets;
...
//assign collection as grid's DataSource.
//From now on any actions on imageSets object will be automatically reproduced
//as grid's row changes.
imageSetGridControl.DataSource = imageSets;
...
private void replaceButtonEdit_Click(object sender, System.EventArgs e)
{
//get focused row record's index at DataSource collection
int index = imageSetGridView.GetDataSourceRowIndex(imageSetGridView.FocusedRowHandle);
var selectedImage = imageSet[index].Image; //accessing to row's record
}

to populate one dropdown on selectedindexchanged of another inside gridview

I have gridview with two dropdowns in a row say country and state,on change of country dropdown i want to populate the state dropdown when the grid is in edit state.
I got both the dropdowns in RowEditing event of grid,also selectedindexchanged event is attached to first dropdown of grid.The problem is how to get second dropdown i.e. state dropdown in selectedindexchange event of country dropdown.
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl1.NamingContainer;
if (row != null)
{
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList3");
{
//call the method for binding the second DDL based on the selected item on the first DDL
DataTable dt = BindDropDownList(ddl1.SelectedItem.Text);
ddl2.DataTextField = "Field1";
ddl2.DataValueField = "Field2";
ddl2.DataBind();
}
}
}
If RowEditing event occurs before dropdown selection changed event, you can store reference to current Row in you application. Then get this row in dropdown changed event.

how to get dropdown value inside gridview on button click in c#?

I have tried this but its not working,giving null value:please tell me what have to change in this code.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
use
DropDownList ddl= (DropDownList)(row.FindControl("ddlCol1"));
and try and let me know
try this code on click event
foreach (GridViewRow row in GridView1.Rows)
{
//Finding Dropdown control
DropDownList ddl1 = row.FindControl("ddlTest") as DropDownList;
if (ddl1 != null)
{
// your code here
}
}
#creby i saw your code.
you are adding your dropdown in grid view during row data bound event ok.. but now when you click on the button, all the drop down will be vanished so that's why you will not able to get drop down.
use item template of grid view and then bind dropdown in row data bound event.

How to obtain the value of HiddenField in a GridView when a CheckBox is checked?

I have a GridView which has multiple rows, on each row I have a CheckBox and a HiddenField. On button click I want to check if the CheckBox is checked and if it is I want to take the value of the HiddenField for that row. Each HiddenField on each row has a different value. User could check multiple CheckBoxes so I need to be able to pull the value of each HiddenField.
Any help will be really appreciate it.
Thank you
Loop through each row in the grid, check if the checkbox is checked and if it is, grab the value of the hidden field.
foreach (GridViewRow row in grdView.Rows)
{
if((row.FindControl("chkBoxId") as CheckBox).Checked)
{
string hiddenFieldValue = (row.FindControl("hiddenFieldId") as HiddenField).Value;
}
}
Where chkBoxId is the ID property of your checkbox on the page and hiddenFieldId is the ID of the hiddenfield control on your page.
Possible duplicates.
How to get values of CheckBoxes inside a gridview that are checked using asp .net
Get the id of selected checkboxes in gridview (Asp.net) c#
How to get the value in the gridview which the checkbox is checked?
One of the answer in above links :
foreach(Gridviewrow gvr in Gridview1.Rows)
{
if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
{
//Get hidden field value here.
}
}
You can use a code like this:
protected void BtnMybutton_click( Object sender, EventArgs e)
{
Button Mybutton = (Button) sender;
GridViewRow row = (GridViewRow) MyButton.NamingContainer;
CheckBox ChkTest = (CheckBox) row.FindControl("ChkTest");
HidenFiekd HdfValue = (HidenField) row.FindControl("HdfValue");
if(ChkTest.Checked)
{
Console.WriteLine(HdfValues.Value);
}
}

Get the selected value of a dropdownlist located in a listview

how can i get the selected value from a dropdownlist which is in a listview , from the DropDownList_SelectedIndexChanged event?
i have always had problem with finding controls in the page :-)
foreach (ListViewItem item in CouncilListView.Items)
{
CouncilIdLabel = (Label)item.FindControl("CouncilIdLabel");
}
it just pass all the items and i don't know how to get out of the foreach when reach the wanted control.
If you are registering the event from within the template markup of your listview like so:
<asp:DropDownList runat='server' id='ddl1' OnSelectedIndexChange='dropdownlist_selectedindexchange' />
then all you have to do is this:
protected void dropdownlist_selectedindexchange(Object sender, EventArgs e){
DropDownList ddl1 = (sender as DropDownList);
String value = ddl1.SelectedValue;
}
You can Exit the foreach loop:
string look_for = "bbb";
ArrayList names = new ArrayList();
names.Add("aaa");
names.Add("bbb");
names.Add("ccc");
foreach (string name in names)
{
if (look_for == name)
{
break;
}
}

Categories