how to show confirmation dialog from drop down list? - c#

i have a grid which display data from database, and i have a custom column in the left side with checkbox, i choose records to be deleted and i have a dropdown list, which will trigger an event in server side to delete records, before i delete those records i want to show a confirmation dialog like "are you sure? with ok and cancel", how to do that? any thought?
i do this :
if(ddlAction.SelectedValue == "Delete")
{
string id = string.Empty;
int i = 0;
List<int> idx = new List<int>();
foreach (GridViewRow rowitem in gvDept.Rows)
{
CheckBox itemchk = (CheckBox)rowitem.FindControl("cbSelectOne");
if (itemchk != null & itemchk.Checked)
{
id += rowitem.Cells[3].Text.ToString() + ',';
idx.Add(i);
}
i = i + 1;
}
id = id.Trim(",".ToCharArray());
List<string> objRemoveKeys = id.Split(',').ToList();
if (objRemoveKeys.Count > 0)
{
ddlAction.Attributes.Add("OnChange", "javascript:return confirmDeletion('Are you sure you would like to remove the selected items?');"); // this part not working.
AirAsiaLinqDataContext LinqDataCtx = new AirAsiaLinqDataContext();
var record = from a in LinqDataCtx.departements
where objRemoveKeys.Contains(a.departementcode)
select a;
LinqDataCtx.departements.DeleteAllOnSubmit(record);
LinqDataCtx.SubmitChanges();
for (int j = 0; j < idx.Count; j++)
{
gvDept.DeleteRow(idx[j]);
}
}
ddlAction.SelectedValue = "";
}

This looks like code-behind (C#) code. Dialogs happen on the client side. You can do this relatively easily with jQuery (or even vanilla JavaScript code), or use something like the Ajax Control Toolkit's ConfirmButton:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ConfirmButton/ConfirmButton.aspx
For a little more control over the process you can also give JuiceUI a go: http://juiceui.com/controls/dialog

try this
ddlAction.Attributes.Add("onchange", "return confirm('Are you sure you would like to remove the selected items?');");

you should not only show an alert for confirmation but also check if the user has selected some row or not. below code accomplishes both.
javascript function:
function checkIfSelected() {
if (yourGrid.GetSelectedRowCount() == 0) {
alert("You must select atleast one.");
return false;
}
else {
if (confirm("Are you sure you want to proceed?")) { // This is what you want
}
else {
return false;
}
}
}
your dropdownlist:
<asp:DropDownList ID="ddlAction" onChange="javascript:if( checkIfSelected() == false){return false};" AutoPostBack="true" runat="server" OnSelectedIndexChanged="yourID_SelectedIndexChanged">

Related

selected value cleared in postback

I've dynamically bonded select list and when I hit save button then I'm getting value 0 not the selected one.
I'm Using HtmlSelect not Asp:Dropdownlist.
Can anybody help me.?
thanks in advance !!
If you are using html select.,
you use javascript function for change ddl and assign changed ddl value at one hidden field. send that hidden field value to server. Check below code.
function onchangeddl(e) {
var ddl = document.getElementById('ddlid')
for (var i = 0; i < ddl.options.length; i++) {
if (ddl.options[i].text == e.target.options[e.target.options.selectedIndex].text) {
ddl.selectedIndex = i;
ddl.options[i].selectedValue = e.target.options[e.target.options.selectedIndex].value;
ddl.options[i].selected = true;
document.getElementById('<%=hdnfld.ClientID%>').value = e.target.options[e.target.options.selectedIndex].text;
break;
}
}
}
Codebehind.aspx page you have to assign hdnfld value.
I hope its helpful to you.

Uncheck the first checked CheckBox when count exceeds the Maxlimit

I have a CheckBoxList and my requirement is to allow user to select maximum three options(through Javascript) and as soon as he clicks on the fourth one the first selected CheckBox gets unchecked, similarly when he selects the fifth (another) one, the CheckBox which was selected second, gets unchecked and so on. Eventually user is left with only three selected options.
Eg. In the given image if user selects .Net(first),Java(second),PHP(third) and when he selects SQL(fourth), .Net gets unchecked and SQL gets checked. Further when he selects Cloud Computing(fifth), Java gets unchecked.
I have written the following Javascript which works fine for the first scenario and un- checks the first selected item when fourth one is selected but it doesn't work further because counter value again reaches to 4 and cbArray[counter - 4] again tries to uncheck the first box instead of second. How to resolve this problem. Thanks.
My Javascript:
<script type="text/javascript" language="javascript">
function limitChecked(maxCount) {
debugger;
var ocbList = document.getElementById('cbList');
var cbArray = ocbList.getElementsByTagName('input');
var counter = 0;
for (var i = 0; i < cbArray.length; i++) {
if (cbArray[i].checked==true) {
counter++;
if (counter > maxCount) {
cbArray[counter - 4].checked = false;
}
}
}
}
</script>
.aspx code:
<body>
<form id="form1" runat="server">
<div>
Courses:
<asp:CheckBoxList ID="cbList" runat="server" onclick="limitChecked(3)">
<asp:ListItem>.Net</asp:ListItem>
<asp:ListItem>Java</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
<asp:ListItem>SQL</asp:ListItem>
<asp:ListItem>Cloud Computing</asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
As per Trendy's suggestion (McGarnagle's answer also helped) I've come up with this solution, which is working fine for me.
function limitChecked(maxCount) {
debugger;
var ocbList = document.getElementById('cbList');
var cbArray = ocbList.getElementsByTagName('input');
var checkedArray = [];
for (var i = 0; i < cbArray.length; i++) {
if (cbArray[i].checked == true) {
checkedArray.push(i);
if (checkedArray.length > maxCount) {
checkedArray = checkedArray.slice(0);
cbArray[checkedArray[0]].checked = false;
}
}
}
}
You could use push/pop instead of the counter. Using slice(0) allows you to use an array like a FIFO queue:
function limitChecked(maxCount) {
debugger;
var ocbList = document.getElementById('cbList');
var cbArray = ocbList.getElementsByTagName('input');
var checked = [];
for (var i = 0; i < cbArray.length; i++) {
if (cbArray[i].checked==true) {
checked.push(i);
if (checked.length > maxCount) {
cbArray[checked[0]].checked = false;
checked = checked.slice(1);
}
}
}
}

Accessing Controls in Header/FooterTemplate C#

My problem is I have a button in the FooterTemplate and HeaderTemplate (In a datalist) and I need to disable them / enable them based on a bool. I know about using the ItemDataBound, but with the way the rest of the page is setup that does not work because the only time it is bound is the initial page load.
I at one time had this working using javascript, but something happened and my javascript is no longer doing the job. So I would like to just be able to do it through the codebehind. So is there anyway to use a foreach loop to access the control?
In the events I am trying to do this I have the following loop:
foreach (DataListItem dli in list.Items)
{
int qty = Convert.ToInt32(((TextBox)dli.FindControl("qtyTextBox")).Text);
int productID = Convert.ToInt32(((Literal)dli.FindControl("prodId")).Text);
isQtyValid = COMMONACES.GetValues.getTotalQty(sessionID, productID, qty);
lblError.Visible = !isQtyValid;
lblError.Text = isQtyValid ? string.Empty : "The total quantity for one or more items exceeds the maximum. The total quantity includes items already in the cart.";
Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "SetButtonStatus()", true); //used to access javascript
}
Javascript:
function SetButtonStatus() {
var bb = document.getElementsByClassName('ButtonSubmit');
for (var i = 0; i < bb.length; i++) {
bb[i].disabled = <%=(!isQtyValid).ToString().ToLower()%>;
}
}
I am trying to do this in a text changed event as well as in a selectedindexchanged event if that is helpful info. If any other info is requested I will do my best to provide it.
Thank you for any help you can offer.
Following your code:
Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "SetButtonStatus();", true); //used to access javascript
and in SetButtonStatus()
document.getElementById('<%= ControlButton.ClientID %>').disabled = true/false;
If your code previously worked and has now stopped, make sure you don't have a JavaScript error, i.e. displayed data from data list is causing that problem.
bool isNotValid= false;
foreach (DataListItem dli in list.Items)
{
int qty = Convert.ToInt32(((TextBox)dli.FindControl("qtyTextBox")).Text);
int productID = Convert.ToInt32(((Literal)dli.FindControl("prodId")).Text);
isQtyValid = COMMONACES.GetValues.getTotalQty(sessionID, productID, qty);
lblError.Visible = !isQtyValid;
lblError.Text = isQtyValid ? string.Empty : "The total quantity for one or more items exceeds the maximum. The total quantity includes items already in the cart.";
if(!isQtyValid)
isNotValid=true;
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "function", string.Format("SetButtonStatus('{0}')", isNotValid.ToString().ToLower()) , true); //used to access javascript
function SetButtonStatus(isNotValid) {
var bb = document.getElementsByClassName('ButtonSubmit');
for (var i = 0; i < bb.length; i++) {
bb[i].disabled = isNotValid;
}
}

Drag Re-order on Telerik Radgrid

I am trying to use the drag re-ordering on my radgrid. The code I have works well for me (it fires on the RowDrop event) but my client cant get it to work and I have troubleshooted it down to show that when he does the drop, the DestDataItem property of the args is null, so the drop logic never triggers?!? here is my code:
protected void questionGrid_RowDrop(object sender, GridDragDropEventArgs e)
{
if (e.DestDataItem != null)
{
int tempId = int.Parse(editingId.Value);
theTemplate = ent.SurveyTemplates.Where(i => i.Id == tempId).FirstOrDefault();
int id = int.Parse(e.DraggedItems[0]["Id"].Text);
SurveyQuestion draggedQuestion = ent.SurveyQuestions.Where(i => i.Id == id).FirstOrDefault();
List<SurveyQuestion> tempArray = theTemplate.Questions.OrderBy(i => i.Rank).ToList();
tempArray.Remove(draggedQuestion);
tempArray.Insert(e.DestDataItem.ItemIndex, draggedQuestion);
int j = 0;
foreach (SurveyQuestion sq in tempArray)
{
sq.Rank = j;
j++;
}
ent.SaveChanges();
questionGrid.Rebind();
}
else
{
Exceptions.LogException(new Exception("NULL DEST"));
}
}
It just references the dragged item and pulls it from the list of items and re-inserts it at the new index, then it updates the rank property of each item to its new index and saves.
Why would this work for me and not for him? Could this server side code be bothered by browser differences?
As mentioned in this thread, if the item isn't dropped on an actual data row in the grid, the DestDataItem will be null.
You can prevent your RowDrop event from firing, if the target isn't a data row, by handling the OnRowDropping event on the client side and ignoring the things you don't want:
function gridRowDropping(sender, args)
{
if (!args.get_targetGridDataItem())
args.set_cancel(true);
}

How to enable disable fields using Java Script in Webform

I have created a web page "Default.aspx" in which I have taken fields:
First Name, Last Name, Account Titling, Titling(radio button list), AccountNumber and AccountFormat
under the "Default.aspx" page, I have used a radio button list also, whose values are Yes and No. If I choose Yes, then the following fields visibility should set to false:
First Name, Last Name
If I choose "NO", then the following fields visibility should set to true:
Account Titling, Account number
For this, I have written the below Java Script code in "Default.aspx"
function EnableDisableTaxID() {
if (document.getElementById("<%=rdOpeningSubAccount.ClientID %>") != null) {
var openSubAccountList = document.getElementById('<%= rdOpeningSubAccount.ClientID %>');
var fbo1RadioList = document.getElementById('<%=fbo1RadioButtonList.ClientID %>').value;
var isOpenSubAccount;
if (openSubAccountList != null) {
var openSubAccount = openSubAccountList.getElementsByTagName("input");
for (var i = 0; i < openSubAccount.length; i++) {
if (openSubAccount[i].checked) {
isOpenSubAccount = openSubAccount[i].value;
alert("Print" + isOpenSubAccount);
}
}
}
alert(typeof(isOpenSubAccount));
if (isOpenSubAccount == 'true') {
FirstName.visible = true;
LastName.visible = false;
AccountTitling.visible = true;
lblFirstName.visible=false;
lblLastName.visible=false;
}
else if (isOpenSubAccount == 'false') {
AccountTitling.visible = true;
AccountNumber.visible = false;
lblAccountTitling.visible = true;
lblAccountNumber.visible = false;
}
}
}
However, I am getting the required value from the Radio button list, however, when I go to check if the selected value of the radiobuttonlist is true, then the code above does not work. I dont know what am I missing. I know that directly using the below code will not work:
if (isOpenSubAccount == 'true') {
FirstName.visible = true;
LastName.visible = false;
AccountTitling.visible = true;
lblFirstName.visible=false;
lblLastName.visible=false;
}
Please help as I m stuck here...
For Visible = false;
document.getElementById('FirstName').style.visibility="hidden";
For Visible = true;
document.getElementById('FirstName').style.visibility="visible";
To Enable:
document.getElementById('FirstName').disabled = false;
To Disable:
document.getElementById('FirstName').disabled = true;
Following can be done for
Non Visible
document.getElementById('id-name').style.display='none';
Visible
document.getElementById('id-name').style.display='block';
Disable
document.getElementById('id-name').setAttribute('disabled', 'disabled');
Enable
document.getElementById('id-name').removeAttribute('disabled');
No; document.getElementById will only get the element with the ID you specify (the HTML spec is quite clear that only one element on a page can have a specific ID).
Each radio button has a different ID attribute, but if you look at the HTML source of a page, you will see that all radio buttons in the list have the same NAME attribute. This is what you should use "the name of the radio button".
onclick="GetRadioButtonValue('<%= radiobuttonlist1.ClientID %>')"
function GetRadioButtonValue(id)
{
var radio = document.getElementsByName(id);
for (var j = 0; j < radio.length; j++)
{
if (radio[j].checked)
alert(radio[j].value);
}
}

Categories