Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

How to fire ImageButton "OnClick" event after OnClientClick event using c# asp.net?

I want to fire ImageButton OnClientClick event first and then OnClick event,
OnClick event should not be fire till completed work of OnClickEvent.

Or

How can we create OnClientClick Event Script in OnClick event using c# asp.net?

Please help me.

Thanks in Advance.

Ankit Agarwal
Website Developer
Posted

At the end of OnClientclick script just put return =true.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 20-Jan-14 6:51am    
In fact NOT returning false is enough... :-)
JoCodes 20-Jan-14 10:01am    
Yes, you have actually answered what OP asked.
try like this..

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var dosomethig = function () {
            if (true) { // your condition here
                alert('do something');
                return true;
            }
            else {
                alert('do something');
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ImageButton ID="ImageButton1" runat="server" Height="28px" Width="100px" OnClientClick=" return dosomethig();"
        ImageUrl="~/background.gif" OnClick="ImageButton1_Click" />
    </form>
</body>
</html>
 
Share this answer
 
Comments
[no name] 20-Jan-14 7:19am    
Hi Karthik,
I am using this code for image upload and display image in image button but my image did not display in image button and did not save into the path.

<script type="text/javascript">
function chooseFile() {
document.getElementById("UploadLogo").click();
return true;
}
</script>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<contenttemplate>
<div class="frant-page-header"><div class="frant-page-header-logo">
<div style="height: 0px; overflow: hidden">
<asp:FileUpload ID="UploadLogo" runat="server" />
</div>
<asp:ImageButton ID="ImageLogo" runat="server" ImageUrl="~/images/64.png"
style="width: 100px; height: 100px; line-height: 100px;"
onclick="ImageLogo_Click" OnClientClick="return chooseFile();" />



Code Behind Code:-

protected void ImageLogo_Click(object sender, ImageClickEventArgs e)
{
Boolean FileOK = false;
Boolean FileSaved = false;

if (UploadLogo.HasFile)
{
Session["UploadLogo"] = UploadLogo.FileName;
String FileExtension = Path.GetExtension(Session["UploadLogo"].ToString()).ToLower();
String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (FileExtension == allowedExtensions[i])
{
FileOK = true;
}
}
}

if (FileOK)
{
try
{
UploadLogo.PostedFile.SaveAs(path + Session["UploadLogo"]);

FileSaved = true;
}
catch (Exception ex)
{
LabelWelcome.Text = "File could not be uploaded." + ex.Message.ToString();
LabelWelcome.Visible = true;
FileSaved = false;
}
}
else
{
LabelWelcome.Text = "Cannot accept files of this type.";
LabelWelcome.Visible = true;
}

if (FileSaved)
{
//pnlUpload.Visible = false;
//pnlCrop.Visible = true;

ImageLogo.ImageUrl = "Templates/" + Session["UploadLogo"].ToString();
//uploadImage.ImageUrl = "Templates/" + Session["WorkingImage"].ToString();
}

//Session["TempID"] = TextTemplateId.Value.ToString();
}

Please help me Ho can we display image in image button and save image into the path.
[no name] 20-Jan-14 7:36am    
I can not upload Image properly, Please help me.
Karthik_Mahalingam 20-Jan-14 7:58am    
what is your task
pls explain clearly ..
you need to show in the image button or upload ??
[no name] 20-Jan-14 8:08am    
I don't want to have any File Upload button or Browse Button for Image Upload.
I need to Only One ImageButton and click on that and then it should be Uploaded Image Shown in ImageButton and My Selected Image Save also in my path.
Like this:-
http://jasny.github.io/bootstrap/javascript/

Image upload widgets

I have tried this code and script but my image did not saved in my path.
Please help me any solution for this.
Karthik_Mahalingam 20-Jan-14 8:11am    
ok i got your problem
wait..

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