I need to perform a few checks (enable or disable a label elsewhere on the page) after the user Inserts or Cancels the DetailsView. I have to do this because the DropDownList.SelectedIndexChanged doesn't fire during this event (which is dumb in my opinion, since the index DOES change).
ModeChanging should fire, so can you do this?
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.CancelingEdit)
{
//Checks
}
}
Related
I’ve made a handy “user control” for login to my website and it is placed in the site master.
The natural procedure is that the user logs in the web site and he should be announced with a welcome message containing its full name. The full-Name naturally should sits in a session variable created when the user logged on.
There is no doubt that we place the desired code in the “page_load” event and expect it to pass user’s full-name to the right circumstances (here its a label named lblFullName) in order to print/show the welcome message when login button clicked, But the full-name doesn’t passed until the user logs in the website again (for the 2nd times).
Why this problem happens?
Its some part of my code:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FullName"]==null)
{
//nothing 2 do.
}
else
{
lblFullName.Text = Session["FullName"].ToString();
}
}
You probably set the Session variable in the user control after the Page_Load event has been processed, so that it will not see the user name until the next postback.
In order to update the Label text as soon as the login is validated, you could:
Define an event in the user control
Register an event handler in the main page
Call the event handler as soon as the user has logged in
In the event handler, update the Label text
You could also eliminate the Session variable by passing the user full name in an EventArgs derived class. If you make the following class accessible in the user control and in the main form:
public class StringEventArgs : EventArgs
{
public string Value { get; set; }
public StringEventArgs(string value)
{
Value = value;
}
}
then you can define this event in the user control:
public event EventHandler<StringEventArgs> UserLoggedIn;
In the function where the login is confirmed, you call the event handlers:
private void UserLoginValidation()
{
// Login validation is done here
bool loginSuccessful = ...
if (loginSuccessful && UserLoggedIn != null)
{
UserLoggedIn(this, new StringEventArgs(fullName));
}
}
In the main page, you register the event handler, which updates the Label:
protected void Page_Load(object sender, EventArgs e)
{
loginUserControl1.UserLoggedIn += loginUserControl1_UserLoggedIn;
...
}
private void loginUserControl1_UserLoggedIn(object sender, StringEventArgs e)
{
lblFullName.Text = e.Value;
}
I am developing a MS Word 2010 AddIn, in which I have a combobox which updates a class property when it is changed:
private void comboboxFloweringStart_SelectedIndexChanged(object sender, EventArgs e)
{
Globals.ThisAddIn.currentTaxon.FloweringStart = (short)this.comboboxFloweringStart.SelectedIndex;
}
This class is serialized on shutdown (ThisAddIn_Shutdown event handler). The combobox is located on Microsoft.Office.Tools.CustomTaskPane taxonMarkupPanel based on the user control TaxonPanel myTaxonPanel which I designed.
The problem is that at some point before the shutdown event fires, the SelectedIndexChanged event on the combobox fires and resets the value to 0, and this is the value that is serialized. I know that I could use SelectionChangeCommitted instead of SelectedIndexChanged, but I do at times set the index in code, and the event should fire in these cases too.
The CustomTaskPane does not have a close event that I could use to unsubscribe the event handler and I don't know the order of events when a VSTO is closed. Is there some other event I could subscribe to, or some other way that I can unsubscribe the SelectedIndexChanged event handler when the custom task pane / user control is closed?
You should subscribe to the DocumentBeforeClose event of the Application, and/or Close event of the Document.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Word.Application app = this.Application;
Word.Document doc = app.ActiveDocument;
app.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(ThisAddIn_DocumentBeforeClose);
((Word.DocumentEvents2_Event)doc).Close += new Word.DocumentEvents2_CloseEventHandler(ActiveDocument_Close);
}
private void ThisAddIn_DocumentBeforeClose(Word.Document doc, ref bool cancel)
{
MessageBox.Show(string.Format ("Document {0} is closing.", doc.Name));
}
private void ActiveDocument_Close()
{
MessageBox.Show("Active document is closing.");
}
My logout button's code looks like this:
protected void ButtonLogout_Click(object sender, EventArgs e)
{
Session["login"] = null;
}
And my page's onLoad event looks like this:
protected void Page_Load(object sender, EventArgs e)
{
if ((string)Session["login"] != null)
{
ButtonLogout.Visible = true;
// (...)
}
else
{
ButtonLogout.Visible = false;
// (...)
}
}
I'm having problem with this thing working properly. I didn't know what's going on so I've setted up break points in both ButtonClick and PageLoad events. What I found out is that PageLoad event executes first (sic!) and that's why ButtonLogout wasnt hiding after clicking it. I could simply refresh page in ButtonClick event but I'm not sure whether Loading Page twice after 1 button click is OK. Isnt there any other way to solve this?
Thanks.
The issue is that the page life cycle executes in a specific way. In basic terms it goes:
Page_Load
Events fire
Pre_Render
More details can be found - http://msdn.microsoft.com/en-us/library/ms178472.ASPx
If you're setting the visibility of a control you can set this in the Pre_Render page event and at that point the button event will have fired and set what you require.
I recommend you to use asp.net LoginStatus to handle that. And dont forget to clear the session when the user logs out. Checking Using LoginName and LoginStatus Controls will help you.
protected void HeadLoginStatus_LoggingOut(object sender, LoginCancelEventArgs e)
{
Session.Clear();//It clears the session when the user logged out.
Session.Abandon();
FormsAuthentication.SignOut();
}
I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.
Can I bubble up a button click event of a button in master page to be handled by an event handler in the aspx page ?
You can expose the event handler and hookup to it, like this:
In the master:
public event EventHandler ButtonClick
{
add { ButtonThatGetsClicked.Click += value; }
remove { ButtonThatGetsClicked.Click -= value; }
}
In the page:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
((MyMasterType)Master).ButtonClick += MyHandler;
}
private void MyHandler(object sender, EventArgs e)
{
//Do Something
}
Also, you can avoid the Master type cast and have it already appear in intellisense as your Master's type by using the #MasterType directive in the aspx markup.
You can rebroadcast the event. Declare a new corresponding event in your master page, such as HelpClicked and then aspx pages that use this master can subscribe to the event and handle it appropriately. The master can also take a default action if there are no subscribers (or use an EventArgs with a Handled property or something like that).