I created an asp.net web application which has a linkbutton and hyperlink in the default.aspx. Hyperlink is set navigationurl -"www.google.com". Linkbutton opens the same url in a new tab using javasript's window.open.
Default.aspx
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Data</asp:LinkButton><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>
Default.aspx.cs
protected void LinkButton1_Click(object sender, EventArgs e)
{
string url = "http://www.google.com";
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + url + "');", true);
}
Steps to reproduce my query:
1. Clicks LinkButton. This opens google in new window/tab.
2. Click Hyperlink. This navigates to google.
3. Click Browser Back button.
This time the browser navigates back to default.aspx, simultaneously google opens in new window/tab.
T want this not to happen.
Use Hyperlink Button instead of link button and use target="_blank" and url ="www.google.com" this will open url in new window
No need to use java script to open a new Window .... Also It will Resolve Your Problem
<asp:HyperLink ID="HyperLink2" Target="_blank" NavigateUrl="http://www.google.co.in" runat="server" >Data</asp:HyperLink><br />
<asp:HyperLink ID="HyperLink1" Target="_self" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>
here's the code: add the code to the page which you don't want the user to return using back.
if(history.length>0)
history.go(+1);
call it on body load
basically i increment the browser history.
Add this codefunction in your head tag
<script type="text/javascript">
function myfun() {
window.open("http://www.google.co.in");
return false;
}
</script>
and in your body
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return myfun();">Data</asp:LinkButton><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>
Note: Avoid any request to server until and unless its not required,you can also do the same with the help of <a> tag by keeping target="_blank"
Related
I would like to add a textbox value to my url so I am able to click on a button and open a clients record on the button click. I have tried the below as well as other methods but I am unable to get this working (the below is using a session but I have the client ID value stored in a textbox). Is there a way to do this please?
<asp:Hyperlink runat="server" NavigateUrl='<%# Eval("Client_ID","~/ViewCustomers.aspx?id={0}") %>' />
Result should be ViewCustomers.aspx?id=2 for example.
I am using ASP.NET C# and using HTML 5 for the front end development.
Any help is greatly appreciated.
You can give the HyperLink a class and bind a javascript function to it.
<asp:HyperLink runat="server" CssClass="LinkWithID" NavigateUrl='<%# Eval("ride_id","~/ViewCustomers.aspx?id=") %>' />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Then simply append the value of the TextBox to the url
<script type="text/javascript">
$('.LinkWithID').click(function () {
location.href = $(this).attr('href') + $(this).next('input').val();
return false;
});
</script>
I create a link button and put an image on it.
Here is my code:
<asp:LinkButton ID="LinkButton1" runat="server" Text="">
<asp:Image ID="Image1" ImageUrl="" runat="server" />
</asp:LinkButton>
And here is my subsequent C# code:
if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
I provide the ImageUrl from the c# code behind for the Image and add an OnClientClick event from c# code for the Link Button. When the page first load then the button showed properly.
My browser rendered HTML before post back
<a onclick="ClientClick();" id="MainContent_LinkButton1"
href="javascript:__doPostBack('ctl00$MainContent$LinkButton1','')">
<img id="MainContent_Image1" src="Images/embed.png">
</a>
When I press any button in this page and PostBack occurs, then the button disappears but I don't do anything in my code behind for that button for PostBack.
My browser rendered HTML after post back
<a onclick="ClientClick();" id="MainContent_LinkButton1"
href="javascript:__doPostBack('ctl00$MainContent$LinkButton1','')">
</a>
If I do not add OnClientClick or Text for the Link Button then the image does not disappear.Or if I set ViewState false for link button then image does not disappear. So why this button disappear when page is PostBack?
With EnableViewState="true" this code works correct even with post back and the image is shown
<asp:LinkButton ID="LinkButton1" runat="server" Text="">
<asp:Image ID="Image1" ImageUrl="" runat="server" />
</asp:LinkButton>
if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
With EnableViewState="false" You must remove the IsPostBack because is not saved anywhere and you need to add it again
// remove that check if EnableViewState is false
//if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
This occurs because Postback clears all the control of your asp.net page.
To retain controls from previous load, you need to make sure that whatever code is in Page_Load() is re-executed on "Button_click" which causes postback.
void button_click()
{
// do stuff
page_load(Dummy Object);
}
Im trying to create a search button on my website on my homepage which is an aspx page but when I click search all it does is refresh the page instead of preforming the query, any help would be appreciated
heres the code for my index.aspx and the code for my index.aspx.cs page
<asp:TextBox ID="searchtitle" runat="server"></asp:TextBox>
<asp:Button ID="searchitems" runat="server" Text="Search" />
protected void searchitems_Click(object sender, EventArgs e)
{
String stext = searchtitle.Text;
Response.Redirect("search.aspx?searchquery=" + stext);
}
The button is not working because you didnt call the event on the button
<asp:Button ID="searchitems" runat="server" OnClick="searchitems_Click" Text="Search" />
add OnClick attribute with name of the event you want to call and in the given code the event you want to call is searchitems_Click.
definitely when you will click the searchitems page will reload because function for that click is defined on server side.. what you have to do is on the pageload look for the QueryString and get the value of ["searchquery"] then manipulate it as you want..
if you want to not refresh the page use Ajax then...
I have a button like below which in charge of opening an image in top of the ASP.NET application.
<asp:Button
ID="MatrixButton"
runat="server"
Text="Risk Matrix"
CausesValidation="False"
OnClientClick="openImageDoc('Images/RiskMatrixCapitalAllocation.jpg', 'RiskMatrix')"/>
Now I need to have another button to do the same function but this time opens a PDF file
<asp:Button
ID="AnalysisButton"
runat="server"
Text="Risk Analysis"
CausesValidation="False"
OnClientClick="openPDFDoc('PDF/RiskAnalysis.pdf.jpg', 'RiskAnalysis')"/>
Here is the JavaScript:
function openPDFDoc(filePath, titleName) {
var newUrl = baseUrl + filePath;
window.open(newUrl, titleName, 'width=900,height=800,scrollbars=1');
}
You need to remove the extension .jpg from the path.
Replace This:
PDF/RiskAnalysis.pdf.jpg
With This:
PDF/RiskAnalysis.pdf
On a ASP.Net page, I am in front of a problem only with IE. Here is the description.
On a page, there is two buttons and one label. The first button is visible and calls a JS function on the click event. This JS function calls the click function of the second button. The second button has an C€ event handler on the click event. The C# event handler edit the label.
In Firefox : the label is correctly edited after the clicks.
In IE (8) : the label is not edited, despite the C€ event handler has been correctly hit.
Also, I observed, in IE, that the Page_Load event is called two times after the JS button click :
Page_Load
button2_OnClick => change of the Label Text
Page_Load => The Label Text is reset :(
In Firefox, the Page_Load is called only once.
My question is : how to make IE refresh correctly the page as Firefox does after a JS button click ?
Below is the sample test code :
1) Page ASPX
<head runat="server">
<title></title>
<script type="text/javascript">
function button1Click(sender, args) {
var button2 = document.getElementById("button2");
button2.click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="button1" Text="Click-me!" OnClientClick="button1Click();" />
<asp:Button runat="server" ID="button2" Text="Second" OnClick="button2_OnClick" style="display:none" />
<p />
<asp:Label runat="server" ID="label1" Text="Init" />
</div>
</form>
</body>
</html>
2) C# code-behind :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2_OnClick(object sender, EventArgs e)
{
label1.Text = "Changed";
}
}
The ID of your button will not be button1 or button2 when it's rendered. It will probably be something like ctl001_button1. Therefore your javascript will not work. In ASP.NET 4 you can override this behaviour by using an assigned ClientID.
<asp:Button runat="server" ID="button1" Text="Click-me!"
OnClientClick="button1Click();" ClientIDMode="Static" />
<asp:Button runat="server" ID="button2" Text="Second"
OnClick="button2_OnClick" style="display:none" ClientIDMode="Static" />
As an aside, this alludes to the main problem with ASP.NET Winforms - it tricks developers into thinking that the web is a connected environment.
What actually happens when you click an <asp:Button /> element by default is that a postback is invoked. I.e. Your browser sends a request to the server for a new page. It sends up something called ViewState which is how the server knows what you've done and what to render. There is no "event" handled as such.
I think the problem is with the way you are trying to get the hidden button
var button2 = document.getElementById("button2");
maybe change this to
var button2 = document.getElementById("<%= button2.ClientID %>");
After the buttons are rendered in the browser, the ID is changed by the ASP.Net engine, and not the same as your source.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
Hope this helps.