This is a migrated thread and some comments may be shown as answers.

OnClientClick event is getting fired for <asp:ImageButton>

1 Answer 635 Views
Input
This is a migrated thread and some comments may be shown as answers.
Medac
Top achievements
Rank 1
Medac asked on 05 May 2010, 09:30 AM
Hi,

I have asp imagebutton in my page. By default i am loading disabled image. and have set the property of Enabled = false in the page. On Change of any field value in the page i am making the disabled image as enabled as below. After this if I click on Enabled image it is not firing OnCLientClick javascript method. Why? Any body please help me.

On load of the Screen

 

 

<asp:ImageButton ID="imgbtnOk" runat="server" ImageUrl="~/Images/btn_ok_disabled.gif"

 

 

 

OnClientClick="return ValidateInformation()" AccessKey="O" ToolTip="OK" TabIndex="30" />

On updatation of any field in the screen following javascript has been fired to enable the button

 

//Enables the Ok button if user selects any Carrier Plan Code

 

 

 

 

 

 

function EnableOk() {

 

 

 

var cboCarrierPlanCode = document.getElementById(radcboCarrierPlanCode)

 

 

 

if (cboCarrierPlanCode.value != "") {

 

 

 

var updateflag = document.getElementById(hdnChangeFlag)

 

updateflag.value = 1;

 

 

var imgBtnOk = document.getElementById(imgbtnOk)

 

imgBtnOk.disabled =

 

false

 

 

 

 

 

imgBtnOk.src =

 

"../Images/btn_OK.gif"

 

 

 

 

 

}

}

After this image is getting enabled but initially for the control i have written OnClientClick event to validate some more fields in the screen.... this is not getting fired.

WHY?

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 07 May 2010, 09:32 AM
Hi Medac,

#1:
One suggestion would be using client code to disable the ImageButton instead of using the Enabled property from code. Use the "RegisterStartupScript" method to invoke client function for disabling the button.

CS:

        protected
 void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                string script = "document.getElementById('"+ imgbtnOk.ClientID+"').disabled = true;"
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "disable", script, true); 
            } 
        } 

#2:
Another suggestion is using Enabled property to disable the button from server code and when enabling from client, reattach the OnClick event as shown below.

Javascript:
 
    function EnableOk() 
     { 
        ............ 
        var btn = document.getElementById("<%=imgbtnOk.ClientID %>"); 
        if (btn && btn.disabled == true
        { 
            btn.disabled = false// Enable the button
            btn.onclick = ValidateInformation; //Re-attach the OnClick property
        } 
        . . . . . 
    }

    function ValidateInformation(sender,eventArg)
    {
        . . . . .
    }

Hope these suggestion help you.

Cheers,
Princy
Tags
Input
Asked by
Medac
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or