I am using repeater control to display online question paper to user. I am showing 50 questions to user. And i am giving 4 check boxes for every question to select answer. Now my doubt is how to get all 50 options that checked by user, and to compare those answers with correct answer tag in my XML. I am using XML file, not database.
Can anyone please help me how to achieve this functionality?
You have to iterate Repeater control, like...
if (Repeater1.Items.Count > 0)
{
for (int count = 0; count < Repeater1.Items.Count; count++)
{
CheckBox chk = (CheckBox)Repeater1.Items[count].FindControl("CheckBox1");
if (chk.Checked)
{
}
}
}
To get access to Repeater items you should use:
repeaterId.Items
To get access to all checked controls of a repeater (which are definitely RadioButton controls, as you should have one option per question), you can use:
foreach (ListViewDataItem item in repeaterId.Items)
{
// Finding RadioButton controls by Id
RadioButton firstOption = ((RadioButton)item.FindControl("firstOption"));
RadioButton secondOption = ((RadioButton)item.FindControl("secondOption"));
RadioButton thirdOption = ((RadioButton)item.FindControl("thirdOption"));
RadioButton fourthOption = ((RadioButton)item.FindControl("fourthOption"));
// Here you have four RadioButtones and you should only see which one of them is clicked. Then compare its value to correct value in your XML file.
}
Related
I have a gridview (gvClient) containing checkboxes (chkSelect).
I have enabled paging for same grid and I have lots of data to show.
But I am facing the problem, that while retrieving all data from gridview, I am getting only current page gridview data.
My code:
foreach (GridViewRow gvrClient in gvClient.Rows) // gvClient.Rows not giving all gridview rows
{
cbSelect = (CheckBox)gvrClient.FindControl("chkSelect");
if (cbSelect.Checked == true)
{
//Operations
}
}
I got following solution on google to use, but it is not working.
gvClient.AllowPaging=false;
gvClient.DataBind();
foreach (GridViewRow gvrClient in gvClient.Rows) // gvClient.Rows not giving all gridview rows
{
cbSelect = (CheckBox)gvrClient.FindControl("chkSelect");
if (cbSelect.Checked == true)
{
//Operations
}
}
gvClient.AllowPaging=true;
gvClient.DataBind();
Any help on this?
you can not access all pages in c#.
and by the way, why you want to access complete data of grid? as when you change a page, grid gets reset again. grid does not maintain page state data.
you can access the same data from a datatable/dataset which you have set as source of data to that grid..
I want to use a gridview in asp.net and when I click the button all rows in gridview will save in the array and I can use them one by one.
Please tell me the solution or suggest me a book or link or ...
Thanks
Here I suppose your button is not in the grid. On click event of the button you can write following code
string[] str = new string[gv.Rows.Count];
for (int i = 0; i < gv.Rows.Count; i++)
{
str[i]= gv.Rows[i].Cells[0].Text.ToString();
}
And you can have all values of Gridview cell against each row.
Now it's up to you how you will use this array further.
Thanks
I have code like this with devexpress ASPxGridView. Everything goes well, but when I try to add new row, new row's textbox is disabled...
I have KeyFieldName set.
void BindGrid()
{
var AnnObj = SearchBarBanners.Select(i => new
{
Caption = i.Attribute("caption").Value,
ID = i.Attribute("id").Value, // this is generated by Guid
}).ToList();
ImagesGrid.DataSource = AnnObj;
ImagesGrid.DataBind();
}
I can suggest you two things without grid markup:
1. Call BindGrid method in Page_Init
2. If your datasource initially returns zero rows, grid won't be able to determine type of objects that will be rendered in grid. You need to use ASPxGridView.ForceDataRowType in order to solve this problem.
Use recommendations from the Q392961 DX Article to resolve this issue.
I have a GridView in ASP.net where I have a CheckBox column. The user can toggle the CheckBox. Now, what I want is that when the user clicks on a button, all the records from the GridView where the CheckBox is checked should be displayed. And on another button, the opposite state should be displayed...
I am not getting the logic for the same.
Can anyone please help me with the logic..
You could iterate through the GridViewRows and check if the CheckBox is checked using something like the following
Edit from comments, fixed small bugs. Thanks guys. (3/20/2013):
foreach (GridViewRow row in yourGridViewID.Rows)
{
CheckBox check = (CheckBox)row.FindControl("CheckBoxName");
if (check.Checked)
{
//Take Row information from each column (Cell) and display it
}
else
{
//Display in seperate area
}
}
The index is going to be the column number starting from 0, going left to right of which column holds the CheckBox. You need to make sure the CheckBox has an ID name which is used at CheckBoxName. If you don't have an ID for that, you can also use
CheckBox check = (CheckBox)row.Cells[index].Controls[0];
I am trying to add a checkbox in a listview with value as ids of the records from the database so I can allow the user to check the ones they want to delete and when they click the delete button I can get value collection of checkbox with request.form.
My problem is, because checkbox in a listview ASP.NET renders the listview name into the name property of the checkbox, it prevents me to do request.form["checkboxname"].
I don't want to use Listviews delete commands but simply use request.form to get the collection of checked values.
How can I set name of the htmlinput checkbox so .NET doesn't change it in render time?
I have tried:
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)e.Item.FindControl("CheckBoxDelete");
_CheckBoxDelete.Visible = true;
_CheckBoxDelete.Value = DataBinder.Eval(dataItem.DataItem, "id").ToString();
_CheckBoxDelete.Name = "deletechecked";
But still it renders like:
<input name="PmList$ctrl0$CheckBoxDelete" type="checkbox" id="PmList_ctrl0_CheckBoxDelete" value="3" />
This is happening because ListView is a Naming Container. You can get around this in a couple of ways, but they all boil down to the choice of:
Rendering the HTML you want.
Pulling out the checked items in a different way.
The former is doable, but you'll likely loose a lot of ASP.NET's built in functionality. I'd advise against it unless you're deeply familiar with the control life cycle.
You've got everything you need for the pulling the values out in a way ASP is expecting:
HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)item.FindControl("CheckBoxDelete");
You just need to wait for the control hierarchy to be populated, and then loop over the ListView.Items looking for the checkboxes. Your "Delete" button's event handler is probably a good place to call this from.
Incidentally, why are you using a HtmlInputCheckbox, rather than a CheckBox?
I have sorted it out with:
string idCollectionTodelete = string.Empty;
foreach (string x in Request.Form)
{
if (x.IndexOf("CheckBoxDelete") > -1)
{
idCollectionTodelete += Request.Form[x] + ",";
}
}
new DB().DeleteUserPm(
ActiveUsername(), subdomain, idCollectionTodelete.TrimEnd(','));
It is not an ideal solution but it does work for me.
I do this
List<HtmlInputCheckBox> chkDeleteContacts = new List<HtmlInputCheckBox>();
foreach (RepeaterItem item in rptrFamilyContacts.Items)
{
chkDeleteContacts.Add((HtmlInputCheckBox)item.FindControl("chkDeleteContact"));
}
foreach(HtmlInputCheckBox chkDeleteContact in chkDeleteContacts)
{
//Delete Contact
if(chkDeleteContact.Checked)
blnStatus = BusinessUtility.DeleteConsumerContact(LoginConsumerID, chkDeleteContact.Value);
}
Slightly easier in my opinion