getting the RadGrid checkbox column value in server - asp.net

I have a RadGrid having a checkbox column, i have added the column as a ItemTemplate to make it editable in the regular mode.
<telerik:GridTemplateColumn UniqueName="IsSelected" DataField="IsSelected">
<ItemTemplate>
<asp:CheckBox ID="chkBoolean" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsSelected")) %>'Enabled='<%# Convert.ToBoolean(Eval("IsSelectionDisable")) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
Using this I am able to display a checkbox as editable. Now my problem is how do i get the checked value of the checkox for saving it when the user has changed the checkbox. On click of a button i need to get all the rows that are still checked and save them. The below code does not work as it does not get the checkbox. Is there any way possible to get the value.
foreach (GridDataItem item in rgUnavailResult.MasterTableView.Items)
{
(CheckBox)item["IsSelected"].Controls[0]
}
Thanks

Found the checkbox
((System.Web.UI.WebControls.CheckBox)(item["IsSelected"].FindControl("chkBoolean")))

Related

How can I disable/hide edit button in gridview control of ASP.net

I have a gridview control in which there are two command buttons (linkButton), one is "select" and the other is "edit", now I have a session variable named as department and it contains values like "WRITING", "EDITING", "ADMIN" etc... Now the thing is I want the edit button to disappear when the department is "WRITING" but when it is "EDITING" or "ADMIN" I want edit button appear.
I was searching some forums for this problem and found this
row.Cells[0].Controls.Clear();
The problem with this code is that it hides the entire command column including "select" and "edit" buttons while I only want to control "edit" button, select should remain visible for all departments.
if I do it like (on row data bound event)
e.row.Cells[0].Controls[1].visible = false OR
e.row.Cells[0].Controls[0].visible = false
Specified argument was out of the range of valid values.
How can I do this properly ?
Thanks.
You can use the Visible property in the following way.
if (Session(mode) == "WRITING")
{
(e.Row.FindControl("btnEdit")).Visible = false;
}
First of all - your code does not work because your gridViewCell at index 0 contains only one control. Every CommandButton gets a single cell! Furthermore I am not quite sure why you make use of the RowDataBound event. As you are using a Session-Var this one may not change while binding your rows.
I tried the following piece of code in my pageLoad eventHandler - and it worked out for me:
/// just replace CAPITAL letters with your names and
/// (Session["department"] == "WRITING") with your boolean expression
gvYOURGV.Columns[INDEXOFSELECTCOLUMN].Visible = (Session["department"] == "WRITING");
In case you do want to have (need to have?) more flexibilty on modifying the appearance of your command columns - you may even consider, changing your command columns to templated columns. Doing so you may have the possibility for this code:
<asp:GridView ID="gvYOURGV" runat="server">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
visible='<%# Session["department"] != "WRITING" %>'
runat="server" CausesValidation="False"
CommandName="Select" Text="Auswählen"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
hth

CheckBox in ASP.NET GridView template field does not retain it's value on submit

I'm trying to use CheckBoxes within a GridView's TemplateField to select multiple entries from that GridView. The GridView's data source is a list of items that is generated at page load.
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False"
AllowPaging="True" onpageindexchanging="TANsGridView_PageIndexChanging"
DataKeyNames="GUID">
<Columns>
<asp:TemplateField ShowHeader="False" HeaderText="Checker">
<ItemTemplate>
<asp:CheckBox ID="SelectCheckbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
The problem is that when I press a submit button, all the CheckBoxes are returned with the Checked property as 'false'.
For cycling through the rows, I use:
foreach (GridViewRow row in TANsGridView.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("SelectCheckbox");
}
What should I use in order to have access to the correct value?
Thanks, Catalin
Are you mistakingly rebinding the gridview on page load every time? The gridview binding code should be wrapped in a if statement ensuring it's only done on not postback.
Am I supposed to put this here for an accept check now? :)
if your are binding the grid on page load.load the grid like this.
if(!ispostback)
{
..........loading data to databind.
}

How to change values of Fields in GridView/RadGrid EditTemplate when a chkBox is checked?

When a 'Update' is clicked the row shows the Edititem mode.
I have a check box that when it is 'clicked' I want the other fields to dissapear/become read-only.
How can this be done client or server side?
My best guess is for server side I have something like this below.. but then in the event how do I get access to those items in edit mode and change them ?
<EditItemTemplate>
<asp:CheckBox ID="cbNR" runat="server" AutoPostBack="True"
OnCheckedChanged="cbNR_Clicked"
Checked='<%# Boolean.Parse(Eval("NR").ToString()) %>' />
</EditItemTemplate>
This would be pretty simple using jQuery. Code to hide the other cells when checkbox is checked:
jQuery(function() {
$("input[id*='cbNR']").click(function() {
$(this).parents("td").siblings().toggle();
});
});

FindControl("someTextBox") in GridView not sending the updated value

Im populating a GridView from List so am forced to use TemplateField controls to allow editing. This requires displaying a TextBox populated with the original value when in edit mode and using FindControl to get the new value out on update submit.
Problem is foundTextBox.Text == "OriginalTextBoxValue"
<asp:TemplateField HeaderText="A Field">
<ItemTemplate>
<asp:Label ID="_theLabel" runat="server" Text='<%# Eval("AField") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_theTextBox" runat="server" Text='<%# Eval("AField") %>' />
</EditItemTemplate>
</asp:TemplateField>
And the code in my update event handler
TextBox newText = (TextBox)_myGridView.Rows[e.RowIndex].FindControl("_thTextBox");
//newText.Text == the old value of the text box
Is your gridview binded at every postback? This could explain why you never get the updated value, because the gridview is rebinded before reading the textbox.
Could you paste your complete update method?
You've got the code behind in the wrong event handler. Move it to the Editing event handler, so it will populate the textbox whenever the user clicks on the Edit command for a row.

How to find checked RadioButton inside Repeater Item?

I have a Repeater control on ASPX-page defined like this:
<asp:Repeater ID="answerVariantRepeater" runat="server"
onitemdatabound="answerVariantRepeater_ItemDataBound">
<ItemTemplate>
<asp:RadioButton ID="answerVariantRadioButton" runat="server"
GroupName="answerVariants"
Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"/>
</ItemTemplate>
</asp:Repeater>
To allow select only one radio button in time I have used a trick form this article.
But now when form is submitted I want to determine which radio button is checked.
I could do this:
RadioButton checkedButton = null;
foreach (RepeaterItem item in answerVariantRepeater.Items)
{
RadioButton control=(RadioButton)item.FindControl("answerVariantRadioButton");
if (control.Checked)
{
checkedButton = control;
break;
}
}
but hope it could be done somehow simplier (maybe via LINQ to objects).
You could always use Request.Form to get the submitted radio button:
var value = Request.Form["answerVariants"];
I think the submitted value defaults to the id of the <asp:RadioButton /> that was selected, but you can always add a value attribute - even though it's not officially an <asp:RadioButton /> property - and this will then be the submitted value:
<asp:RadioButton ID="answerVariantRadioButton" runat="server"
GroupName="answerVariants"
Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"
value='<%# DataBinder.Eval(Container.DataItem, "SomethingToUseAsTheValue")%>' />
Since You are using javascript already to handle the radio button click event on the client, you could update a hidden field with the selected value at the same time.
Your server code would then just access the selected value from the hidden field.
I'm pretty sure that the only thing you could use LINQ to Objects for here would be to take the conditions from within the foreach loop and move them to a where clause.
RadioButton checked =
(from item in answerVariantRepeater.Items
let radioButton = (RadioButton)item.FindControl("answerVariantRadioButton")
where radioButton.Checked
select radioButton).FirstOrDefault();

Resources