Open a new window with Same session using jquery/C# Razor - c#

I am trying to open a new window with same session as the current one. I wrote some code below but its no luck yet.
For example lets say i have a form with a label (Name), textbox (where the text goes in)
and a button when pressed takes me to a new window.
If i press the button it should open a new window with the same form elements and text in the textbox if it was put in before.
Also please note the new window must be opened as a new tab in the browser with the same state and same elements as in the previous browsers.
Anyone got an idea on this (using razor view engine or jquery/javascript) ? Thanks in advance.
<label for="Name">Name</label>
<input type="textbox" value="nothing" id="text" />
<input type="button" value="press me" id="submitButton" />
$(document).ready(function()
{
$('#submitButton').click(function()
{
var currentUrl=document.URL;
window.open(currentUrl,"newWindow",300);
event.preventDefault();
});
});

You may be able to accomplish this by using modals instead of popup windows. Jquery UI has built in modal support: http://jqueryui.com/dialog/
The advantage to this approach is that no data needs to be passed into a separate page. Your modal code would look something like this, with jQuery Dialog. This would go on the same page as your initial inputs:
<div id="dialog" title="Basic dialog">
<input type="text" id="popupText" name="popupText" />
</div>
and your click event would modified update the modal input and show the modal:
$(document).ready(function(event)
{
$('#submitButton').click(function()
{
$("#popupText").val($("#text").val());
$("#dialog").dialog();
event.preventDefault();
});
});
This could definitely be cleaned up but I believe it'll do what you need pretty simply. The dialog can contain any html elements, so you can add a separate form if necessary.

Related

Show same Popup on two Views

I am building a mvc 5 application.
I have two .cshtml views.
View1 with Layout1 and View2 with Layout2.
On Layout1 under some conditions i am showing a popup like
<input type="button" id="btnpopu" value="Open Modeless popup" onclick="ShowPopup();" />
<script type="text/javascript">
ShowPopup = function () {
window.open('/Home/OpenPopup', "PopupWindow", 'width=400px,height=400px,top=150,left=250');
}
</script>
When user changes to View2 by a button click or something like that, i need the popup window to stay in place.
Means still visible on Layout2
Any suggestions ?
You should create the same popup at layout1 to layout2 onload also and send to it the specific conditions that you want, I think that the only way you can't keep state of view per request only by the same layout.

Making a button element submit

Using ASP.NET, how do I make this button tag submit my ASP.NET form when clicked:
<button>Submit</button>
I'd like it to do a post back just like a regular asp.net server control button would work. I'd prefer a jquery way to do it if possible.
$('button').on('click', function(){
$(this).closest('form').submit();
});
On button click, it will find the closest form (which will be the parent) and submit it by passing the form to the action where you can access the values through your defined method; either get or post.
I think this is what you're asking.
You can either use:
<input type="Submit" />
Or using jQuery you can use:
$(function() {
$('button').on('click', function() {
$('form').submit();
});
});
you use javascript when you have links, or divs, or other elements that can not do post back.
In your case the <button>Submit</button> in html5 renders a submit button that if you click it, you just submit the form and that all you need.
The extra asp.net controls have some more functionality and communication with the code behind, but for the submit of the form, any submit button ether that one, ether the classic <input type="submit" value="Submit"> can do what you ask as they are.

How can I create in C# a page to response to another one?

How can I create in C# a page to response to another one? So when you press the button,login form opens in a new widow(browser tab or widow), and as you login... the form automaticaly refresh the first page.
Like the Open ID form. You press the (Connect with Facebook) button it opens a new window with the login form and then it refreshes the the website where u pressed the button.
Sorry for my English!! :) & please help!
You open a new window from a button click like this:
Markup:
<asp:button type="button" id="btnLogin" runat="server" Text="Click me" OnClientClick="javascript:window.open('newPage.aspx'); OnClick="ServerSideCode" />
Or with a regular HTML button:
<input type="button" id="btnLogin" value="Click me" onclick="javascript:window.open('newPage.aspx');" />
On newPage.aspx you define a function to auto close the current form and reload the parent form. Something like:
function closeMe()
{
window.parent.location.reload();
self.close();
}
And on server-side, when you handle your button Click event, you add this line all the way at the bottom depending on whether the process was successful. Example:
if(loginSuccessful)
ScriptManager.RegisterStartupScript(this.GetType(), "whatever", "closeMe();");
Learn more about AJAX. And technically, a C# code deal with pages (it may however handle HTTP requests & replies).
you can use ( if you open a new window) the opener object.
and you can also make use of the PostBackUrl
also you can make use of form1.Action

How to assign values before unload event in asp.net

Hi i want to assign two string values from popup window back to parent window which open the popup window.
for which i take two hidden field both in parent and child window as runat server,
and on button click event i assing these hidden field some text value
/*(Hidden field 1 ->)*/ c_value.Value = Treeview.SelectedNode.Parent.Value;
/*(Hidden field 2 ->)*/ c_text.Value = "File selected";
and on body unload event i assign these value to parent's hidden field through javascipt
(HTML)
<script>
function assgin()
{
window.opener.document.getElementById("ctl00_ContentPlaceHolder1_p_value").value = document.getElementById('c_value').value;
window.opener.document.getElementById('ctl00_ContentPlaceHolder1_p_text').value = document.getElementById('c_text').value;
</script>
<body onunload="assign()">
but when i try to get these value on parent form it is showing empty.
i not understand why these values become empty may be due to onunload event, if this is the reason then tell me on which event i assign these values to parent form .i want to assign these values before closing child window.
Maybe your problem is due to a typo of "assgin()" function. But if not, here is a small example that works for me. NOTE: you can't test this on a local disk, because in that case the browser will prevent access to window.opener for security reasons.
parent.html
<input type="button" onclick="window.open('popup.html','My Popup')" value="Open popup" />
<br/>
Result from popup: <input type="text" id="result" />
popup.html
<script>
function assign() {
window.opener.document.getElementById("result").value = document.getElementById("mystring").value;
}
</script>
<body onunload="assign()">
Enter string and close the popup: <input type="text" id="mystring" />
<body>

Open a new window and navigate the old window on button click

I have a website with a button that opens up a pop-up window with information. When this button is clicked I want the window with the button to navigate to a new window. Hopefully using the Url.Action commands. The following code was my attempt but does not work. The second part <%=Url.Action("MainScreenAction", "Controller")%> is what should navigate the main screen. But it stopped everything from working.
<input type="button" value="test" onclick="openwindow('<%=Url.Action("PopupWindowAction", "Controller")%>');<%=Url.Action("MainScreenAction", "Controller")%>"/>
Openwindow() is a javascript function that just allows me to open the passed href (url.action) to open it in a pop-up window.
Edit: I forgot to mention that this is in asp.net MVC
You have to set the window's location
<input type="button" value="test"
onclick="openwindow('<%=Url.Action("PopupWindowAction", "Controller")%>');window.location='<%=Url.Action("MainScreenAction", "Controller")%>';"/>
you need to set the window.location.href for that. try below code:
function reloadWindow(url)
{
window.location.href = url;
}
<input type="button" value="test"
onclick="openwindow('<%=Url.Action("PopupWindowAction", "Controller")%>');reloadWindow('<%=Url.Action("MainScreenAction", "Controller")%>');"/>

Categories

Resources