Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using one datalist in my project with item command property and itemdataboundproperty.In item data bound i call one javascript in imagebutton?? after do this my itemcommand property is not firing.

Then i create the button in datalist and call the button click in same javascript. Now item command property is firing but the javascript modalpopup is not displaying.

if one is working and then another one is not working. but i must need two functionalities to work.

How can i do this. help any one please
Posted
Updated 26-May-11 9:09am
v2

1 solution

you need to return true value from javascript function you are invoking. The page will be post back to the server if javascript function returns true at the end.

By default javascript function will return false, that is why your page is not posting back the data.
here is an example:
<input type="submit"  önclick="return doSomething();" />

<script>
function doSomething()
{
 ''do something...
return true;
}
</script>


Hope this will help
Thanks,
hemant

Edit:

btntest.Attributes.Add("onclientclick", "javascript:ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');return false;");

There are 2 problems with your code:

1- onclientclick:
The Problem: OnClientClick is a asp.net button's server side property it can't be added like this, while Attributes collection keeps attributes to be rendered for client end as-it-is. The above line will generate something like below for client:

<input type="Submit"  önclientclick="javascript:ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');return false;" />

The onclientclick in the above client code should be onClick

The Solution: you need to modify your code to add onClick attribute instead of onclientclick, like this:
btntest.Attributes.Add("onClick","...your javascript code....");

or
btntest.OnClientClick = "javascript:ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');return false;"


The above code will make sure your Javascript gets executed but it wont guarantee that itemcommand event will be raised because there is a problem in your javascript that wont allow your button to post current page back to server i.e. no postback. To solve it see Problem and solution 2.

2- Javascript:
javascript:ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');return false;

The problem: The above code is identical to below javascript function:

function OnClickFunction()
{
     ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');
     return false;
}


When you will click on the button, 2 statements will be executed first the ModalPopupsConfirm function and then return false. Because at the end you are returning false you are asking your browser to not submit your page because of some validation failure on button click.

The Solution:
The logic of your page demands: if user clicks yes in the pop-up post the page back and raise the itemcommand event but if user says no to the confirm pop-up don't do anything and stays on the page.
2.1 - Remove return false
2.2 - Return proper bool from ModalPopupsConfirm function. (i'm not sure about this i dont' work with ajax modal popup)
C#
function ModalPopupsConfirm(hidden, Pdtdesc) {
            var ItemCount = gethdnvalue(hidden);
            var ProductName = document.getElementById(Pdtdesc).innerHTML;
            document.getElementById(hidden).value = ItemCount;
            return ModalPopups.Confirm("idConfirm1",
        "Confirm your Order",
        "<div style='padding: 25px; width:300px'>You Selected " + ItemCount + " Items For " + ProductName + "<br></br>Are you sure?</div>",
        {
            yesButtonText: "Yes",
            noButtonText: "No",
            onYes: "ModalPopupsConfirmYes()",
            onNo: "ModalPopupsConfirmNo()"

        }

    );

add return before ModalPopups.Confirm... code
2.3 - add return before javascript function call in Attributes.Add or OnClientClick
btntest.Attributes.Add("onClick", "return ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');");


or

btntest.OnClientClick = "return ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');";


Final Solution:
implement the changes in point 2 i.e. from 2.1 to 2.3

Additional Link:
there is an article doing exactly what you want to achieve. i'm adding it for an example and because i'm not sure how ModalPopup works at client end:
http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html[^]

Hope this will resolve your issue and help you better understand about how things gets processed.

Thanks,
Hemant
 
Share this answer
 
v2
Comments
janablisslogix 26-May-11 8:45am    
Thank you for your replay. I tried (return true) also but it is nothing changed. how can i get fired please
Hemant__Sharma 26-May-11 12:17pm    
Post your code with .aspx markup + javascript
janablisslogix 30-May-11 4:30am    
function ModalPopupsConfirm(hidden, Pdtdesc) {
var ItemCount = gethdnvalue(hidden);
var ProductName = document.getElementById(Pdtdesc).innerHTML;
document.getElementById(hidden).value = ItemCount;
ModalPopups.Confirm("idConfirm1",
"Confirm your Order",
"<div style='padding: 25px; width:300px'>You Selected " + ItemCount + " Items For " + ProductName + "<br></br>Are you sure?</div>",
{
yesButtonText: "Yes",
noButtonText: "No",
onYes: "ModalPopupsConfirmYes()",
onNo: "ModalPopupsConfirmNo()"

}

);

//document.getElementById(btntest).click();
}

function ModalPopupsConfirmYes() {
ModalPopups.Close("idConfirm1");

}

function ModalPopupsConfirmNo() {
//alert('You pressed No');
ModalPopups.Cancel("idConfirm1");
//return false;

}

function gethdnvalue(hidden) {

var Item = document.getElementById(hidden).value;
var ItemCount = parseInt(Item) + parseInt(Item);
return ItemCount;
}

function ItemCommand(btntest) {
document.getElementById(btntest).click();
return true;
}

that is my javascript and my codebehind is below

Button btntest = (Button)e.Item.FindControl("btntest");
ImageButton btnplus = (ImageButton)e.Item.FindControl("btnplus");
HiddenField hdnmin = (HiddenField)e.Item.FindControl("hdnminorder");
Label Pdtdesc = (Label)e.Item.FindControl("lbldescrip");
btntest.Attributes.Add("onclientclick", "javascript:ModalPopupsConfirm('" + hdnmin.ClientID + "','" + Pdtdesc.ClientID + "');return false;");
Hemant__Sharma 1-Jun-11 0:06am    
Check my updated solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900