ASP.Net Open New Tab in Browser from CodeBehind - asp.net

I need to open a browser tab from a link that is given to me by an asp.net code behind.
Normally I would have a link and target="_blank", but the link that I need is dynamic, so I must have the behavior of a _blank link from code behind.
Any Ideas?

If you have the data needed to create the link when generating the initial HTML, you can do something like this in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick="javascript:window.open('MyPage.aspx?Param=" + Param1.ToString() + "');"; }
}
If you're waiting for the PostBack to get the required data to build the link, you can send javascript down to the browser via the ScriptManager:
protected void Button1_Click(object sender, EventArgs e)
{
//process whatever you need to to get Param1
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('MyPage.aspx?Param=" + Param1.ToString() + "');",true);
}

You're looking for the Target property.

Related

How to write script alert in response.redirect on button click in asp.net

I have written following code in asp.net . It gives error like Bad request.
Code as follows
protected void btnSubmit_Click(object sender, EventArgs e)
{
// some code
Response.Redirect("<script>alert('Successfully Added!');window.location ='../Admin/frmStateMgmt.aspx';</script>"); // It gives error here.
}
Response.Redirect does not take script as parameter it takes url of page on which you want to go.
Response.Redirect("somepage.aspx");
If you need to alert something you can do by this
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "SomeKey", "alert('Some alert')", true);
I got my answer ,
protected void btnSubmit_Click(object sender, EventArgs e)
{
//some code
Response.Write("<script type='text/javascript'>");
Response.Write("alert('Successfully Added!');");
Response.Write("document.location.href='../Admin/frmStateMgmt.aspx';");
Response.Write("</script>");
}
I have got answer by using Reponse.Write. Hope some 1 would get help.

InnerHtml not working with ASP

I have the following test code:
protected void Page_Load(object sender, EventArgs e)
{
MyDiv.InnerHtml = "1=" + Request.Form.Count.ToString(); // WORKING
if (Request.Form.Count > 0)
{
MyDiv.InnerHtml = "2=" + Request.Form.Count.ToString(); // NOT WORKING
}
}
Why is this happening? Do I have to use the Page.IsPostback feature? I try to avoid that because it works poorly with my javascript code. I use jquery ajax when posting data.
It is not working because the whole ASP page is not reloaded when you do your AJAX call, and therefore MyDiv is not being updated in your browser.
You need to do something more like this:
protected void Page_Load(object sender, EventArgs e)
{
MyDiv.InnerHtml = "1=" + Request.Form.Count.ToString(); // WORKING
if (Request.Form.Count > 0)
{
Response.Write(Request.Form.Count.ToString());
Response.End();
}
}
From a code-maintenance point of view, it would probably be better to write a special page or handler for receiving the ajax postbacks, but I'm just sticking with the one page for simplicity.
And then add something like this to the settings parameter of your ajax call:
success: function(data){
$("<%=MyDiv.ClientId%>").innerHTML = "2=" + data.responseText;
}

Adding FileUpload control via code-behind

Because of the environment I work in I need to add controls to a page via code-behind. I have done it dozens of times. For some reason the FileUpload control is giving me grief. here is my code:
FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUploadControl";
this.Controls.Add(fileUpload);
The page looks as though it is timing out and display this error, "Internet Explorer cannot display the webpage".
When I remove the last line (the Add), then the page renders just fine.
Any ideas?
Thanks!!
You didn't mentioned which event handler you have used. Please try this,
FileUpload file;
protected void Page_Load(object sender, EventArgs e)
{
file= new FileUpload();
PlaceHolder1.Controls.Add(file);
}
protected void Button1_Click(object sender, EventArgs e)
{
if(file.HasFile)
{
file.SaveAs(MapPath("~/" + file.FileName));
}
}

Click event not work properly

In my website I wrote that code:
protected void Page_Load(object sender, EventArgs e){ LinkButton lbtnTopicAddress = new LinkButton(); lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
}
But when I press on the link in run time, the caller doesn't go to the EventHandler method.
Why?
Note,
I wrote code like that in many pages in the same website but it work only in one page.
i added that code to many page in website but it worded only in one page every page has its specific code and no relation between them I hope you understand me thanks
I need help pleaseeeeeeee..........................
Did you mean to miss off a ;and a } here?
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
I assume you've put a breakpoint in to ensure it isn't being fired?
I'm not exactly sure but I've got a feeling that instead of Page_Load you need to use Page_Init so your code would look this this:
protected void Page_Init(object sender, EventArgs e)
{
LinkButton lbtnTopicAddress = new LinkButton();
lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;
}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e)
{
Server.Transfer("~/SpecificTopic.aspx");
}
p.s. 5 mins formatting your code can work wonders when trying to debug
Are you adding the button to the controls on your page, or are you trying to find the "lbtnTopicAddress" control on your page?
Simply declaring the button won't do anything -- you have to get a reference to the control itself from the page.

RegisterOnSubmitStatement after client-side validation

I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.

Resources