I have a problem in my C# ASP.NET application, where the id and name tags are modified at runtime with a prefix "MainView_" and "MainView$" respectively. So my code:
<asp:Button ID="OKToContinueCheckInButton" runat="server" CausesValidation="False" Visibility="false" Style="display: none" OnClick="btnOKToContinueCheckIn" />
<script type="text/javascript">
<!--
var answer = confirm("Some Warning");
if (answer)
document.getElementById('OKToContinueCheckInButton').click();
// -->
</script>
becomes:
<input type="submit" name="MainView$OKToContinueCheckInButton" value="" id="MainView_OKToContinueCheckInButton" Visibility="false" style="display: none" />
<script type="text/javascript">
<!--
var answer = confirm("Some Warning");
if (answer)
document.getElementById('OKToContinueCheckInButton').click();
// -->
</script>
getElementID() returns null because the name has changed. Can anyone tell me why this occurs and if there is a way to possibly disable it from changing the id and name values. Thanks!
-Sephrial
I think this would help you...
<script type="text/javascript">
<!--
var answer = confirm("Some Warning");
if (answer)
document.getElementById('<%= OKToContinueCheckInButton.ClientID %>').click();
// -->
</script>
This is the part that does the trick
<%= OKToContinueCheckInButton.ClientID %>
This is a fundamental part of the way the Asp .Net Forms model work.
The IDs are changed so that you don't have to worry about keeping IDs unique across user controls, custom controls, repeaters etc.
You can use Cyril's method. I also find JQuery suits this model very well to because you can easily reference controls by their class or position in the document.
The good news is ASP.Net 4.0 will solve this issue.
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
I think you forgot the single quotes:
document.getElementById(<%= OKToContinueCheckInButton.ClientID %>).click();
Should be:
document.getElementById('<%= OKToContinueCheckInButton.ClientID %>').click();
<asp:Button ID="OKToContinueCheckInButton" runat="server" CausesValidation="False" Visibility="false" Style="display: none" OnClick="btnOKToContinueCheckIn" />
<script type="text/javascript">
<!--
var answer = confirm("Some Warning");
if (answer)
document.getElementById(<%=OKToContinueCheckInButton.ClientID %>).click();
// -->
</script>
Write your script from the code behind. Register it as a start up script, and use the client ID of the object.
System.Text.StringBuilder script = new System.Text.StringBuilder();
script.Append("var answer = confirm('Some Warning');");
script.Append("if (answer)");
// The client ID will be what is put in the browser so it will find it.
script.AppendFormat("document.getElementById('{0}').click();", this.btnSomeButton.ClientID);
// Hook this up to the button
this.btnSomeButton.OnClientClick = script.ToString();
You must use the ClientID Property like this:
<asp:Button ID="OKToContinueCheckInButton" runat="server" CausesValidation="False" Visibility="false" Style="display: none" OnClick="btnOKToContinueCheckIn" />
<script type="text/javascript">
<!--
var answer = confirm("Some Warning");
if (answer)
document.getElementById(<%=OKToContinueCheckInButton.ClientID%>).click();
// -->
</script>
If you are willing to use jQuery, this is not a problem at all, and you wont have to worry about changing names. By using the $= (ends with) selector, this will be noe problem:
if (answer)
$("a[id$='MainView_OKToContinueCheckInButton']:first").click();
jQuery selectors are very easy to work with
Thanks for all of your responses, I think I understand why the values are replaced. However, when I attempted the solutions above, it gives an error because it produces the following HTML with a '_' instead of a '$'. I was using, document.getElementById(<%= OKToContinueCheckInButton.ClientID %>).click();
<input type="submit" name="MainView$OKToContinueCheckInButton" value="" id="MainView_OKToContinueCheckInButton" Visibility="false" style="display: none" />
<script type="text/javascript">
<!--
var answer = confirm("Some Warning");
if (answer)
document.getElementById(MainView_OKToContinueCheckInButton).click();
// -->
</script>
Related
My front code for checkbox:
<asp:CheckBox ID="chkActive" ng-model="chkActive" ng-init="chkActive='true'" runat="server" Text="Active" Checked="true" />
My AngularJS Controller Code:
var _master = {
UnitCode: $scope.unitCode,
UnitName: $scope.unitName,
chkActive: $scope.chkActive
};
I need to get checkbox value in above chkActive variable, bu I can get.
Help me to solve this problem in such easy ways.
why don't you use ng-checked to set checkbox checked? And call ngClick function to set the checkbox value to _master variable
<asp:CheckBox ID="chkActive" ng-model="chkActive" ng-init="chkActive='true'" runat="server" Text="Active" ng-checked="chkActive" ng-click="clCheck()" />
$scope.clCheck = function(){
var _master.chkActive = $scope.chkActive
}
You can make use of ng-checked to achieve that.
Like this example:
var myApp=angular.module('myApp', []);
myApp.controller('Padre1Controller', function($scope){
$scope.myvalue = true
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="Padre1Controller">
ng-checked :<input type="checkbox" ng-checked="myvalue"><br/>
<pre>{{myvalue}}</pre>
</div>
</div>
I don't see any date picker in ASP.NET controls.
Anyone knows how to add it in my my webpage.
I am using VB as my programming language.
Result
I would like to thank everyone who posted the solutions, code and links which helped me to achieve this thread goal. Based on all of the source the below code is the one which works nicely.
Content1
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.min.js"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
//initialise the datepicker with the date format specified
$(".datepicker").datepicker();
});
</script>
And in
Content2
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:TextBox runat="server" ID="txt_MyDate" CssClass="datepicker" MaxLength="10" />
Add this inside your [head] Tag.
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
and use this jquery function in aspx page inside [script] tag
$(function () {
$("#datepicker").datepicker();
$("#datepicker").change(function () {
$('#btnRefresh').click();
});
and add one textbox for display selected date. and button for getting the date in sever side. like this code
<input type="text" id="datepicker" runat="server" clientidmode="Static" />
<asp:Button ID="btnRefresh" runat="server" onclick="btnRefresh_Click" ClientIDMode="Static"
Text="Refresh" AutoPostback="true" style="display:none;"/>
use this onclick event to do want you want..
You're right, there isn't anything built-in.
Personally I would recommend the jQueryUI datepicker - it's reliable and flexible, and it's javascript based, so it doesn't matter what your server-side language is.
See https://jqueryui.com/datepicker/
Once you've added the required references to jQuery and jQueryUI, in your aspx page, add a script section like this:
<script type="text/javascript" language="javascript">
$(function () {
//initialise the datepicker with the date format specified
$(".datepicker").datepicker();
});
</script>
and then add the "datepicker" class to any textbox controls on the page where you want to use the datepicker:
<asp:TextBox runat="server" ID="txt_MyDate" CssClass="datepicker" MaxLength="10" />
P.S. I can also recommend most of the rest of the jQueryUI package, there are some useful items in there which can speed up your development time and make your apps look nicer and be more usable.
You can implement a datepicker on your page with the help of the Calendar control, using javascript on your page. Try this links for simple methods to do it:
http://www.vbknowledgebase.com/?Id=150&Desc=ASP.Net-DatePicker-using-Calendar-Control
http://www.codedigest.com/Articles/ASPNET/247_DatePicker_Control_in_ASPNet.aspx
http://www.aspsnippets.com/Articles/DateTimePicker-control-for-ASPNet-TextBox-Example.aspx
I'm wanting to change the value of my inputs with my asp form fields so that when clicked it clears the content and if someone doesn't type any value and clicks another field it will re-add the default value again, if changed it leaves the new text.
eg. field:
<asp:TextBox ID="NameInfo" CssClass="NameInfo" Width="300" runat="server" text="Name" />
Thanks in advance for any responses. I'm a beginner at asp so I'm not sure how to do these types of features.
I believe the functionality you need would be best achieved using jQuery and a plugin. Perhaps look into http://code.google.com/p/jquery-watermark which from experience works well.
You should just need to download that library and then have something like the following within the head tags of your page:
<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript" src="/path/to/watermarkplugin.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#<%=NameInfo.ClientID").watermark('The text you want to appear');
});
</script>
Found a simple alternative:
<asp:TextBox ID="NameInfo" CssClass="NameInfo" onfocus="if(this.value == 'Name'){this.value='';}" onblur="if(this.value == ''){this.value='Name';}" Width="300" runat="server" text="Name" />
I need a calendar to be displayed on a textbox.
This can be achieved using ajaxToolkit: Calendar control.
I can use FilteredTextBoxExtender to ensure that it enters only valid characters.
Also I can use jQuery to ensure that the dates are valid dates only (13/13/20123 is an invalid date).
But is there a better way or ajax control (in the toolkit) for it?
Note: I am looking for a solution using ajax control toolkit. For my project, jQuery-UI is not allowed.
<ajaxToolkit:Calendar runat="server"
TargetControlID="Date1"
CssClass="ClassName"
Format="MMMM d, yyyy"
PopupButtonID="Image1" />
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="txtDate"
FilterType="Custom, Numbers"
ValidChars="1234567890/" />
REFERENCE:
http://forums.asp.net/p/1825929/5076051.aspx/1?Re+How+to+disable+future+dates+in+ajax+calendar+extender+
Try Using jQuery UI Datepicker:
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<div class="demo">
<p>Date: <input id="datepicker" type="text"></p>
</div>
jQuery UI Datepicker
I am trying to implement JQuery in my web page but i am not been able to implement it successfully.
I have a master page
where i added one script for menu bar that is already using jquery hosted by Google
This is coded in master page itself
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
Now i want to implement a Jquery to set the css visibility property to true or false.
into my content page of same master page.
<script type="text/javascript">
$(document).ready(function(){
$("#lnkAddMore").click(function(){
alert();
}
);
});
</script>
This html control is under my UpdatePanel.
I dont know why it is not working ?
I am using this control under UpdatePanel.
<input type="button" id="lnkAddMore" value="Add More" />
I tried to use it outside my update
panel it is running successfully but
not in UpdatePanel
I think there is a problem using it with an UpdatePanel
HERE IS MY PAGE SOURCE
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<link href="../../Css/Domain.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
$("#lnkAddMore").click(function(){alert();$('#h').hide(100);});
$('#h').show(100);
});
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="divDomain" runat="server">
<asp:gridview/>
<div style="text-align:right;">
<input type="button" id="lnkAddMore" value="Add More" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Do you really have nested <script> tags or was it a typo?
<script type="text/javascript">
<script type="text/javascript">
If that's not a typo, then that's probably the issue.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#ddmenu > li').bind('mouseover', ddmenu_open)
$('#ddmenu > li').bind('mouseout', ddmenu_timer)
});
$(document).bind('click', ddmenu_close); // do you really want to close on any click?
</script>
I have used two approaches with jQuery and UpdatePanels.
The first is this:
jQuery $(document).ready and UpdatePanels?
The other approach I found on Rick Strahl's blog (http://www.west-wind.com/Weblog/posts/154797.aspx) Which is to do something like this in the code behind:
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alertScript",
#" $(document).ready(function(){
$("#lnkAddMore").click(function(){
alert();
}
);
});", true);
Check this out for another example: Problem with ScriptManager.RegisterClientScriptBlock and jQuery in Internet Explorer 8
Also have a look at jQuery's live() event subscriber.
Check that the ID of your button isn't actually getting changed to something like
ctl00_ContentPlaceHolder1_lnkAddMore
since it's residing in a content placeholder for the master page.
If it is, you'd have to adjust your JQuery.
When you're loading code in under an UpdatePanel, try not wrapping it in $(document).ready();. Ready isn't fired on document for AJAX events.