How to pass a variable from html to aspx? - html

I want to pass the variable from html page to a hidden field in aspx page, I have a aspx form with code
<div id="divFrameHolder" style="display: none">
<iframe src="inner/RegSelect.html"name="myIframe" id="myIframe" width="100%" height="200px" frameborder="0" onload="hideLoading()">
<asp:HiddenField ID = "hidField" runat="server" value = " " />
</iframe>
</div>
and RegSelect.html has this code
<div id="tabs">
<ul>
<li style="font-family:'b nazanin'; font-weight:bold"><a onclick="use();" href="site"><i class="fa fa-picture-o fa-5x"></i><br />سایت</a></li>
<li style="font-family: 'b nazanin'; font-weight: bold"><a onclick="use();" href="stor"><i class="fa fa-shopping-cart fa-5x"></i><br />فروشگاه</a></li>
<li style="font-family: 'b nazanin'; font-weight: bold"><a onclick="use();" href="blog"><i class="fa fa-comments fa-5x"></i><br />وبلاگ</a></li>
</ul>
<section class="tabs-content" style="width:100%">
<div id="site" style="width:100%" >
</div>
<div id="stor">
</div>
<div id="blog">
</div>
</section>
</div>
<script>
$('#tabs a').click(function (e) {
var f = $(this).attr('href');
alert(f);
window.opener.location.href = "RegForm.aspx?" + "val=" + f
//var c = $('#myIframe').contents().find('#HF1').val();
//alert(c);
//alert($('#myIframe #HF1').val());
//$(' #HF1').val(f);
});
</script>
Now, I want get var f in aspx page? Can I get the variable from html page into aspx page?

You can use Request.QueryString to get the value of an URL parameter
Retrieve GET variables from URL in ASPX

Seems like you want to pass a variable from the html page in the IFrame to a hidden field in the page containing the IFrame.
Accessing IFrame content using jQuery as explained here: how to access iFrame parent page using jquery?
Note: ASP.NET WebForms has a setting that determines how ID's are generated for a control. See Here Make sure your hidden field's id mode is static.
Also understand that ASP.NET generates ID's to prevent collisions. Use an ID for your hidden field that you know will be unique on the page.
<asp:HiddenField ID="hidField" ClientIDMode="Static" runat="server" value=" " />
Use this to access the hidden field in the aspx page:
$('#tabs a').click(function (e) {
var f = $(this).attr('href');
alert(f);
window.opener.location.href = "RegForm.aspx?" + "val=" + f
$('#hidField', window.parent.document).val(f);
//var c = $('#myIframe').contents().find('#HF1').val();
//alert(c);
//alert($('#myIframe #HF1').val());
//$(' #HF1').val(f);
});
Note that hidField needs to be a dom element in the containing aspx page.
ASPX
<div id="divFrameHolder" style="display: none">
<iframe src="inner/RegSelect.html"name="myIframe" id="myIframe" width="100%" height="200px" frameborder="0" onload="hideLoading()">
</iframe>
<input type="hidden" id="hidField" />
</div>

Related

CSS how to use onclick and this to bind to a different element

Ok, I'll try to explain as best I can.
Here is my code:
HTML:
<div class="mobile-wrapper">
<div id="mobile" class="mobile">
<img src="assets/images/mobile-moments-away-img.png">
<img id="mobile-close-btn" class="mobile-close-btn" src="assets/images/mobile-close-button-img.png" onclick="handleClose(this)">
<img id="mobile-continue-btn" class="mobile-continue-btn" src="assets/images/mobile-continue-now-img.png">
</div>
</div>
JS:
const handleClose = (elem) => {
console.log(elem);
elem.style.display = "none";
};
So, currently the handclose function currently hides the element, in this case it closes the image that has onclick="handleClose(this). What I need, is that when I click that image, it closes the mobile class instead of closing that image. So I am unsure how to reference the mobile class whilst still using this.
I have to use this as I have been asked too. Because I have the same HTML as above but for different images, as the JavaScript functions will doing the same thing for the above HTML as well as the different images I have. For example: My handleclose function will be using different elements but doing the same thing, hence why I am using this. So I would appreciate some help with this, and not to be told to do it a different way ie getelementbyid as that's not what I need.
Also, how would I be able to use elem.parentElement in this function, where popUp should be elem.parentElement
document.addEventListener("visibilitychange", () => {
if (!cookieExists()) {
popUp.style.visibility = "visible";
// popUp.style.top = "10vh";
document.cookie = `${cookieName}=true;max-age=604800;SameSite=None; Secure`;
}
});
You can refer to the parent element using .parentElement:
const handleClose = (elem) => {
elem.parentElement.style.display = "none";
};
<div class="mobile-wrapper">
<div id="mobile" class="mobile">
<img src="assets/images/mobile-moments-away-img.png">
<img id="mobile-close-btn" class="mobile-close-btn" src="assets/images/mobile-close-button-img.png" onclick="handleClose(this)">
<img id="mobile-continue-btn" class="mobile-continue-btn" src="assets/images/mobile-continue-now-img.png">
</div>
<div id="mobile" class="mobile">
<img src="assets/images/mobile-moments-away-img.png">
<img id="mobile-close-btn" class="mobile-close-btn" src="assets/images/mobile-close-button-img.png" onclick="handleClose(this)">
<img id="mobile-continue-btn" class="mobile-continue-btn" src="assets/images/mobile-continue-now-img.png">
</div>
<div id="mobile" class="mobile">
<img src="assets/images/mobile-moments-away-img.png">
<img id="mobile-close-btn" class="mobile-close-btn" src="assets/images/mobile-close-button-img.png" onclick="handleClose(this)">
<img id="mobile-continue-btn" class="mobile-continue-btn" src="assets/images/mobile-continue-now-img.png">
</div>
</div>

Access functions of main page from innerhtml

I have two html pages. In the main page, when a link is clicked, the url behind the link should be opened in a small inline window. After searching a lot I found that I could use Innerhtml to load the url into a small div (I also tested iframe and ajax but they did not work in chrome). 1. Is it possible to call a function defined in main page, inside the innerhtml? 2. Inside the main page and innerhtml page I have the same tabs and objects of same name, and one of the tabs is called map. when a button is clicked in innerhtml, I want a point to be added in the map of main page not the map of innerhtml itself.
Here is a part of the code:
<script>
function load_home(url) {
document.getElementById("content").innerHTML = '<object type="text/html"
data = "' + url + '" > < /object>';
}
</script>
<ul class="list-group gn-resultview">
<li class="list-group-item" data-ng-repeat="md in searchResults.records" gn-displayextent-onhover="" gn-zoomto-onclick gn-fix-mdlinks="">
<div class="media-body">
<div draggable id="content"> </div>
<h4>
<input gn-selection-md type="checkbox" ng-model="md['geonet:info'].selected" ng-change="change()" />
<!-- Icon for types -->
<a ng-href="#/metadata/{{md.getUuid()}}" onclick="load_home(this.href);return false;" title="{{md.title || md.defaultTitle}}">
<i class="fa gn-icon-{{md.type[0]}}" title="{{md.type[0] | translate}}"/>
{{(md.title || md.defaultTitle) | characters:80}}</a>
</h4>
<p class="text-justify" dd-text-collapse dd-text-collapse-max-length="350" dd-text-collapse-text="{{md.abstract || md.defaultAbstract}}"></p>
<blockquote ng-if="md.getContacts().resource">{{::md.getContacts().resource}}</blockquote>
</div>
<div>
<gn-links-btn></gn-links-btn>
</div>
</li>
</ul>
In the innerhtml page you can call a function of main page such as "test()" like this:
parent.test()
So as an example:
In the innerhtml url:
<script>
function test1()
{
parent.test();
}
</script>
in the innehtml page:
<button type="button"
class="btn btn-default btn-sm btn-block"
data-ng-show="hasAction(mainType)"
onclick="test1()" >

Change style of an item inside updatepanel dynamically

I have an image inside an updatepanel. When I click on the image it needs to be highlighted. I use addclass and removeclass to make this work, if it is outside updatepanel. When I put the image inside the updatepanel, the css is applied but then reverts to original after the update completes and the page is rendered.
How can I change the styling of the image inside the updatepanel dynamically?
Markup:
<asp:UpdatePanel ID="UpdatePanel2" runat="server" class="journey-categories">
<ContentTemplate>
<ul class="journey-categories">
<asp:Repeater ID="rptJourneyCategories" runat="server">
<ItemTemplate>
<li>
<a href="javascript:void(0)" data-categoryid="<%# Eval("Id") %>" data-introtitle="<%# Eval("IntroTitle") %>" data-introtext="<%# Eval("IntroText") %>">
<img class="thumb" src="<%# Eval("CategoryIcon") %>" width="70" height="70" alt="" id="imgCategoryIcon" />
</a>
<span><%# Eval("Title") %></span>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ContentTemplate>
</asp:UpdatePanel>
Jquery:
$('[data-categoryid]').off('click.categories').on('click.categories', function () {
$(this).closest('ul').find('li.selected').removeClass('selected');
$(this).parent().addClass('selected');
});
I got this to work with the help of EndRequestHandler
I added the following inside script document ready and it updated the styling after the update panel completed.
<script type="text/javascript">
$(function () {
var onDomReady = function () {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler() {
var type = $('[id$=ddlType]').val();
$('a[data-categoryid="' + type + '"').parent().addClass('selected');
}
}});

How to open new window with Html.BeginForm

Is there any way to open a new window if the Submit Button is inside #using Html.BeginForm? I tried the following but it doesn't work?
#using (Html.BeginForm("SetCol", "Document", FormMethod.Post, new {target="_blank"})){
<div class="modal-body" data-height-relative="20">
.
.
.
</div>
<div class="modal-footer">
<div class="pull-right">
<input type="submit" value="Senden" class="btn btn-primary"/>
<a href="#ViewData["closeUrl"]" class="btn btn-primary" #(ViewData["closeUrl"] == "#" ? "data-dismiss=modal" : "")>#Translations.Close</a>
</div>
</div>
}
The output is a PDF File:
<html>
<body marginwidth="0" marginheight="0" style="background-color: rgb(38,38,38)">
<embed width="100%" height="100%" name="plugin" src="http://localhost:54906/Document/SetCol?id=108" type="application/pdf">
</body>
</html>
Below line works for me with your code. May be something related to your action method.
#using (Html.BeginForm("About", "Home", FormMethod.Post, new { target = "_blank" }))
As you mentioned in your comment, that this method returns a PDF file, you can use window.open like:
window.open("../Document/SetCol", "_blank");
or
window.open("#Url.Action("LoadReport", "ExportReport")" + link, "_blank");
You will have to define a click event handler obviously.

Page won't display after clicking alert box

Creating a game where the user answers 5 questions. I want to display their score on a separate page when then complete the game.
This is how my page is set up:
<div data-role="page" id="displayScore">
<div data-role="header">
<h1>How Did You Go?</h1>
</div>
<div data-role="content">
<p id="displayScore" align="center"></p>
<input type="button" value="Play Again?" onClick="startGame();"/>
<input type="button" value="Main Menu" onClick="" />
</div>
<div data-role="footer">
<h4>AL</h4>
</div>
</div>
</body>
</html>
This is my function:
function score()
{
ans += "You scored <strong> " + score + " </strong> out of <strong> " + count + "!</strong>";
document.getElementById('displayScore').innerHTML = displayScore;
}
if you want to pass value to another page then you have to do one of the method between the following methods
pass the value by query string (using get method)
pass the value by post method
use cookies, localStorage or sessionStorage to store the value and retrieve the value in the another page.
if you want to open up a new window the the code will be like this
window.open('http://www.google.com','GoogleWindow', 800, 600);
if you want to create a new window with dynamic value then
<html>
<body>
<script>
myWindow=window.open('','','width=200,height=100');
var displayScore = "your score";
myWindow.document.write("<p> + displayScore + </p>")
myWindow.focus();
</script>
</body>
</html>
it is the best way to directly open the new window with score.
if you have a result.html page then you can pass the score by query string or you can write the javascript code in the result.html page to access any of the cookies, localStorage or sessionStorage to get the score. but make sure you have saved your score in any of it.