How to find checked RadioButton inside Repeater Item? - asp.net

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();

Related

RadDatePicker in ASP.NET Gridview TemplateField

I've got a date picker field declared within a template field in a GridView as follows:
<asp:TemplateField HeaderText="Shipping Date">
<ItemTemplate>
<asp:Label ID="lblShippingDate" runat="server" CssClass="dnnFormLabel"
AssociatedControlID="ShippingDatePicker" />
<dnn:DnnDatePicker runat="server" ID="ShippingDatePicker" />
</ItemTemplate>
</asp:TemplateField>
I can select the date just fine and it appears in my label control - I then click on a standard asp:Button which finds the selected row and despite it finding all the other controls on the row including the label control displaying the selected date, the label's Text attribute is blank:
var txtShippingDate = row.Cells[7].FindControl("lblShippingDate") as Label;
Please note that I'm using the DotNetNuke dnn:DnnDatePicker control but this is actually a RadDatePicker under the covers.
Can anyone suggest how I can successfully read the value?
Thanks for looking :)
Try following after you find label:
if(txtShippingDate != null) {
string date = Request.Form[txtShippingDate.UniqueID].ToString();
}

getting the RadGrid checkbox column value in server

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")))

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.

Changing WebControl ID Inside of a Repeater

<ItemTemplate>
<asp:Label runat="server"><%#DataBinder.Eval(Container.DataItem, "Question")%></asp:Label>
<asp:DropDownList runat="server" id="<%#DataBinder.Eval(Container.DataItem, "QuestionID")%>">>
<asp:ListItem value="1" text="Yes" />
<asp:ListItem value="0" text="No" />
</asp:DropDownList>
<ItemTemplate>
This is roughly what I'm trying to do. Obviously, the implementation is faulty, but I can't find any information on how I'd go about this in practice. Any help is appreciated.
Edit: What I'm trying to do exactly is add a DropDownList for each item in this Repeater, and upon submission of the form, use the ID's of each Yes/No answer to input into a database. The SqlDataReader that I'm using has two fields: The question content and the questionID.
I think you'd be better off using the built in support for IDs inside a Repeater. If the goal is to assign it an ID to make it easy to find the proper control after the data has been bound you might try something like this:
<asp:Repeater ID="Repeater1" runat="server>
<ItemTemplate>
<asp:Label ID="QuestionID" Visible="False" Runat="server"><%#DataBinder.Eval(Container.DataItem, "FieldContent")%></asp:Label>
<asp:DropDownList ID="MyDropDownList" Runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
Then, in your code you can loop through the items in the Repeater until you find the label you're looking for:
foreach (RepeaterItem curItem in Repeater1.Items)
{
// Due to the way a Repeater works, these two controls are linked together. The questionID
// label that is found is in the same RepeaterItem as the DropDownList (and any other controls
// you might find using curRow.FindControl)
var questionID = curRow.FindControl("QuestionID") as Label;
var myDropDownList = curRow.FindControl("MyDropDownList") as DropDownList;
}
A Repeater basically consists of a collection of RepeaterItems. The RepeaterItems are specified using the ItemTemplate tag. Each RepeaterItem has its own set of controls that are, by the very nature of a Repeater, associated with each other.
Say you're pulling the Repeater data from a database. Each Repeater item represents data from an individual row in the query results. So if you assign the QuestionID to a label and the QuestionName to a DropDownList, the ID in the label would match up with the name in drop down.
Could you remove the control from the markup file, and hook the repeater's onItemDataBound event. In that event, you should be able to create the dropdown control "manually", assigning whatever ID you want.

Resources