Controls in ContentPlaceHolder are not recognized by javascript document.getelementsbyname - javascript

I have a calender script in my project. For using it, I have added an image and on its onclick event I call a javascript function displayDatePicker:
<img alt="Calender" src="calender.jpg" onclick="displayDatePicker('txtBox1', this);"/>
I use a masterpage in my project and if the image and the associated textbox control are in contentPlaceHolder, the javasript library's function document.getElementsByName returns null and it seems that it can not detect controls inside a contentPlaceHolder.
I used this code for a page that was not using a masterpage and everything went well.
I appreciate any help from you in advance.
Good luck.

Your asp textbox may render as something like:
<input name="ctl00$FeaturedContent$txtBox1" id="FeaturedContent_txtBox1" type="text"/>
So, in javascript, you should use clientid:
<img alt="Calender" src="calender.jpg" onclick="displayDatePicker('<%= txtBox1.ClientID %>', this);"/>

Related

Rerender full page using ajax

I am more backednd developer and sometimes ui things confuding me.
I have html form
<form action="getNewPage">
...
<input type="submit'>
</form>
getNewPage returns html page
when I click on submit
Then page reloads and html returned from getNewPage renders on this page.
How can I achieve it using ajax?
Is it possible?
P.S.
I just want to know
You can use the jQuery (the JavaScript framework) for it.
Example:
// #theForm - form id
$.get('server.php?' + $('#theForm').serialize());
$.post('server.php', $('#theForm').serialize());

Unable to call a function while using master page in Javascript at checkbox checked change event

I was trying to call a function when the checkbox status changes, it calls without a masterpage. I have tried the following code:
<script type="text/javascript">
$(function () {
$('#cbOption1').on('change', PublishToPreferredZerker);
});
function PublishToPreferredZerker() {}
</script>
[...]
<asp:CheckBox ID="cbOption1" runat="server" style="text-align: left"
Text="Publish the job to particular Zerker or a group of the Zerkers." /><br />
The function is not called when using MasterPage.
Note cbOption1 is not the client ID, but the ID for the server side.
You need to do something like (Use Control.ClientID Property to get the id for HTML element):
$('#<%=cbOption1.ClientID%>').on('change', PublishToPreferredZerker);
The code
$('#cbOption1').on('change', PublishToPreferredZerker);
finds the control with id cbOption1 and then acts on that control
But when using this code on a master page, the control id does not remain cbOption1.
It is prefixed by master page content Id.
something like ct$001cbOption1
To make it work when using master pages use code like this to find the clientId for the control :
$(#"<%= cbOption1.ClientID %>").on( .... )
I got success while I added below code on Page load function.
cbOption1.Attributes.Add("onChange", "javascript:PublishToPreferredZerker()");
Javascript function
function PublishToPreferredZerker() {}
Also I have tried above answers but not get required output.
Thanks,

Can i use JS to replace a GET-parameter with a Post in a link?

I'm working with an old ASP WebForms page in which there is a link which opens in a new windo using javascript. This link includes a GET-parameter, like this:
<href="javascript:window.open(http://myurl.com?MyId=123).focus()">
Search for object
</a>
What I would like to do is replace this GET-parameter with a Post-variable in order to avoid the value of MyId being stored in the browser-history. Possibly something like:
<input type="hidden" id="MyId" name="MyId" value="123">
<a href="submitSearchCriteria()">
Search for object
</a>
Note: Since this is a webforms page, the whole contents of the page is within a pair of <form>...</form> tags which post back to the page itself, and I don`t want to mess with these. The page I would like to link to is another page.
My question: Is there some fairly simple, clean and safe way to pass along a Post-variable from within a link like this? I would prefer to do this without including any third-party java script libraries if possible (I want to minimize the necessary changes to an aging system).
Any ideas?
Add your MyId in your form:
<input type="hidden" id="MyId" name="MyId" value="123">
Then your hyperlink:
Search for object
Then use javascript to change your form action:
function submit(){
[Your Form].action = "[Your Action]";
[Your Form].submit();
}
The form submits but as the page refreshes, the form action goes back to what it was before. But this could depend to where you point back your new action. You could add something to handle the response. This is the easiest solution if you ask me. But for the cleanest, you should try reading about AJAX.

How do you output raw javascript in asp.net

I would like to output raw javascript to an aspx response. Using response.write fails because along with the javascript, it dumps all of the other asp.net stuff in the page, for example:
...
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTAxODk4MjA3OWRk9jVlI2KaQPiVjEC+P0OPYGV74vKjJQZuwD6OaHhb+U0=" />
...
Is there anyway to simply output raw text to the output without having all of the other asp.net stuff on the page? I am trying to use the page as follows:
<script src="mypage.aspx"></script>
Where this page contains the javascript this triggers off the aspx page, however chrome doesn't like it for obvious reasons... it expects javascript, but it is getting javascript + markup + .NET injected stuff.
Please no comments on why I am doing this! Working on very specific problem.
Use .ashx Generic Handler. Then you can set context.Response.ContentType = "text/javascript" and just use Response.Write
I'm not sure that I understand your question, but you can try something like this:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "registerSomething", "doSomething = function(){ ... }", true);
You could also disable the viewstate, etc... and remove everything from the aspx file.
<%#Page ... EnableViewState="false" EnableViewStateMac="false"%>
Include nothing else in the aspx file, no runat=server anything. Then write your javascript in the codebehind using response.write.
This will be pretty tough using WebForms. The best way I can think of would be to add MVC to your project so you can have more control over the output. (Yes, MVC and WebForms can coexist in the same project).
This way you can still leverage the .aspx syntax (or the even-cooler Razor syntax) to produce a javascript file, without WebForms adding all of its annoying stuff to your output.
I needed this and this was my solution:
Created a file script.aspx and the content was straight javascript with the header directive of the page. Then in the javascript I was able to insert calls to static methods:
<%# Page Language="C#" ContentType="text/javascript" %>
function someFunction()
{
var someVar;
someVar="<%= MyClass.MyMethod() %>";
}
That returned just pure javascript with the server side insertions as needed:
function someFunction()
{
var someVar;
someVar="Some string returned by my method";
}

Javascript calling JSF handler method

I am reading an xml file using javascript and then I need to submit my form so that it calls a particular method in my JSF handler. Usually this can be done on a jsp when user clicks a button by having an actionlistener like so:
<h:commandLink styleClass="button" action="#{myHandler.outcome}" actionListener="#{myHandler.doNext}">
<span><h:outputText value="#{text.button_submit}" /></span> </h:commandLink>
I am not sure how to call a method like 'doNext' above in the handler from javascript. I cannot do a simple:
document.form.submit();
as it then repeats the processing i have already done. I want to read values from an xml file and then call a particular method in handler. Any ideas much appreciated.
I found a solution to this. I added a hidden button to my jsp like:
<p id="hiddenButton" style="display:none;" >
<h:commandLink id="hiddenRegButton" styleClass="button" action="#{myHandler.doNext}" />
</p>
and in my javascript I used jQuery to execute the click which leads t the "doNext" method in my Handler to get executed.
jQuery('#hiddenRegButton').click();
this was easier than I thought.

Categories

Resources