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

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.

Related

How to select just a radiobutton in a repeater with postback

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.

Preserving (dynamically populated) dropdownlist items and selection inside of GView

I have a column in a GridView which has a Dropdownlist in the EditTemplate
The data for this dropdownlist is added dynamically (items.add()) after the user enters the edit mode for that row.
foreach (var bgElement in userCropList.FirstOrDefault(c => c.Crop == cropToBeUpdated).BGs)
{
ddl.Items.Add(bgElement.BG);
}
ddl.SelectedIndex = userCropList.FirstOrDefault(c => c.Crop == cropToBeUpdated).BGs.FindIndex(d => d.Default == true);
When the user clicks the update button to save his changes, the page does a postback and as such lose the items aswell as the selectedindex which i need for the following bit in the RowUpdating event.
DropDownList ddl = (DropDownList)GVCrops.Rows[e.RowIndex].FindControl("ddlAvailableBGs");
updatedCrop.BGs.FirstOrDefault(c => c.BG == ddl.SelectedValue).Default = true;
What came to my mind was to put the value the user selects in a viewstate variable and read from it in the RowUpdating event.
I am asking if there is an easier (not that it is hard) or already available thing that I am missing which could that for me?
Edit:
Ok, turns out my method ..
RowEditing():
ViewState.Add("SelectedBGIndex", ddl.SelectedValue);
RowUpdating():
if (ViewState["SelectedBGIndex"] != null)
{
updatedCrop.BGs.FirstOrDefault(c => c.BG == ViewState["SelectedBGIndex"].ToString()).Default = true;
}
..is not going to work for the same reason apparently.
When the user clicks the update button for the row it will post back and the drop down list is reset with no selection and no items inside.
Seems like the solution would be a JavaScript function.
Any thoughts how to approach it?

How to get values of textbox and dropdownlist controls in a gridview on a button click event?

On the page load there will be gridview with a default row....there is a button with a + sign at the bottom...on the button click i want to create a new rows in the grid..before that i need to make sure that all fields have values.But I am getting null values..
My code is:
TextBox name_value = ((TextBox)Gridview1.Rows[0].FindControl("TextBox1"));
TextBox age_value = ((TextBox)Gridview1.Rows[0].FindControl("TextBox2"));
DropDownList sex_value = ((DropDownList)Gridview1.Rows[0].FindControl("DropDownList1"));
DropDownList berth_value = ((DropDownList)Gridview1.Rows[0].FindControl("DropDownList2"));
Any help will be appreciated.
Please write this code at end of the code
Gridview1.EditIndex-1 not sure EditIndex or Edititemindex please try it may be help you.
Try using a condition
if(name_value.Text!="" && age_value.Text && sex_value.Text && berth_value.Text)
{
//action
}

Radiobuttonlist selected value

Could someone help me with this solution, stuck on it.
I have a list and filling radiobuttonlist with data, but I can't get selected value when i press submit button.
List<CustMobilePhonesEntity> cusMobile = GetCusMobile(Email);
RadioButtonList1.Items.Add(customerMobile[0].PhoneNumber);
RadioButtonList1.DataSource = customerMobile;
RadioButtonList1.DataTextField = "PhoneNumber";
RadioButtonList1.DataValueField = "PhoneNumber";
RadioButtonList1.DataBind();
Label1.Text = RadioButtonList1.SelectedValue;
Any ideas what I'm doing wrong, thank you.
First you need to check you are doing it in this fashion (If not the list is again binded with the DataSource when you click submit and the selection is lost)
if(! IsPostBack)
{
RadioButtonList1.DataSource = customerMobile;
RadioButtonList1.DataTextField = "PhoneNumber";
RadioButtonList1.DataValueField = "PhoneNumber";
RadioButtonList1.DataBind();
}
Also since you are binding RadioButtonList1.Items.Add(customerMobile[0].PhoneNumber); this won't be required (not clear if anything else).
Also see that ViewState is enabled

Do not show confirm messagebox if gridview checkbox is not checked

I have gridview which has columns of data and a button below the "Apply" to perform some action.
One column in Gridview is a checkbox column. When you select checkboxes and click apply a confirm message pops up asking "Are you sure?" with option of "Yes" and "No".
But if no check box is checked i want to show a popup to user to select a checkbox before performing an action and not show the confirm message popup.
What i have now is if no checkbox is selected the confirm message pops up first and if i press "yes" then an alert message pops up to select atleast one checkbox. If i press cancel no pop up shows.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ApplyAction_Button.Attributes.Add("onclick", "return confirm('Are you sure?');");
}
}
protected void ApplyAction_Button_Click(object sender, EventArgs e)
{
// Gets the selected checkboxes in the gridview
ArrayList selectedMachines = new ArrayList();
GetSelectedMachineIds(selectedMachines);
if (selectedMachines.Count == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('Please select a machine for the action to be applied');</script>");
return;
}
// Action to be applied
}
I am trying to avoid the use of postback.
Another question:
Is there a server side confirm message box in asp.net?
Any help will be appreciated. Thanks
You can set your button visible to false, once check box is checked then you can show the button.
another way is to use javascript to check if checkbox is checked
code behind
ApplyAction_Button.Attributes.Add("onclick", "ShowConfirm();");
javascript
function ShowConfirm()
{
//check if checkbox is checked if is checked then display confirm message else display alert message
if( "assumed checkbox is checked"){
return confirm("message here");
}
else{
alert("message here");
}
}
try to see this thread
Checkbox in gridview
Determining a which checkbox is checked on a datagrid and updating DB
Regards

Categories