On Asp.net Button click open link in new maximized window - asp.net

I have a button in Update panel in my page. on button click event dependendant on the text boxes entered i m buliding an url and have to open that url in new window.
i have tried:
i) window open .. but it opens un maximized , not a fully maximized window. i tried setting its height to screen hieght,,but the requirement is full window only, shouldnt need to be maximize every time i open window.
<Asp:button OnClick="Btn1_Click" />
//in code behind
protected void Btn1_Click(object sender, EventArgs e)
{
//my code to generate url
// In below lines im trying to open window
//Im ont using OnClientClick becoz..the button is in Update panel
ScriptManager.RegisterStartupScript(btnLoadReport.Page,
Btn1.GetType(), "", "window.open('" + myUrl+ "','','height=' + screen.height + ',width=' + screen.width + ',resizable=yes,scrollbar=yes,toolbar=yes,menubar=yes,location=yes,top=0, left=0');", true);
}
or can i create a hyperlink click event dynamically on button click event.. and how do i try that?
any ideas and samples would be appreciated.
Thanks,
Ahmed

try this: OnClientClick="window.open('url')"

the c# file you write,
button.attributes.add("onclick","javascript:function1()");
in the aspx page,write the below javascript function
function function1()
{
window.open("pagename",other attributes whose values are to be passed to other page);
}

Did not understand you the first time. Try this. Im guessing your not using update panels here right? cause if you are you should use scriptmanger.register.. instead of client script
Btn1_Click()
{
//my code to bulid url
ClientScript.RegisterStartupScript(this.GetType(),"Redirection","window.open('www.google.com', '_blank')",true);
}
<Asp:button OnClick="Btn1_Click" />

try this please : window.open(url, '_blank');
EDIT : look at this link dude example for opening new tab

My issue got resolved by adding 'chromemode=yes' in parameters.. like below..
window.open(url, 'chromemode=yes, scrollbars=yes' );
thank you all for your answers :)

Related

Button's .click() command not working in Chrome

I have a button
<asp:Button ID="submitRegionsButton" runat="server" Text="OK" OnClick="OnSubmitRegion" />
and when I click it, everything works as expected, however when I have a javascript function call it using btn.click(), the click command is not executed. The button is found properly but just doesn't then work or run the OnClick function in my code behind
Any ideas? It works in IE, haven't tested firefox though
Okay, tested in firefox, doesn't work there. Everything works right up until the actual call to .click(), no idea why :/
Code that calls the click:
function dropDownProductBlur() {
if (isItemClicked) {
var combo = $find("<%= productDropDown.ClientID %>");
var btnSubmitProd = $get(combo.get_id() + "_Footer" + "_submitProductsButton");
if (btnSubmitProd)
btnSubmitProd.click();
}
}
Just for understanding, the button is contained within a dropdown, and when the dropdown closes, it "clicks" the button (or, well, is supposed to..) clicking the button manually works, and the find works and finds the button properly.
I recently ran into this problem myself. I believe the issue is that in javascript, this .click() is either treated as the component's onClientClick event, or something else entirely. Not sure on this.
In any case, my solution:
Instead of calling btnSubmitProd.click(), do __doPostBack('Foo', 'Bar'); where Foo is an arbitrary name (typically the component, so "submitRegionButton") and Bar is an arbitrary value for that name, typically the event (so "click") (also, there are TWO underscores there, not one). Then, in your codebehind:
try {
if (Request["__EVENTTARGET".ToString() == "Foo" && Request["__EVENTARGUMENT"].ToString() == "Bar" {
//call the codebehind directly here
OnSubmitRegion(null null);
}
}
I assume your codebehind function is of the form protected void OnSubmitRegion(object sender, EventArgs e). If you need the values for any of those variables, things get a little more complicated. If not, give the above a try.

ASP.NET Refresh Parent Page after Closing Modal Dialog Window

I have an ASP.NET page that launches a modal dialog window:
Dim sURL As String = System.Configuration.ConfigurationManager.AppSettings("PAYORS_Path") & "PayorCopy.aspx"
lnkCopy.Attributes.Add("onclick", "javascript:window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');return false;")
The user can create new items in this modal window. When the user clicks the Close button the modal dialog window is successfully closed, but the new items created do not appear on the parent page. When the modal window is closed I would like the parent page to refresh to show the new items the user created. Currently:
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnClose.Click
Dim strjscript As String = "<script language='javascript'>self.close();</script>"
LtClose.Text = strjscript
End Sub
I have tried adding to this script:
window.opener.location.reload(true);
But then when I test I get an error that says "Unable to get value of the property 'location': object is null or undefined."
Any help would be much appreciated!
Instead of using the old modal dialog window, try jQuery Dialog because it behaves just like a modal but keeps you in the same window. Therefore it's is easily refreshed with the following code:
location.reload(true);
For more information, check out this question here at SO.
Hope it helps.
That would be because there is no 'opener' :)
Location is method of the window object so you can even treat it as global is most cases. Any of those should work:
window.location.reload(true);
or:
location.reload(true);
EDIT Yeah I just noticed where you put that script.. if it's in the click handle inside the modal..then 'window' there is really a different one - it's modal after all! Try adding it directly after the code opening modal window - the script execution will be blocked until the modal returns, so you can so something like this:
var shouldReloadPage = window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');
if(shouldReloadPage){
window.location.reload(true);
}
return false;
This is still inside the click handler ofc, just showing in code block for easier reading.

How do I programatically put telerik rad grid in "add new" mode on page load

Seems like this should be easy but I must just be missing something... I have a Telerik RadGrid on a page that allows inline editing. How do I programatically put the grid into edit mode to insert a new row into the grid. When the page loads I would like show the existing data and also display 1 empty row that a user can easily type into to add a new record to the table. (I don't want them to have to push the add new button)
Found the answer while back.... updating this in case others need it
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid1.Rebind();
Lifesaver!!
You can set
radGrid1.MasterTableView.IsItemInserted = false;
radGrid1.Rebind();
that will remove the inserted item (like pressing cancel).
If you need show inset form always you can use next:
protected void NeedDataSource(object sender, GridNeedDataSourceEventArgs e) {
parametersGrid.DataSource = data;
parametersGrid.MasterTableView.IsItemInserted = true;
}
You could try using jQuery to press your add button once the page is ready.
Something along the lines of -
$(document).ready(function() {
$("#addButton").click();
}
What I did when I wanted to do the same with the Telerik grid is to set the MasterTableView.IsItemInserted property of the control to true inside the OnNeedDataSource event handler. I suppose that it should work if you set the property inside the OnDataBound grid handler as well.
Dick
RefreshGrid(userName, "priority", true, false);
RadGrid radGrid = RadGrid1;
radGrid.MasterTableView.InsertItem();
radGrid.Rebind();

Redirect from pop up window

how can i redirect to other page from pop up window with response.redirect.
I'm assuming that you're wanting to do this when the user presses a button or something on the popup. You can use ClientScript.RegisterStartupScript function to get ASP.Net to put some JavaScript in the right place so that it's executed as soon as the postback has completed. Something like this...
public void RedirectParent(string url)
{
string js = "";
js += "window.opener.location.href='" + url + "';";
js += "window.close();";
ClientScript.RegisterStartupScript(this.GetType, "redirect", js, true);
}
You can remove the window.close() line if you want to keep the popup open, but I'm guessing that you want to close it automatically.
The "redirect" bit on the last line is just the name that you want to assign to the script - this can be anything you wish.
Once popup is opened to a given URL you cannot use server side script to redirect the parent. You need to use javascript inside the popup:
if (opener && !opener.closed) {
opener.location.href = "http://www.example.com/somenewurl";
}

How to click a button on an ASP.NET web page programmatically?

I am trying to figure out how to click a button on a web page programmatically.
Specifically, I have a WinForm with a WebBrowser control. Once it navigates to the target ASP.NET login page I'm trying to work with, in the DocumentCompleted event handler I have the following coded:
HtmlDocument doc = webBrowser1.Document;
HtmlElement userID = doc.GetElementById("userIDTextBox");
userID.InnerText = "user1";
HtmlElement password = doc.GetElementById("userPasswordTextBox");
password.InnerText = "password";
HtmlElement button = doc.GetElementById("logonButton");
button.RaiseEvent("onclick");
This fills the userid and password text boxes fine, but I am not having any success getting that darned button to click; I've also tried "click", "Click", and "onClick" -- what else is there?. A search of msdn of course gives me no clues, nor groups.google.com. I gotta be close. Or maybe not -- somebody told me I should call the POST method of the page, but how this is done was not part of the advice given.
BTW The button is coded:
<input type="submit" name="logonButton" value="Login" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="logonButton" tabindex="4" />
How does this work? Works for me
HtmlDocument doc = webBrowser1.Document;
doc.All["userIDTextBox"].SetAttribute("Value", "user1");
doc.All["userPasswordTextBox"].SetAttribute("Value", "Password!");
doc.All["logonButton"].InvokeMember("Click");
var btn = document.getElementById(btnName);
if (btn) btn.click();
There is an example of how to submit the form using InvokeMember here.
http://msdn.microsoft.com/en-us/library/ms171716.aspx
You can try and invoke the Page_ClientValidate() method directly through the clientscript instead of clicking the button, let me dig up an example.
Using MSHTML
mshtml.IHTMLWindow2 myBroserWindow = (mshtml.IHTMLWindow2)MyWebBrowser.Document.Window.DomWindow;
myBroserWindow.execScript("Page_ClientValidate();", "javascript");
Have you tried fireEvent instead of RaiseEvent?
You could call the method directly and pass in generic object and EventArgs parameters. Of course, this might not work if you were looking at the sender and EventArgs parameters for specific data. How I usually handle this is to refactor the guts of the method to a doSomeAction() method and the event handler for the button click will simply call this function. That way I don't have to figure out how to invoke what is usually just an event handler to do some bit of logic on the page/form.
In the case of javascript clicking a button for a form post, you can invoke form.submit() in the client side script -- which will run any validation scripts you defined in the tag -- and then parse the Form_Load event and grab the text value of the submit button on that form (assuming there is only one) -- at least that's the ASP.NET 1.1 way with which I'm very familiar... anyone know of something more elegant with 2.0+?
Just a possible useful extra where the submit button has not been given an Id - as is frequently the case.
private HtmlElement GetInputElement(string name, HtmlDocument doc) {
HtmlElementCollection elems = doc.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
String nameStr = elem.GetAttribute("value");
if (!String.IsNullOrEmpty (nameStr) && nameStr.Equals (name))
{
return elem;
}
}
return null;
}
So you can call it like so:
GetInputElement("Login", webBrowser1.Document).InvokeMember("Click");
It'll raise an exception if the submit input with the value 'Login', but you can break it up if you want to conditionally check before invoking the click.
You posted a comment along the lines of not wanting to use a client side script on #Phunchak's answer. I think what you are trying to do is impossible. The only way to interact with the form is via a client side script. The C# code can only control what happens before the page is sent out to the browser.
try this
button.focus
System.Windows.Forms.SendKeys.Send("{ENTER}")

Resources