Confirm DropDownList selected value - asp.net

I want to show the user a confirm dialog when he tries to change the selected item of a DropDownList. If the selection is confirmed i want to execute some server side code.
Say, DDL has values 1 and 2.
Value 1 is selected (default).
The user selects Value 2. A confirm dialog appears.
If the user selects 'Yes', then the selected item changes. Some server side code must be executed.
If the user selects 'No' then the selected item is reverted back to Value 1. No server side code executed.
I'm having a lot of trouble with this one, since DDL has few events to use.
So far i got
this.MyDropDown.Attributes["onChange"] = #"return confirm('Are you sure?');";
and a event handler for the SelectedIndexChanged event of the DDL for the server side code.
But i'm having trouble with the fact that i can't neither stop (or revert) the item being changed nor the SelectedIndexChanged event being fired.
Any suggestions?

The reason it's not triggering the server side event is because you're wiping out the built-in webforms event handler that would trigger the post back. As for reverting the value, you'll need to save it and then reload it.
add this javascript function
function handleChange(opt) {
if (!confirm('are you sure')) {
opt.selectedIndex = opt.oldIndex;
}
else {
__doPostBack('MyDropDown','')
}
}
and set the client side events like so
this.MyDropDown.Attributes["onChange"] = "handleChange(this)";
this.MyDropDown.Attributes["onFocus"] = "this.oldIndex = this.selectedIndex";

This is complete solution, as i have already used it. Just refer the following instructions:
// Paste it in Aspx file
function handleChange(opt) {
if (!confirm('Are you sure?')) {
opt.selectedIndex = opt.oldIndex;
}
else {
__doPostBack('MyDropDown','')
}
}
Paste this in Page Load event outside IsPostBack:
this.MyDropDown.Attributes["onChange"] = "handleChange(this)";
this.MyDropDown.Attributes["onFocus"] = "this.oldIndex = this.selectedIndex";
Note: Set AutoPostBack Proerty False of Dropdownlist.

Related

CheckBox Check is not Working on server side in Gridview

I am facing issue in Gridview Check box checked and server side checkbox is showing checked = false.
Its really strange and I haven't seen yet before.
I have written following code.
<script type="text/javascript">
function SelectAll() {
if ($('.SelectAll input:checkbox').attr("checked"))
$('.chkTechs input:checkbox').attr("checked", true);
else
$('.chkTechs input:checkbox').attr("checked", false);
}
function SetCheckBoxes(item) {
//$(item).attr("target").checked // this is to find which element clicked
if ($('.chkTechs input:checkbox').filter(":not(:checked)").length > 0) {
$('.SelectAll input:checkbox').attr("checked", false)
}
else {
$('.SelectAll input:checkbox').attr("checked", true)
}
}
</script>
Server side Button Click
foreach (GridViewRow row in gvList.Rows)
{
CheckBox Checked = (CheckBox)row.FindControl("chkSelect");
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
}
The state of server controls is maintained in viewstate and changing the state of control like you are changing the checked status of checkbox with client script (javascript) is not updated in viewstate. So when you access the control on server side you do not get the changes. You have to store the changes in some hidden field and use that hidden field on server side to update your controls. It is the way asp.net implements the viewstate.
You have to get their value by checking the existence of Request.Form[xxx] parameter of the corresponding checkbox. In you case [chkSelectXXX].
1) Append something meaningful to the ID of your checkbox while creating it. Ex: the primary key val so that the ID of the checkbox should be [chkSelect_PKValue1]
2) On server side loop through Request.Form variables and check for the existence of variables that have the key value starting with chkSelect. Something like this:
foreach(var x in Request.Form)
{
if(x.StartsWith("chkSelect"))
{
//3. Then this checkbox is selected you should parse the
//PK value and do what's necessary.
}
}

Why are my ASP.NET checkboxes always false?

I'm working on a ASP.NET web forms application. I have a four-column listview, bound to a datasource on pageload(), populated with contact names. One of the columns contains a checkbox. Users select a checkbox to indicate the corresponding contact should be processed in the next step.
The form also contains a button. When this button is clicked, the following code runs to process the selected contacts.
foreach (var x in lvPeople.Items)
{
chkSelected = (CheckBox)x.FindControl("IsLetterRecipient");
if (chkSelected.Checked)
{
// the person was selected by the user, do stuff here...
}
}
When I set a breakpoint on the line containing the IF statement, the breakpoint gets hit seven times (once for each row in the listview == seven checkboxes). However, the code inside the IF block never runs because .Checked is always False, regardless of the whether or not the checkbox is actually checked.
AutoPostBack, on the checkbox, is set to False. EnableViewState on the checkbox and listview is set to True.
What am I doing wrong? How do I get the .Checked status of the checkboxes?
Probably, when you bind the data on Page_Load you forgot to do:
if(!IsPostBack)
{
//bind the data to the list
}

How to end/stop item edit in Flex DataGrid

Normally, you update the data bound to a datagrid item like this:
protected function onGridEditEnd(event:DataGridEvent):void
{
if (grid.dataProvider != null && event != null)
{
var editor:Object = event.currentTarget.itemEditorInstance;
if (event.columnIndex == getColumnIndex(columnA) {
collection[event.rowIndex].name = TextInput(editor).text;
}
}
}
The itemEditEnd event is dispatched right after the user finishes editing.
So, what if I want to
End the edit (from another method or handler other than itemEditEnd handler)
Update the value of the bound data item at the same time.
"while" the item is being edited by the user.
For example; how do I trigger this from a keyDown event handler?
Note: You could call onGridEditEnd(null) which would end the edit but not update the data. onGridEditEnd(new DataGridEvent(DataGridEvent.ITEM_EDIT_END, ...)) would work but you also need to store and pass values like columnIndex, rowIndex, dataField, etc.. which does not seem to be the best and simplest way.
thanks...
try setting editable property to false (then you can turn it back on).
Or you could try setting focus (FocusManager.focus = null) elsewhere. This way it will be still editable, but the user will have to click it again.

Asp.net CheckBox within GridView looses its value

I have a GridView which has bound fields and a template field for checkbox. I wrote a code for deletion of records as per checking checkboxes. My problem is
HtmlInputCheckBox chk;
foreach(GridViewRow dr in dgvdetails.Rows)
{
chk = (HtmlInputCheckBox)dr.FindControl("ch");
chk.Checked = true;
if (chk.Checked)/// **here checkbox is not checked even if I'm check it**
{
pl.id = int.Parse(chk.Value);
bl.deletedgvdetails(pl);
}
}
There are two things that come to mind that could be at play here. I'm not sure when your code is running - with regard to the page lifecycle - but if it is being handled in a button click event you may want to make sure that your grid view has not been "rebound" during a page event such as Page_Load which runs prior to button click events firing. IF this is the case, all of your check boxes will have been reinitialized before your delete method runs.
Secondly, inside your foreach loop you may want to pay attention to the RowType of the current row. The first iteration through the loop may be focusing on the Header Row (if you have it enabled for the GridView) and therefore may not have the checkbox.
Checking that if(chk != null) {} is always a good idea if you use e.Row.FindControl() - just in case.

ASP.Net Client Events on DropDownList?

Is there a client event that I can use for when a DropDownList's data has been loaded/bound onto the control? I need to trigger event on their side when this happens.
Basically, I am trying to lock out the controls while the data is being loaded as if there is a slowdown (not uncommon) a user can start inputting data and then lose focus as they are typing.
I tried doing this in the tags but the methods located there seem to stop working after the first postback! (Any help there would be greatly appreciated). As a workaround I tried attaching the events to the elements themselves and while this works for locking, using the onchange event, I am unable to unlock it upon the data successfully loading!
Any ideas? Thanks for the answers so far :)
Since data will be bound on the server side, you don't have a client-side event for that specific event, however, one the page has rendered, the data will be there, so you may want to run your client script in the document.load event, or using something like jQuery's document.ready event. That will trigger your script to run once the page (including your bound drop down) is finished loading.
Jason is correct here in that you cannot "notify" the client when such an event occurs. One thing you could do, is call the Page.RegisterStartupScript() method to do something with JavaScript once the page has finished loading (and assumedly that the post back that has done your databinding has occurred). Again, this assumes that you want to do something on the client side once the data binding is complete, as opposed to server side.
Are you able to use ASP.NET AJAX in your application? If so, you can have the selected event open up a modal dialog in which you can display your "processing" text while you are populating the drop down list. That way the user does not have access to any other controls and you can do what you need without worry.
i use the following code in my master pages for my websites. This stops the user from attempting to use a control before its completely bound. I have found that if a control hasn't been completely bound (slow connections) then the page blows up.
Essentially the script hijacks the post back if that page isn't done. Allowing the user to not do anything until the page has finished processing. I wrote this a year ago and its come in very handy.
first set the onload body tag to setdopostback()
add this in a scrip block in the body.
var boolDoPostBack = false;
if (__doPostBack)
{
// save a reference to the original __doPostBack
var __oldDoPostBack = __doPostBack;
//replace __doPostBack with another function
__doPostBack = AlwaysFireBeforeFormSubmit;
}
function setdopostback()
{
boolDoPostBack = true;
}
function AlwaysFireBeforeFormSubmit (eventTarget, eventArgument)
{
var x= document.readyState
if (x != "complete")
{
if (x == "loading" || x == "interactive" || x == "unitialized" || x == "loaded")
{
//do nothing with IE postback
}
else if (!boolDoPostBack)
{
//do nothing with FireFox postback
}
else
{
//alert('Allow Postback 1');
return __oldDoPostBack (eventTarget, eventArgument);
}
}
else
{
//alert('Allow Postback 2');
return __oldDoPostBack (eventTarget, eventArgument);
}
}

Resources