Change picture on mouse hover - c#

How to set in ASP.NET for ImageButton to change picture on mouse hover?

<asp:ImageButton onmouseover="changePic(this);" ...
JavaScript:
function changePic(obj)
{
obj.src = "<picture path>";
}

You will have to do this in the client side - that is, using javascript.
You can add a client side onMouseOver="this.src = 'pathToOtherimage'" attribute and a similar one for onMouseOut to revert back.

It can't be done from the server-side (in the C# code), but rather must be done on the client-side (in JavaScript). Starting with that fact, there are various approaches to the subject.

Related

ASP.NET : Avoid multipile clicks on a button

I'm using DevExpress ASPxButton, and I was wondering how can I prevent the user from clicking the button more then once?
Basically it does an update/insert statement.
This can be done using the following code:
var buttonClicked = false;
function MyBtnClick(s,e){
if(buttonClicked) return;
buttonClicked = true;
// do something
}
<ASPxButton id="btn" runat="server">
<ClientSideEvents Click="function(s,e) { MyBtnClick(s,e); } "/>
</ASPxButton>
On the client side, you can disable it when it's clicked.
On the server side, my favourite approach is generally to send a nonce with the form data, and then reject duplicate nonces.
As always, client side validates in a fast and (hopefully) friendly way, server side validates for sure even if client side code suffers from some unexpected browser issue, or someone is deliberately messing things up.
Not sure about aspxbutton specifically but in general you will have something like this on the onclick attribute of the control.
onclick="this.disabled=true;"
In the code where you run the update query simply add to the start of the code:
ButtonNameOrID.Enabled = False // add ; to end if C#
Then at the end of the code, add:
ButtonNameOrID.Enabled = True // add ; to end if C#
This is will disable/enable the button from your C# or VB code.

page postback in asp.net?

in my application i have the playvideo page where video will play, below that i have the option for sharing the video like adding to favorite ,playlist and sending mail.
when i click on any of the link the page is postbacking and video will start from the first.
i place update panel for link button even though it is not working (video is playing from the first i.e., page is postbacking. can u help me. thank you
Actually, the part of page that is within the UpdatePanel does the postback. Make sure you have only those controls(for instance, your links) inside the UpdatePanel.
Alternatively, you can use multiple UpdatePanels; for instance one for your video and one for the links. In this case note that, when one UpdatePanel gets updated other UpdatePanels also gets updated, which you may not want; so all you have to do then is to mark the UpdateMode property to Conditional and call YourDesiredUpdatePanel.Update() method manually - whenever required.
Btw, updating selected portions of the page also reduces the load on the server
Or you may want to look into using client callbacks instead of a postback. But since client callback uses XMLHTTP, which means Microsoft implementation of AJAX, therefore callbacks are just awesome as long as your are working with IE.
You might want to try taking advantage of Page Methods to do the work you need done server side.
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Also, if you want to prevent a control from posting back, you can add return false to the end of your javascript onclick event on the control.
For example, if you had an asp button you were using you could do this:
<asp:Button ID="myButton" runat="server" OnClientClick="DoThingsInJavascript(); return false;" />
Or if you were just using a standard button you could say:
<input type="button" onclick="DoThingsInJavascript(); return false;" />
I've never really liked the update panel and I have sometimes found it's behaviour awful. Have you thought of trying something like a proper ajax call from Javascript

stop setting onclick event for asp:LinkButton if no OnClick event was explicitly defined

How do I setup a default setting so that if I do not set an OnClick (i.e the asp.net OnClick attribute) explicitly for an asp:LinkButton tag, it will not render an onclick(html attribute for javascript) attribute client side? By default, asp.net adds an onclick='doPostBack....' for the LinkButton.
Case for use:
There is a LinkButton tag on the page. For this page, if the user has one friend, I only want to run client side code if the button is clicked and would not for any reason want to make a post back. If the user has more than one friend I would want a click to trigger a postback.
Solutions that include the following are not helpful:
Using any asp.net Ajaxtoolkit
Dynamically switching the control type (i.e. if friends == 1 use a asp:Hyperlink)
-I want to avoid this because it is not scalable. There might be many cases where I want an asp:Link tag to do a postback or to not do a postback depending on the user context or user attributes
Using OnClientClick (I am using jQuery would like to avoid this)
Solution that would be helpful if possible:
If I could see server side at runtime whether an OnClick event was explicitly set on an asp:LinkButton tag, this would solve my problem, too. any ideas?
How about rather than dynamically switching the controls (as you mentioned is a solution you don't want), you could always use an asp:HyperLink and set the NavigateUrl property to redirect your page back to itself with a query string of some sort indicating what was clicked.
If you don't want the post to happen at all, simply leave the NavigateUrl property blank.
Of course, this will be pretty worthless if the rest of the page is dependent on ViewState and such.
http://forums.asp.net/t/1129106.aspx
This link explains how to see server side at runtime whether an OnClick event was explicitly set using reflection

calling javascript function on selected index change of select input type in ASP.NET

Is there a way to call a javascript function when the selectedIndex of a select input has been changed?
Using this code in the c# code behind file
riskSeverityDropDown.SelectedValue = Convert.ToString(planRisk.Severity);
riskSeverityDropDown.SelectedIndexChanged += new EventHandler(riskSeverityDropDown_SelectedIndexChanged);
riskSeverityDropDown.AutoPostBack = true;
want to change something else on the page with javascript when the index is changed.
Any help would be much appreciated.
Thanks in advance
You're already going to server code (AutoPostBack is true), which means you're going to rebuild the entire page anyway. Running javascript at this point would be a little silly, because any changes you make to the DOM will be lost and any ajax requests you want to send can instead be handled by normal server code. If you really want to do this, you can just register the script to run when the page loads after the postback.
On the other hand, if you can do this without any server code at all then set AutoPostBack to false and the basic html select control has a nice onchange event you can handle.
Simply add an onchange to your asp:dropdownlist.
<asp:DropDownList ID="riskSeverityDropdown"
AutoPostBack="True"
SelectedIndexChanged="riskSeverityDropDown_SelectedIndexChanged"
onChange="functionName()"
runat="server" />
or to do this from codebehind use:
riskSeverityDropdown.Attributes["onchange"] = "functionName()";
You want to add an attribute to that drop down called "onchange" and set it to call a JavaScript method of your choosing.
Use JQuery. It exposes a rich set of events that you can use to code against practically anything in a uniform manner. It is also quite easy to learn, compact, and popular.
In short you can't go wrong if you use JQuery. It is meant to solve problems like this well.

__doPostBack is not working in firefox

The __doPostBack is not working in firefox 3 (have not checked 2). Everything is working great in IE 6&7 and it even works in Chrome??
It's a simple asp:LinkButton with an OnClick event
<asp:LinkButton ID="DeleteAllPicturesLinkButton" Enabled="False" OnClientClick="javascript:return confirm('Are you sure you want to delete all pictures? \n This action cannot be undone.');" OnClick="DeletePictureLinkButton_Click" CommandName="DeleteAll" CssClass="button" runat="server">
The javascript confirm is firing so I know the javascript is working, it's specirically the __doPostBack event. There is a lot more going on on the page, just didn't know if it's work it to post the entire page.
I enable the control on the page load event.
Any ideas?
I hope this is the correct way to do this, but I found the answer. I figured I'd put it up here rather then in a stackoverflow "answer"
Seems it had something to do with nesting ajax toolkit UpdatePanel. When I removed the top level panel it was fixed.
Hope this helps if anyone else has the same problem. I still don't know what specifically was causing the problem, but that was the solution for me.
Check your User Agent string. This same thing happened to me one time and I realized it was because I was testing out some pages as "googlebot". The JavaScript that is generated depends on knowing what the user agent is.
From http://support.mozilla.com/tiki-view_forum_thread.php?locale=tr&comments_parentId=160492&forumId=1:
To reset your user agent string type about:config into the location bar and press enter. This brings up a list of preferences. Enter general.useragent into the filter box, this should show a few preferences (probably 4 of them). If any have the status user set, right-click on the preference and choose Reset
I had this same problem (__doPostBack not working) in Firefox- caused a solid hour of wasted time. The problem turned out to be the HTML. If you use HTML like this:
<input type="button" id="yourButton" onclick="doSomethingThenPostBack();" value="Post" />
Where "doSomethingThenPostBack" is just a JavaScript method that calls __doPostBack, the form will not post in Firefox. It will PostBack in IE and Chrome. To solve the problem, make sure your HTML is:
<input type="submit" id="yourButton" ...
The key is the type attribute. It must be "submit" in Firefox for __doPostBack to work. Other browsers don't seem to care. Hope this helps anyone else who hits this problem.
this might seem elemental, but did you verify that your firefox settings aren't set to interfere with the postback? Sometimes I encounter similar problems due to a odd browser configuration I had from a debugging session.
Is it because you are doing return confirm? seems like the return statement should prevent the rest of the code from firing. i would think an if statement would work
if (!confirm(...)) { return false; } _doPostBack(...);
Can you post all the js code in the OnClick of the link?
EDIT: aha, forgot that link button emits code like this
<a href="javascript:__doPostBack()" onclick="return confirm()" />
Are you handling the PageLoad event? If so, try the following
if (!isPostBack)
{
//do something
}
else if (Request.Form["__EVENTTARGET"].ToLower().IndexOf("myevent") >= 0)
{
//call appropriate function.
}
Check if you are getting a call this way, if so then maybe the event is not wired and nedes to be explicitly called.
what do you expect from "Enabled = 'false'" ?
I have had problems with firebug on some web forms, something to do with the network analyser can screw with postbacks.
With or without the OnClientClick event it still doesn't work.
The _doPostBack event is the auto generated javascript that .NET produces.
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
*The &95; are underscores, seems to be a problem with the stackoverflow code block format.
Now that i think about it, as noted in my last edit, you want to drop the javascript: in the on client click property. It's not needed, because the onclick event is javascript as it is. try that, see if that works.
Seems it had something to do with nesting ajax toolkit UpdatePanel. When I removed the top level panel it was fixed.
Hope this helps if anyone else has the same problem.
I had this exact same issue in a web app I was working on, and I tried solving it for hours.
Eventually, I did a NEW webform, dropped a linkbutton in it, and it worked perfectly!
I then noticed the following issue:
...
I switch the order to the following, and it immediately was fixed:
...
IE had no issue either way (that I noticed anyway).
I had a similar issue. It turned out that Akamai was modifying the user-agent string because an setting was being applied that was not needed.
This meant that some .NET controls did not render __doPostBack code properly. This issue has been blogged here.
#Terrapin: you got this exactly right (for me, anyways).
I was running User Agent Switcher and had inadvertently left the Googlebot 2.1 agent selected.
In Reporting Services 2008 it was causing the iframes that reports are actually rendered in to be about 300x200 px, and in Reporting Services 2008 R2 is was throwing "__doPostBack undefined" errors in the Error Console.
Switching back to the Default User Agent fixed all my issues.
I had the same problem with Firefox. Instead of using __doPostBack() could you use the jQuery .trigger() method to trigger a click action on an element that has your postback event registered as the click action?
For example, if you had this element in your aspx page:
<asp:Button runat="server" ID="btnMyPostback" OnClick="btnMyPostback_Click" CssClass="hide" ToolTip="Click here to submit this transaction." />
And your postback event in your controller:
protected void btnMyPostback_Click(object sender, EventArgs e)
{
//do my postback stuff
}
You could do the postback by calling:
$("#btnMyPostback").trigger("click");
This will cause the Page_Load event to fire if you need to do something on Page_Load.

Categories

Resources