How to select just a radiobutton in a repeater with postback - c#

As I found on Internet that there is a bug from MS when we add a RadioButton inside repeater for selecting just one.
But I found this code here that solved my problem. (only one radiobutton selection in repeater)
$(function () {
$('[name$="$YourGroupName"]').attr("name", $('[name$="$YourGroupName"]').attr("name"));
$('[name$="$YourGroupName"]').click(function () {
//set name for all to name of clicked
$('[name$="$YourGroupName"]').attr("name", $(this).attr("name"));
});
});
But it only works when the radiobutton is not set to autopostback. But I need to do a postback when I select a radiobutton and a response to the database. But.... anytime that I do a click on a radiobutton with postback, always the first in the list is selected and the ItemDataBound it's not working because of the jquery function which it's renaming all the radiobuttons with the same name.
Suggestions?

Try to add this script instead of the one you're using at the end of the page after the repeater :
<script>
var radios = $("input:radio");
radios.click(function () {
radios.removeAttr('checked');
$(this).prop("checked", true);
//$(this).attr('checked', 'checked'); // jQuery < 1.6
return true;
});
</script>
This will allow the AutoPostBack of your radiobutton to work again.

Related

sample asp program to select all the checkbox while selecting one checkbox from the check box list

Im new to ASP. can any body give the sample code for select all the checkbox while selecting one checkbox from the check box list.
Select All
A
B
C
once checking the select all check from the checkbox list all other items also should get selected.
First off, Please try to search internet for answers, since you are new to ASP, its better to try examples from the internet.--> Link to W3
what you can try is on click of "select all" check box, iterate through the checkbox collection list, and set the selected property to true.
Something like this
foreach (ListItem item in this.CheckBoxes.Items)
{
item.Selected = true;
}
use jquery for this purpose , use this id input#chkView for the main checkbox that will select/deselect all checkboxes and assign this class .viewPerm to all the checkboxes that you want to to check/uncheck on click of input#chkView .
<script type="text/javascript">
$(document).ready(function () {
$('input#chkView').change(function () {
if ($(this).attr('checked')) {
$('.viewPerm > input:checkbox').each(function () {
$(this).attr('checked', true);
});
}
else {
$('.viewPerm > input:checkbox').each(function () {
$(this).attr('checked', false);
});
}
});
});
take a one checkboxlist add item, add first item text as Select All ,after creating checkbox, set autopostback property to true, and handle selectedindexchanged event of checkboxlist
use following code in in the selectedindexchaged event:
if (CheckBoxList1.Items[0].Selected == true)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
CheckBoxList1.Items[i].Selected = true;
}
}

Set a ViewState using JQuery

I have a CustomWebUserControl of a GridView and to move around the items I use a ViewState, now I need to move the SelectedIndex when I click on the row, the process gonna be:
Click in the expected row;
Set the value of rowIndex in a currentItemTextBox.Text;
This value gonna be input on the ViewState using the TextChanged event;
I'll Call a DataBind for my GridView to my internal methods select the currentRow using this ViewState.
<script type="text/javascript">
$(document).ready(function () {
$("#<%=mainGridView.ClientID%> tr").click(function () {
var ind = $(this).index();
$("#<%=currentIndexTexBox.ClientID%>").val(ind);
});
});
</script>
*When the script set the value the TextChanged event isn't call.
I hope that you can understand my scenario.enter code here
Thanks!
Try to add AutoPostBack="True" in you TextBox. This will call the OnTextChanged event.
<asp:TextBox ID="currentIndexTexBox" runat="server" OnTextChanged="CurrentIndexTextBoxOnTextChanged()" AutoPostBack="True"></asp:TextBox>

problem with check box in asp .net c#?

The controls on aspx page are like this
Submitted
Submission Date
I want, if the check box is checked, then the textbox will be enabled I wrote
if(chkSubmitted.Checked)
{
txtSubmissionDate.Enabled = true;
}
in the page load event. But when the page is loaded this checkbox having no effect on. Whats going wrong?
If you want the action of clicking the checkbox to enable the textbox, you'll need to do a postback when the box is checked by setting AutoPostBack="True":
<asp:CheckBox runat="server" ID="chkSubmitted" AutoPostBack="True" />
Or, you could use JavaScript:
<asp:CheckBox runat="server" ID="chkSubmitted" onclick="setSubmissionDateEnabled()" />
function setSubmissionDateEnabled()
{
var chkSubmitted = document.getElementById("<%= chkSubmitted.ClientID %>");
var txtSubmissionDate = document.getElementById("<%= txtSubmissionDate.ClientID %>");
txtSubmissionDate.disabled = !chkSubmitted.checked;
}
First set autopostback property to true of checkbox
write following code in checkbox_Selectedindexchanged event
if(chkSubmitted.Checked)
{
txtSubmissionDate.Enabled = true;
}
else
{
txtSubmissionDate.Enabled = false;
}
Thats most likely because the default state of txtSubmissionDate is enabled already.
Try this in your page_load:
txtSubmissionDate.enabled = !chkSubmitted.Checked
To clarify, the textbox should not (!) be enabled when the Checkbox is.
Put this in Page_PreRender event. At this stage it will capture the user affected state of chkSubmitted.

How can i get ASP.Net Grid Element using Javascript

I am using a Grid View in asp.net, i want to get Element on clicking a grid, how can i do so?
A grid has a column id, name, warp, weft, etc, i want to pick the selected cell data using Javascript, let me know.
Please help...
Regards
Atif
To track which row button is clicked, you have to set the row Index as a parameter to a JS function like...
protected void grdForecast_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType ==DataControlRowType.DataRow )
{
((Button)e.Row.FindControl("buttonId")).Attributes.Add("onclick", "javascript:update(" + (e.Row.RowIndex ) + ");");
}
}
And then in JavaScript:
<script language="javascript" type="text/javascript">
function update(ri) {
var grd = document.getElementById('<%= GridView1.ClientID %>');
SecondCellValue = grd.rows[ri].cells[1].childNodes[0].value
ThirdCellValue = grd.rows[ri].cells[2].childNodes[0].value
}
</script>
Do you have a control inside the cell that you can reference? If not, then you can create a hidden control. Then you can write out the control's client id to the client side in the PreRender event handler via ScriptManager. And you can then get a hold of that element by id and find other content inside that parent cell.
Alternatively, you can use jquery to handle cell click events...
$('#myTable td').click(function () {
alert($(this).html());
});

How to retrieve radiobutton selected value on asp: button click in javascript

I have an update page with radiobutton "Alternate Addresses".
If the radiobutton has "Yes" value, There would be altenate addresses for client.
If radiobutton has "No" value, there won't be any alternate addresses.
In update page, if supplier changes radiobutton value from "Yes" to "No" and clicks asp:Button "Update", all alternate addresses will be deleted.
I want to show a confirm messagebox on Update button click. But i am not being able to retrieve radiobutton selected value in javascript.
var list = document.getElementById("radios"); //Client ID of the radiolist
var inputs = list.getElementsByTagName("input");
var selected;
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].checked)
{
selected = inputs[i];
break;
}
This code works fine with html buttons. But I want code for asp:Button onClientClick. Please Help. Thanks in advance
You can attach javascript from code behind as well like.
ASPNET_Server_Control.Attributes.Add("onclick", "return yourJSFunction();")
You should make sure you return false in asp button, if you dont want a post back to happen.

Categories