Open a new window and navigate the old window on button click - c#

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")%>');"/>

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.

Issue related to Submit HTTPPOST ActionMethod in MVC

I have asp.net MVC Web application.
i have an input button:
<input type="submit" name="Report" value="To File" id="rptToFile" />
i have another input textbox :
<input type="text" id="txtMemItem1" name="" />
when user hits enter in TextBox i have written code to open a new popup:
but problem is: by hitting enter, it calls HTTPPOST Action Method in Controller.. i dont want to submit it on EnterPress key, but want to open popup..
how can i solve my problem ???
THanks
That is default behavior of the HTML forms Whenever focused on form control and you pressed enter form gets posted to the server.
Alternative solution to this behavior can be write a javascript onkeypress event and check if keyCode is 13 (enter) then return false and open a popup else return true.
HTML
<input type="text" onKeyPress="keyPressed(event)" .../>
Javascript
function keyPressed(event)
{
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13)
{
//open a popup here.
return false;
}
return true;
}
Alternatively if you don't need to post the form ever then you can use something like below too.
<form onsubmit="return false" ></form>

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

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.

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

Categories

Resources