I have an asp.net application, where the user would click a button and launch another page (within the same application). The issue I am facing is that the original page and the newly launched page should both be launched.
I tried response.redirect, but that tends to unload the original page.
Any suggestions?
This button post to the current page while at the same time opens OtherPage.aspx in a new browser window. I think this is what you mean with ...the original page and the newly launched page should both be launched.
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('OtherPage.aspx', 'OtherPage');" />
Edited and fixed (thanks to Shredder)
If you mean you want to open a new tab, try the below:
protected void Page_Load(object sender, EventArgs e)
{
this.Form.Target = "_blank";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Otherpage.aspx");
}
This will keep the original page to stay open and cause the redirects on the current page to affect the new tab only.
-J
If you'd like to use Code Behind, may I suggest the following solution for an asp:button -
ASPX Page
<asp:Button ID="btnRecover" runat="server" Text="Recover" OnClick="btnRecover_Click" />
Code Behind
protected void btnRecover_Click(object sender, EventArgs e)
{
var recoveryId = Guid.Parse(lbRecovery.SelectedValue);
var url = string.Format("{0}?RecoveryId={1}", #"../Recovery.aspx", vehicleId);
// Response.Redirect(url); // Old way
Response.Write("<script> window.open( '" + url + "','_blank' ); </script>");
Response.End();
}
Use an html button and javascript? in javascript, window.location is used to change the url location of the current window, while window.open will open a new one
<input type="button" onclick="window.open('newPage.aspx', 'newPage');" />
Edit: Ah, just found this: If the ID of your form tag is form1, set this attribute in your asp button
OnClientClick="form1.target ='_blank';"
You should use:
protected void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("otherpage.aspx");
}
Related
Button on click method is not calling
Button code :
<asp:Button ID="personalSub" runat="server" ValidationGroup="personal" Text="Save" CausesValidation="false" OnClick="InsertPersonalDetail" />
C# Code :
protected void InsertPersonalDetail(object sender, EventArgs e)
{
Console.WriteLine("hello");
MessageBox.Show("hello");
}
If you have any problem on the page then you must see a compiler error.
You do NOT have compiler error witch is means that asp.net finds the InsertPersonalDetail function on code behind.
From what I see you call inside the button click two functions that are for desktop programming (not for web page).
Neither one can have any visible effect on your click - there is no console there to see anything, neither user interface to open the MessageBox.
protected void InsertPersonalDetail(object sender, EventArgs e)
{
Console.WriteLine("hello"); // have no effect on web page
MessageBox.Show("hello"); // have no effect on web page
}
So its called but you don't see it by just wait a pop up to appears
To check this out, run it with debuger and add a break point there.
Or add a literal on page and add some text there to verify that is called.
eg, add on page
<asp:Literal runat="server" ID="txtLiteral" />
and on code behind
protected void InsertPersonalDetail(object sender, EventArgs e)
{
txtLiteral.Text += "InsertPersonalDetail called <br />";
}
We have a user control that has a button. We use this user control on different aspx pages. We want the button to behave differently on different pages (dynamically). How might I achieve this goal?
It can be done using event in aspx.cs.
Suppose I have a button on user control
<asp:Button ID="btnClick" runat="server" Text="Click Me" onclick="btnClick_Click" />
on the user control code behind you can write as follows
public event EventHandler ButtonClickDemo;
protected void btnClick_Click(object sender, EventArgs e)
{
ButtonClickDemo(sender, e);
}
No on your aspx.cs content page you can use it as follows
protected void Page_Load(object sender, EventArgs e)
{
Demo1.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);
}
protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
{
Response.Write("It's working");
}
Where Demo.ascx is the user control, so you can write Demo1.ButtonClickDemo
Depending upon how dynamic you want this functionality to be, it may be easier to simply create a property on your user control that would be set by your ASPx (if you can pass the information declaratively) or your codebehind (if you need to pass it programmatically). c.f. http://msdn.microsoft.com/en-us/library/26db8ysc.aspx for more information on creating user control properties.
I have mostly worked with MVC but now doung a project in asp.net. I can't figure out how to render an anchor button, link button or whatever from code behind in a block of text. This button will do postback.
Example: The following errors occured, Click Here.
How do you get the html of that button and append it to the text?
You can add a button to placeholder, like this:
In markup, add this:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
In code, add this:
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "myButton";
btn.Text = "Dynamic Button";
btn.UseSubmitBehavior = true;
btn.Click += new EventHandler(myButton_Click);
PlaceHolder1.Controls.Add(btn);
}
And access the button's click event :
protected void myButton_Click(object sender, EventArgs e)
{
// Do whatever you need
}
NOTE: You have to recreate all dynamically created controls after postback! In this example code to create the button is placed in Page_Load so that it will be recreated at each postback.
Hi i need to display dialog box on linkbutton click in asp.net.On this dialog there will be file upload control to browse files.
Can you please suggest me any example.Because i am pretty new in development.
Ok, The way you must do is :
1) create a page that has a File Upload control inside it (name it for exmaple MyDialogPage.aspx)
and in code behind
protected void btn1_click(object sender, EventArgs e)
{
//do ur stuff with file
}
2) on the Calling page do this:
<asp:LinkButton Id="callDiag" runat="server" onClientClick="CalljavascriptFunction();return false"/>
3) write this javascript function on the Calling page:
<script>
function CalljavascriptFunction()
{
var url="MyDialogPage.aspx";
var retval = window.showModalDialog(url, window, "center:yes;dialogWidth:800px;dialogHeight:600px");
}
</script>
regards
I am working on login/signup module(FYI no rolls/membership just a simple login form),data flow is from sql server for both registeration and login,and code is currently working fine.
I am trying to use signin/signout functionality in my code. I have already created a login form and the user is able to login and after successful login, user is redirected to default_page.aspx.
my r&d code upto this is as:
<td width="122" align="right"><asp:LinkButton ID="LinkButton1" runat="server"
onclick="LinkButton1_Click">Sign Out</asp:LinkButton></td>
cs page:
protected void LinkButton1_Click(object sender, EventArgs e)
{
//After click Log out we need to delete all session values
Session.RemoveAll();
Response.Redirect("userlogin.aspx");
}
from above code u can see I have put only a linkbutton and if user click on above link button user got sign out and redirected to loginpage(here userlogin.aspx). But now if I am trying to change(see below aspx and .cs) as signin|signout i am getting error as:'System.Security.Principal.IPrincipal' does not contain a definition for 'IsAuthenticated' and no extension method 'IsAuthenticated' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?)
aspx page:
<td width="122" align="right"><asp:LinkButton ID="LinkButtonSignInOut" runat="server"
onclick="LinkButton1_Click">""</asp:LinkButton></td>
.cs page:
protected void LinkButton1_Click(object sender, EventArgs e)
{
//After click Log out we need to delete all session values
if (User.IsAuthenticated)**//error is here**
LinkButtonSignInOut.Text = "Sign Out";
else
LabelSignInOut.Text = "Sign In";
Session.RemoveAll();
Response.Redirect("userlogin.aspx");
}
Any suggestions???
thanks!
Check the below code, I have wrote the comment
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (User.IsAuthenticated) **//error is here**
if(User.Identity.IsAuthenticated) // this is correct code
LinkButtonSignInOut.Text = "Sign Out";
else
LabelSignInOut.Text = "Sign In";
Session.RemoveAll(); //
Session.Abandon(); // Better to use this
Response.Redirect("userlogin.aspx");
// You are display assigning text in label and immediate after you are redirecting it to another page ??
}