Calling javascript function in asp.net - asp.net

Ok so this is a snippet of my aspx file, I am trying to call a "hello world" function from the 'test.js' file. This is obviously not working, could someone please recommend me a good way of doing it?
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="\App_JavaScript\test.js" />
</Scripts>
</asp:ScriptManager>
<asp:Button ID="timebutton" runat="server" Text="clickme" OnClick="javascript:getData();" /><br />

OnClientClick
<asp:Button ID="timebutton" runat="server" Text="clickme" OnClientClick="javascript:getData();" />

Use jQuery to assign the function to the click event of the button.
<script type="text/javascript" src="\App_JavaScript\test.js"></script>
<script type="text/javascript" src="jQuery.js"></script>
<script>
$(document).ready(function(){
$('#timeButton').click(getData);
//Use below instead if you are using Master Pages
//$('#<%= timeButton.ClientID %>').click(getData);
});
</script>
<asp:Button ID="timebutton" runat="server" Text="clickme" />

If you need a postback to occur you can also use ScriptManager.RegisterClientScriptBlock

Related

update label inside updatepanel in asp.net

i need to be able to update the label text whenever i want without clicking on a button or something similar to that. the update happens based on server-client logic. i tried to use java script, but when i call the script from a function i wrote it doesn't work. now i'm trying to use updatepanel and no luck so far. i tried to use
UpdatePanel1.Update();
but it didn't refresh the label. the label defined inside the updatepanel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label3" runat="server" Text="Label" AccessKey="l"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
is there a away to force it to refresh?
If you just want to update your label from javascript you can try this function:
<script type="text/javascript">
function updateLabel(newValue) {
$('#<%= Label3.ClientID %>').val(newValue);
}
</script>

modalpopupextender cancel button working as if it is doing postback

I have implemented my owm functionality for the cancel button(I'm using javascript for that) of the ModalPopUpExtender instead of writing "cancelcontrolid" property for the ModalPopUpExtender.
It is working fine but there is one issue with that this cancel button is behaving as if it is doing postback.
It is necessary to have this functionality because when i'm writing the "cancelcontrolid" property it is not resetting the ModalPopUpExtender with the default values.
Please help..
Since you're using the button to only execute client side code why are you using <asp:Button you should use simple HTML<input type="button" instead:
<form id="form1" runat="server">
<asp:Button ID="btnShow" runat="server" Text="Show" />
<asp:ToolkitScriptManager ID="scripManager" runat="server" />
<asp:ModalPopupExtender ID="modal" BackgroundCssClass="modalStyle" PopupControlID="popup"
TargetControlID="btnShow" runat="server" BehaviorID="modalBehavior" />
<asp:Panel runat="server" ID="popup" CssClass="panelStyle">
<input type="button" id="btnCancel" onclick="Hide()" value="Cancel" />
</asp:Panel>
</form>
<script type="text/javascript">
function Hide() {
$find("modalBehavior").hide();
}
</script>
Try this......
<asp:button runat="server".... OnClientClick="myfunction(); return false;" />

why ajax loader image always shown using jquery and asp.net

with asp.net code below my ajax_loader image doesn't work well and always shown ..
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblname">
Name</asp:Label><br />
<asp:TextBox runat="server" ID="txbname"></asp:TextBox><br />
<asp:Label ID="lblemail" runat="server">
Email</asp:Label><br />
<asp:TextBox runat="server" ID="txbemail" /><br />
<asp:Label runat="server" ID="lblsugg">
Suggestion</asp:Label><br />
<asp:TextBox runat="server" Rows="3" Columns="20" ID="txbsugg" TextMode="MultiLine"></asp:TextBox>
<asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_onclick" Text="submit" />
<asp:Label runat="server" ID="lblresultmsg"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<div id="loading">
<p>
<img src="Images/ajax-loader.gif" />
Please Wait</p>
</div>
and jquery code
$("#loading").ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
any suggestions !?
ajaxStart and ajaxStop works only for ajax request sent by jQuery, if you use other libraries or UpdatePanel, it won't help you.
jQuery only.
Whenever an Ajax request is about to be sent {With jQuery-gdoron}, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.
Example of jQuery ajax request that will fire the ajaxStart and ajaxStop:
$.ajax({
url: 'foo',
...
...
});
You could create a generic way to handle this by adding the following code to a common js include. Here's a quick and dirty example:
Note: Be sure you initialize it by calling SetupGlobalAjaxHandlers on your page load.
function SetupGlobalAjaxHandlers()
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
}
function InitializeRequest(sender, args)
{
if(typeof(OnPageInitRequest) != "undefined")
OnPageInitRequest();
}
function EndRequest(sender, args)
{
if(typeof(OnPageEndRequest) != "undefined")
OnPageEndRequest();
}
Then, in any page that includes the js file, you could optionally implement the OnPageInitiRequest() and OnPageEndRequest() methods. There, you could show/hide your loading indicator.
I do recommend, though, that you use an UpdateProgress control as you get the show/hide logic for free.
This technique opens up some possibilities for you, say, if you wanted to disable controls while a partial postback is occurring.

Timer issue on Asp.Net

I have encountered a serious issue in asp.net which is stopping me going further.
The issue is due to server side timer control. The following is the scenario,
When main page loads , timer will start automatically and it should keep run throughout the application. My page contains Infragistics controls.
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" onload="UpdatePanel1_Load">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Enabled="true" Interval="10000" />
</ContentTemplate>
</asp:UpdatePanel>
<ig:WebDataMenu ID="WebDataMenu1" runat="server" OnItemClick="wdmenu_Context_ItemClick">
<Items>
<ig:DataMenuItem Key="0" Text="Add" >
</ig:DataMenuItem>
<ig:DataMenuItem Key="1" Text="Edit">
</ig:DataMenuItem>
</Items>
<ClientEvents ItemClick="wdmenu_Context_ItemClick" />
</ig:WebDataMenu>
</div>
</form>
</body>
If i open model dialog and after 2 to 3 sec , i am closing the model dialog... the wdmenu_Context_ItemClick server side event (which is for opening model dialog ) is firing two times...
Also on click of that button , i am opening model dialog using java script which is shown below
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function wdmenu_Context_ItemClick(sender, eventArgs)
{
var win = window.showModalDialog("Default2.aspx", "Dialog", 'center:yes;resizable:no;dialogHeight:500px;dialogwidth:660px');
}
</script>
</head>
Please help me regarding this....

ASP.net, Ajax, JavaScript help

I need help in how to set webform control proprieties in asp.net and ajax control using javascript.
I have asp page that has checkBox, TextBox, MaskedEditExtender, and RegularExpressionValidator.
I set the mask for MaskedEditExtender as Mask="(999)999-9999" and I set the ValidationExpression for RegularExpressionValidator as ValidationExpression="\d{10}".
I want to change these two properties when user checked the international checkbox to: Mask="999999999999" and as ValidationExpression="\d{12}"
Using JavaScript without interrupting with server side and when the user unchecked they get previous value so the interaction should be only in the client side.
Please help me with this and here is my code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function pageLoad() {
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:CheckBox ID="chkIntphoneHome" runat="server" Text="Internation Code"
AutoPostBack="false"/>
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
<cc1:MaskedEditExtender ID="txtHomePhone_MaskedEditExtender" runat="server"
AutoComplete="False" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat=""
CultureDatePlaceholder="" CultureDecimalPlaceholder=""
CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
Mask="(999)999-9999" TargetControlID="txtHomePhone">
</cc1:MaskedEditExtender>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="RegularExpressionValidator" ControlToValidate="txtHomePhone"
ValidationExpression="\d{10}"></asp:RegularExpressionValidator>
</div>
</form>
</body>
</html>
Instead of meddling with the maskEditor/validator controls' client-side behaviour, are you OK with having 2 sets of textbox+maskExtender+validator?
Set 1 = texbox_HomePhone + maskEditExtender_HomePhone + regexValidator_HomePhone
Set 2 = texbox_IntHomePhone + maskEditExtender_IntHomePhone + regexValidator_IntHomePhone
Using the checkbox to toggle (via javascript) the display/hidding of either set.

Resources