How to create an object which runat server asp [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I wish to define a timer, however, the timer which define in my asp.net behind code doesn't work.
When I create the timer in the html, which enable runat server is running perfectly.
How I can define a timer on the behind code, with the runat = "server" attribute?
private Timer loopMessage = new Timer();
protected void Page_Load(object sender, EventArgs e)
{
loopMessage.Tick += new EventHandler<EventArgs>(loopMessage_Tick);
loopMessage.Interval = 3000;
loopMessage.Enabled = true;
}
This code doesn't work, the event is not able to execute

You never added the Timer to the page.
It's created at the class level, but the class is disposed immediately upon handling the HTTP request and no server-side state is maintained for it. So every request is essentially creating a Timer and then disposing it.
If Timer is indeed a page control, it would need to be added to the page. I'm not entirely familiar with this control, but if it needs to emit JavaScript to the client in order to invoke things at given intervals then the control would need to be on the page to make that happen.
Something like this, perhaps:
this.Controls.Add(loopMessage);

Like David said, this code actually creates a new timer on every postback. And if your timer is not in an update panel, then every tick triggers another postback. So if I understand your problem correctly, first you must add an update panel on your page, then maybe you can add that update panel's controls to this timer.
Or you can leave all that ASP.NET problems here and turn to javascript timer and use jquery ajax method to solve your problem. Be sure that this will be way more effective for this problem and your future projects.

Related

How to access design Windows Forms C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Pretty much new to Windows Forms, I know the C# language, just not in the same context. I have searched around for a while and it seems to me that every solution is doing something similar to this:
Label1.Text = "I'm a label".
But I don't understand where Label1 is coming from.
All I have is a new Windows Form Application, which comes with one form preloaded and a Program class. So as this class came with some code, I thought this would be a logical way of accessing the label's properties:
static class Program
{
static void Main(String[] args)
{
FormUpdate frmUpdate = new FormUpdate();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(frmUpdate);
// Why isn't this a suitable way of getting the label?
frmUpdate.label1.Text = "I cause an error!";
}
}
But I don't understand where Label1 is coming from.
Someone used the Visual Studio Designer for Windows Forms and dragged and dropped a Label component onto their form. As Visual Studio has no way of naming them, but needs a name, it simply counts up. The first dropped label is called "Label1".
The access specifier for those controls added is private by default and I'd suggest to leave it that way. If you want to interact with your form, either do it from inside your form or write a public method that you call that will then set all the private properties like the text of a certain label.
Generally speaking, Application.Run(frmUpdate); is running the program, based on the starting form you gave. Anything after that will have little effect. So you ran your form and after you closed it, you set the label. That's not going to have any visible effect. You need to do that before you run the form or while you are running it.

Form.Show() not showing form [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've a c# project in which a form hides itself after some background task finishes, to be opened on command, but under certain conditions, the call to this.Show() or setting this.Visible to true doesn't make the form reappear! I even had the state of the form's Visible value output to be sure, and the form seems to think it's on screen, but it's nowhere to be seen.
The condition that seems to cause it to break is if I give another window control before the form hides itself. If I let it stay in front while it's working, hide itself, then tell it to come back it always does, even if I change focus after the fact, but I change focus away beforehand, it doesn't reappear, even though form.Show appears to be called.
Does anyone have any insight as to why this might be happening? It's such a weird case, especially since the state of form.Visible changes.
public partial class testForm : Form {
private void testForm_Sometrigger(object sender, EventArgs e) {
//some delay. In the actual program, this is thanks to a background worker working.
Thread.Sleep(5000);
//manually change focus to another process/window before this
this.Hide();
//I've been adding a wait here, since in practice the form won't be called again right away.
Thread.Sleep(3000);
//show form again.
this.Show();
// this will be true even if the form isn't actually visible
Console.WriteLine("is visible? "+this.Visible.ToString());
}
}
Code block added upon request. There isn't much to this bit, just showing and hiding and time passing, really.
Turns out the window not appearing probably has something to do with form inactivity and the UI thread not coming back to it, so adding a Form.Activate() after the show call fixed it.
Original question updated with solution.

Reload/Refresh data from DB periodically using entity framework 5 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using entity framework 5 on my winform application. I have a datagridView on my form which contains data from my database:
public partial class Form1 : Form
{
etudiantEntities cont = new etudiantEntities();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
Right now, every thing is perfect.
Now, i want to reload data when there is an update happened in other forms. I want to reload it periodically.
Is there a way to do that with entity framework?
Thank you!
You have like five problems at once. Writing a proper answer would require a book (which I would suggest you to go read anyway), so this answer is a summary you can use to learn more.
First, you need to elaborate on "when there is an update happen[ing] in other forms". You need to detect this change. How to do that, depends on how that form works. Hopefully it does using data binding and INotifyPropertyChanged, see Raise an event whenever a property's value changed?.
Then on these "other forms", you subscribe to their model's PropertyChanged event and propagate that as an event on each form. Be sure to unsubscribe when appropriate as well. In the PropertyChanged event handler of your form, you raise an event that's specific to that form, like MyModelChanged.
Now you have a form that can notify interested parties of events, by subscribing to that event.
Something like this:
var yourEditForm = new YourEditForm();
yourEditForm.MyModelChanged += this.YourEditForm_MyModelChanged;
yourEditForm.Show();
Now where you place this code is pretty crucial. When working with multiple forms you want to communicate with each other, you need some kind of "controller" (or give it a name) that knows about all forms and their events that are relevant to your application, and ties it all together.
So in your controller you now have the above code and this event handler:
private void YourEditForm_MyModelChanged(object sender, EventArgs e)
{
}
Now in that event handler, you can let your aptly named Form1 reload its data. You can do so by exposing a public method that does just that:
public void RefreshGrid()
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
There's your "refresh". You can call form1.RefreshGrid() in the event handler shown above.
Note that all of this is pretty much hacked together. Go read a tutorial or two about data binding in WinForms to let this properly be handled, because doing it manually is going to be a pain to maintain.
You can start by reading Data Binding and Windows Forms and Change Notification in Windows Forms Data Binding on MSDN.

ASP.Net's auto-postback. What happens when its too slow?

I am making a web application. I have gotten a weird error with update panels.
Ok, so say you have two update panels and each update panel has a textbox in it. Both of these textboxes are auto-postback and the update panels update conditionally.
Well, from the behavior I'm observing it seems like if the server isn't faster than the user at processing a request then it sorta gets ignored on the client side.
Like say you type something in 1 of these text boxes and then quickly tab to the next one and type something and tab out. This should cause 2 post backs.
Well, what if 1 post back is being processed at the server and another one happens? Does that post back get dropped at the server side or client side?
The main problem I'm observing with this situation is that when a post back occurs the 1st time, there is a Update() for an update panel. Well, when the 2nd post back occurs interrupting the first, it also does an Update on an update panel(a different one). What the user sees is if they tab through it very quickly(or the server is under high load or whatever) then the 2nd update panel gets updated but not the first.
tl;dr: When a post back interrupts another post back, any update panels that were suppose to be updated in the first post back are not updated(though the second postback ones are)
How can I work around this problem or solve it? I can not update all of the update panels on the screen because then the control that the user is currently on loses focus along with a whole lot of other problems.
UpdatePanels intercept the postback and make a request back to the server using the XMLHTTPRequest object (i.e. they use AJAX).
The second XMLHTTPRequest will cancel the first one if it is still in progress when the second one is made. This is standard behaviour as far as I am aware.
You might want to have the UpdatePanels update together on a button click as opposed to having the update attached to an event on each textbox (it sounds like you have them attached to the blur event). This way you can ensure that lots of requests aren't being made and perhaps disable the button whilst a request is in progress, to prevent a new request from cancelling the one in progress.
EDIT:
You can prevent another request being made form the client side while one request is already in progress by checking the PageRequestManager's isInAsyncPostBack property. Something like the following
function pageLoad(sender, args) {
var pageManager = Sys.WebForms.PageRequestManager.getInstance();
// add a function to execute when an asynchronous postback is initialized
pageManager.add_initializeRequest(checkAsyncPostback);
}
function checkAsyncPostback(sender, arg)
{
var pageManager = Sys.WebForms.PageRequestManager.getInstance();
// check if an async postback is already in progress
if (pageManager.get_isInAsyncPostBack()) {
// cancel this async postback if one is currently in progress
arg.set_cancel(true);
}
}
There isn't really an easy way to know from the server side if the postback is interrupted.
I can't answer your questions specifically, because I don't know how the page manages UpdatePanels and their requests/responses. But, you can probably very easily tell what's going on if you trace the calls with Fiddler. You can see when the requests are firing, and you can see the response as well as if an HTTP error code is sent back or an exception is thrown, etc:
Fiddler2
Look at using the UpdateProgress control: http://www.asp.net/ajax/documentation/live/overview/UpdateProgressOverview.aspx
Also, UpdatePanels are overkill for this kind of thing. Look at using Page Methods (google ASP.NET Page Methods)
EDIT: To further clarify how this would be useful to you, modify the contents of the ProgressTemplate in the UpdateProgress control to display in a modal fashion, so that users cannot do anything until the request completes.

Textbox and focus problems with Timer controls(asp.net)

I am needing to create something like a lock timer(a little thing that just updates a lock time in a database). I thought the Timer control would suite my needs, but whenever the Timer control causes a partial post back, recently typed text in a textbox can disappear(inbetween the post back begin and post back end) and it loses focus.
Because this is only a lock timer, I do not need to refresh any part of the screen, I basically just need to tell the server "hey, don't free my lock, I'm still on this page". So is a Timer control even necessary? Is there an easier way to do this is pure javascript? The only thing I need to know is an ID, which could be kept as a hidden field(and therefore accessible from javascript by DOM)
anyone have any input on how to tune the timer control or a quick javascript way to do it?
edit:
also, I have the updatepanel that contains the timer control outside of the update panel containing the textbox control
If I understand it correctly you need a method which will update the datetime in the database at periodic intervals.
For that you can simply use Ajax. The window.setInterval is a JavaScript function which will fire a piece of code at regular intervals.
window.setInterval(foo,5000);
The above code fires the foo method every 5 seconds.
The only thing you need to lookup is how to call the database. Since, you are already using MS Ajax I suggest you check out ScriptManager control which contains a section for services. Check out the following post which consists of a simple example of how to call WebService methods using MS Ajax:
http://azamsharp.com/Posts/83_Using_FireBug_Profiler_to_Dig_Deep_into_MS_AJAX_and_JQuery_API.aspx

Categories

Resources