Change label across pages - c#

I'm working on an application that connects to a serial port. I have experience in C#, but not so much in asp.net.
I have a Label on an aspx page which displays the status of the connection. When I click a button, a new page opens which let's you select the serial port you want to use. When you press the OK button on that page, it will send you back to the original page with the label.
Initially, the label says: "Status: Not connected". I'm saving this string in a globaldata class so I can access the string. I want that when you click the OK button on the new page, it will change the label to "Status: Connected", so when the original page loads the label will be changed.
So in the OK button I added onclick="butOK_Click" and then in the event I change the string in the globaldata class. However, I don't know how to get back to the page. Adding a PostBackUrl="Home/Index" (where Home/Index is the begin page with the label) does return to the begin page, but doesn't execute the code that changes the string.
How can I achieve what I want?
EDIT: now that I think about it, I only want to change the label after executing a function that actually connects to the serial port. I already have this function, it just needs to let the page know that it is connected.

Response.Redirect("~/Home/Index.aspx");

Related

Asp.net c# Button click call a procedure and Also opens in a target_blank

I am trying to make a button that when clicked call a procedure but also opens in a pop-up. I can't find how to do it because all the search i did only tells me to put it on te clientclick :
<asp:Button ID="cmbGen" runat="server" Width="240px" Text="Générer le rapport" OnClick="cmbGen_Click"></asp:Button>
the onclick opens up a pdf, and its not working well on Ie, so, to solve this i would like the pdf to opens in a pop up
not sure what i could do.. anyone got an idea ?
EDIT
The code is pretty big, but basicaly, depending on what checkboxed were checked, it will create a pdf file and show it. this works pretty well, but it opens up in the current page, i would like to make it in a pop-up
A Button always submits a postback to the server on the current window, so you can't directly tie a new window to it. You'll need to write some javascript to do that, and open a popup window.
You can do this lots of ways - you can hook up an event handler to the button so when it's clicked, it immediately opens a new window, and that window is pointed to your server code which returns the PDF. Or you can do a regular postback, and return some javascript that pops up a new window. But either way, javascript is the only way to get a popup from a form button.
Liam's suggestion of making a link instead of a button is probably the simplest method - you can throw an image on that link to make it look like a button if you want.
EDIT
Based on your comment on the other answer, your simplest bet would be to return some javascript on the button click method, using ClientScript.RegisterStartupScript or whatever Microsoft is recommending these days. You can do whatever logic you need to first, then get that into a new handler either through session or querystring parameters, and have the client pop up a new window pointing to that handler.
Can't see you c# so it's not 100% sure what you want but why use a asp:Button at all:
Générer le rapport

Receive keypress and ignore 'tab' in MVC 4 c# web site

I want my website to be shown on a 'kiosked' computer (i.e. the site is the only thing visible on the screen). And I have a card reader that sends a string to whatever text field is active when a card is shown to it. After the string the card reader sends a tab-key.
Now how could I receive the string each time the card is shown and at the same time keep the website active at all times. When the tab-key is sent it should not tab to the address bar or anywhere else, if it would then I wouldn't receive the next string as the site wouldn't be active any longer.
Also, the received string should never be visible on the the screen, I just want to receive it in the code behind.
How might I solve this?
You could handle the change event of the textbox and post the data via AJAX to your controller then re-focus the field.
Also, the received string should never be visible on screen
Few ways of doing that, make the field blend into the page (e.g. change the background/fore colour) or hide it by putting a div over it etc.

Page back not working as expected on BinaryWrite

I have a home/main menu page. There's a link to the form. If you run the form with inputs, you get Excel output. If you click 'open' to open that output in the browser, the back-button takes you to the home/main menu.
Which is understandable, since if the form page is X.aspx, the binaryWrite that outputs the Excel doesn't really change that.
The problem is that if there's a postback, the back button takes you to the form page. This is inconsistent. If I run the page or a drop down causes a postback, I really want the back-button to take me to the form field, so that I can run the excel sheet again.
Things I've tried.
I tried adding a location header.
e.ContentFileName = "ELTFile.Output.xls";
Before the response.Writing ...
private void SendFileToBrowser(DownloadFileEventArgs e)
{
if (!e.Cancel && e.FileObject != null)
{
this.Page.Response.Clear();
this.Page.Response.ClearContent();
this.Page.Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}", e.ContentFileName));
this.Page.Response.AddHeader("Content-Length", e.FileObject.LongLength.ToString());
this.Page.Response.AddHeader("Location", e.ContentFileName);
this.Page.Response.ContentType = e.ContentType;
this.Page.Response.BinaryWrite(e.FileObject);
this.Page.Response.End();
}
}
And I tried
Response.Cache.SetCacheability(HttpCacheability.Public);
As well as
Response.Cache.SetCacheability(HttpCacheability.Server);
But basically, depending on whether or not I post back before I click the 'Submit' button to generate the Excel, I get the main menu (don't want) or the form page (want). I want the form page every time. Basically I want to tell the browser "I know it's not really a different page, but treat it that way, will ya please?"
You cannot directly add pages to a browser history.
You could make your application such that the form is always a page ahead of the excel output.
To do this, have a separate page to output the excel.
When you have the inputs to generate the excel, Response.Redirect to the generation page.
This will keep your input form in the browser history.
Note: you could redirect to the same page too if you liked, but that would need you to track it.

ASP.NET Page loads slow as a popup, but not standalone link

Got a strange one, here.
I'm working on a basic ASP.NET/C# code-behind app where summary data is listed in a grid, and each record has an accompanying "update" button. Clicking the button triggers a window.open() where the ID for each row of the grid is passed into a query string to retrieve the related record for edit in the new window.
Example from the rendered "grid" page:
window.open('EditTool.aspx?ID=' + ID, 'new_window', width=550, height=300');
When the page opens from the button, it can take upwards of 10 seconds to render. When I open a new tab and just paste the URL and query string content into the address bar, the page renders almost immediately.
I've peppered the content of the page with log4net statements, and it looks like all of the controls and code-behind C# executes in a few milliseconds.
For investigation's sake, I've got popup blockers deactivated, and I've tried this on IE7 (workplace standard, ugh), FF, and Chrome.
Any ideas as to how to make the rendering faster, or where else I can look to see what's slowing it down?
Update:
I've created a new shell webapp that has a button opening an empty (just the stuff that gets added when you "Add New Item") ASPX as a popup. The popup renders immediately. I've also modified my existing app to open an empty popup, and I get the same delay. It's looking like the app server is waiting to process something before it starts processing the page, rather than rendering the page, slowly.
Does ASP.NET have a setting where you can tell it not to recompile a page on each render?

Getting url of a page once making a form submission

I am designing a winforms based testing app (which is based upon WatiN). I specify a page to test and the value to insert into a textbox, and then click the corresponding button.
Is it possible to add a query string to the request I make (when clicking button) and then get the URL of the next page? Based on this, I need to screen scrape it.
Not sure about Watin syntax, but in Watir (Ruby version) you can get URL of the page displayed in browser with
browser.url
Or do you need to get URL of the next page before you open it?
Based on your comment to AmitK, Željko's answer is right.
In WatiN (and C#), the syntax is:
Console.WriteLine("The current page is:" + ie.Url.ToString());
(just in case: 'ie' is the browser reference, use whatever object you use to enter text and click buttons)
What exactly do you mean by "next page" ? Does the form when submitted redirect to another page? If so, you will receive a HTTP 302/303 status code with the URL of the next page.

Categories