In my Sharepoint 2010 WebPart, I have this code:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (this.dpsvisWebPart != null && this.dpsvisWebPart.CustomTitleProp != null)
{
lbl_Title.Text = String.Format("<h1>{0}</h1>", this.dpsvisWebPart.CustomTitleProp.ToString());
if (this.dpsvisWebPart.CheckboxGenSection1)
{
GenerateSection1();
}
if (this.dpsvisWebPart.CheckboxGenSection2)
{
GenerateSection2();
}
if (this.dpsvisWebPart.CheckboxGenSection3)
{
GenerateSection3();
}
if (this.dpsvisWebPart.CheckboxGenSection4)
{
GenerateSection4();
}
if (this.dpsvisWebPart.CheckboxGenSection5)
{
GenerateSection5();
}
if (this.dpsvisWebPart.CheckboxGenSection6)
{
GenerateSection6();
}
if (this.dpsvisWebPart.CheckboxGenSection7)
{
GenerateSection7();
}
if (AnyCheckboxSelected())
{
// Create Save button
this.Controls.Add(new LiteralControl("<br />"));
Button btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
AddVerticalSpace();
}
}
}
On testing my WebPart (clicking the button, thus - presumably - executing the btnSave_Click handler, I found that nothing was being saved.
Stepping through the code, I see that OnPreRender is being reached - in fact, over and over again.
So, I added this to the WebPart class:
private bool PreRenderAlreadyRun = false;
...and then changed the beginning of OnPreRender() to this:
protected override void OnPreRender(EventArgs e)
{
if (PreRenderAlreadyRun) return;
PreRenderAlreadyRun = true;
base.OnPreRender(e);
. . .
...but PreRenderAlreadyRun is always false when OnPreRender is entered, which happens over and over. Other breakpoints are not reached (the button click, etc.) presumably because the page is in an endless loop.
How can I get OnPreRender() to run only once? Or should this code be in Page_Load() instead of OnPreRender(), or...???
The solution was to move the code that was in OnPreRender() to Page_Load()
Apparently, just like at the stockyard, rendering happens many times, while the page is only loaded once. That's my theory, and I'm stickin' to it (for now, anyway).
Related
we are using chromiumwebbrowser in WPf application, added DragEnter event handler to the chromiumwebbrowser and call come to us.
wanted to change the cursor,
private void Browser_DragEnter(object sender, DragEventArgs e)
{
var currCursor = Browser.Cursor;
if (e.Effects == DragDropEffects.Move)
{
//Mouse.OverrideCursor = Cursors.Hand;
// Mouse.SetCursor(Cursors.Hand);
Debug.Write("DragDropEffects.Move\n");
}
else
Mouse.SetCursor(Cursors.Hand);
Mouse.OverrideCursor = Cursors.Pen;
Browser.Cursor = Cursors.Pen;
//Mouse.Capture(Browse);
e.Handled = true;
}
nothing works, could some help would solve this issue
During my override of OnActivate() in my view-model, I need to call GetView() in order to focus an element. When I do this after I have previously activated my view, it's fine. But when I call this the first activation, it fails.
I was able to get it to work by swapping a few lines in ConductorBaseWithActiveItem.ChangeActiveItem. The original is as follows:
protected virtual void ChangeActiveItem(T newItem, bool closePrevious) {
ScreenExtensions.TryDeactivate(activeItem, closePrevious);
newItem = EnsureItem(newItem);
if(IsActive)
ScreenExtensions.TryActivate(newItem);
activeItem = newItem;
NotifyOfPropertyChange("ActiveItem");
OnActivationProcessed(activeItem, true);
}
and with my changes:
protected virtual void ChangeActiveItem(T newItem, bool closePrevious) {
ScreenExtensions.TryDeactivate(activeItem, closePrevious);
newItem = EnsureItem(newItem);
activeItem = newItem;
NotifyOfPropertyChange("ActiveItem");
if (IsActive)
ScreenExtensions.TryActivate(newItem);
OnActivationProcessed(activeItem, true);
}
This seems to work. Notifying that "ActiveItem" changed triggers the code to load and cache the view. Then ScreenExtensions.TryActivate calls my OnActivate override.
Question: I haven't noticed any problems doing this, but I'm curious if anyone knows better than I do what repercussions this change could have?
Thanks!
One thing you could try is overriding Caliburn's OnViewAttached method and trying to focus it there. That being said, in MVVM, focus is more of a View concern, so if possible, that logic should be moved from the ViewModel to the View.
One way you may be able to solve this is by creating an attached behavior (you will need a reference to the Microsoft.Expression.Interactions assembly):
public class FocusWhenVisibleBehavior : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
this.AssociatedObject.Loaded += this.Loaded;
this.AssociatedObject.IsVisibleChanged += this.VisibleChanged;
}
protected override void OnDetaching()
{
this.AssociatedObject.Loaded -= this.Loaded;
this.AssociatedObject.IsVisibleChanged -= this.VisibleChanged;
}
private void Loaded(object sender, RoutedEventArgs e)
{
this.TryFocus();
}
private void VisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
this.TryFocus();
}
private void TryFocus()
{
if (this.AssociatedObject.IsLoaded && this.AssociatedObject.IsVisible)
{
// Focus the control
this.AssociatedObject.Focus();
}
}
}
And that attaching that behavior to whatever control you want to focus:
<Button>
<i:Interaction.Behaviors>
<b:FocusWhenVisibleBehavior/>
</i:Interaction.Behaviors>
</Button>
Is there a way that I could access the e event arguments for a button that has not been clicked?
I need to delete multiple entries in a gridview by clicking a button and having it simulate clicking the delete button for each selected entry, but I can't use performClick, so I'm trying to call the actual method that deletes each one. However, that method requires an "e As System.Web.UI.WebControls.GridViewCommandEventArgs" parameter and I can't figure out how to get that.
You won't be able to access the EventArgs parameter.
I'd suggest you design your code like this:
public class MyClass
{
private ListView listView;
protected void OnClick(EventArgs e)
{
performAction();
}
private void performAction()
{
listView.deleteSelectedItems();
}
}
Don't implement functionality you are going to need somewhere else in delegates. Instead call this functionality inside the delegates' body. This way you can reuse performAction() somewhere else ..
Your problem calling delete button can be resolved if you add one check box in each row of datagrid and on click of button Delete you can perform delete operation for the checked rows in following manner
Protected void btnDelete_Click(object sender, EventArgs e)
{
for(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if(chkDelete != null)
{
if(chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
ids.Add(strID); //ids can colletion of any type
}
}
}
}
Now send ids to any function to perform delete.
I am trying to track navigation between a number of Web Forms in ASP.NET. I've tried the client side back navigation using the following:
<asp:Button ID="BackButton" runat="server" Text="Back"
OnClientClick="JavaScript:window.history.back(1);return false;" />
Unfortunately this does not work for my scenario due to postbacks going on. My scenario has a number of Web Forms:
Page1.1
Page1.2
Page2
Page3
Navigating forward through the pages works similarly to a wizard. There are 2 starting points - from Page1.1 and Page1.2.
Page1.1 -> Page2 -> Page3
Page1.2 -> Page2 -> Page3
So clicking back buttons will have the following navigation:
Page3 -> Page2
Page2 -> Page1.1
Page2 -> Page1.2
There are additional parameters passed between the pages which need to be maintained.
I am currently looking at maintaining something in the Session to maintain the current call stack which somewhat works however, I am getting a build up of referrer urls. At the minute I am just trying to conceptualise this.
I am running this in SharePoint as Application Pages, however each page is essentially an ASP.NET page for the sake of this example.
So I have introduced an abstract class for each Page:
public abstract class SecureLayoutsPageBase : System.Web.UI.Page
{
private PageController _pageController;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_pageController = (PageController)Session["PageController"];
if (_pageController == null)
{
_pageController = new PageController();
Session["PageController"] = _pageController;
}
if (!Page.IsPostBack && Page.Request.UrlReferrer != null)
{
this.PageController.History.Push(Page.Request.UrlReferrer.ToString());
}
}
protected PageController PageController
{
get
{
return _pageController;
}
}
}
Which has an instance of PageController:
[Serializable()]
public class PageController
{
private Stack<string> _history = new Stack<string>();
public void Previous(HttpResponse response)
{
string previous = _history.Pop();
response.Redirect(previous);
}
public Stack<string> History
{
get
{
return _history;
}
}
}
Then each page will call the PageController.Previous in the server side event handler for the back button click:
protected void BackButton_Click(object sender, EventArgs e)
{
this.PageController.Previous(this.Response);
}
This issue with this is that calling PageController.Previous still results in the Url being added to the stack. I am just wondering if there is a way to prevent the url getting added when back has been clicked. Or alternative solutions...
History(-1) wont work, because this will include postbacks. Just set the button href dependant on whatever page you're on. If you know itsloaded page 3, set the back button to page 2
OK... couple of tweaks to get my scenario working. Not keen on this solution so any others would be good.
Change to SecureLayoutsPageBase:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_pageController = (PageController)Session["PageController"];
if (_pageController == null)
{
_pageController = new PageController();
Session["PageController"] = _pageController;
}
if (!Page.IsPostBack && Page.Request.UrlReferrer != null && Page.Request.Url.ToString() != this.PageController.PreviousUrl)
{
this.PageController.AddHistory(Page.Request.UrlReferrer.ToString());
}
}
Change to PageController:
[Serializable()]
public class PageController
{
private Stack<string> _history = new Stack<string>();
private string _previous;
public void Previous(HttpResponse response)
{
_previous = _history.Pop();
response.Redirect(_previous);
}
public void AddHistory(string url)
{
if(url != _previous)
{
_history.Push(url);
}
}
public Stack<string> History
{
get
{
return _history;
}
}
public string PreviousUrl
{
get
{
return _previous;
}
}
}
I have a parent page Page1 which has button1. Page1 has a usercontrol uc1. uc1 has an update panel inside which a grid grid1 is present. I am trying to set Page1.button1's visibility to false, depending on the row command event(there are some if conditions in the row command event) of uc1.grid1. I am setting Page1.button1's visibility in the following way:
Create a IsButton1Visible property in uc1. Set the property in UC1.Grid1.RowCommand to false, on page1 PreRender event, access IsButton1Visible and set Page1.button1 visibility.
Even though in quick watch Page1.button1 visibility is set to false at the line of assignment, when I see the UI, it is still visible. I don't know what I am doing wrong. Or the way that I am getting hold of button1 and its visibility is not correct.
In general can we set a Parent page's control's property from a user control during the user control event?
If you use the event-driven model approach
Delegate/EventArgs code:
public class ButtonVisiblityEventArgs : EventArgs
{
public ButtonVisiblityEventArgs(bool visible)
{
this.Visiblity = visible;
}
public bool Visiblity { get; private set; }
}
public delegate void UpdateParentButtonVisibilityEventHandler(object sender, ButtonVisiblityEventArgs args);
User control code:
public event UpdateParentButtonVisibilityEventHandler RaiseUpdateParentButtonVisibilityEvent;
private void RequestParentButtonVisibilityChange(bool setVisible)
{
if (RaiseUpdateParentButtonVisibilityEvent != null)
{
RaiseUpdateParentButtonVisibilityEvent(this, new ButtonVisiblityEventArgs(setVisible));
}
}
And in your command handler, just call:
RequestParentButtonVisibilityChange(false);
whenever you want to hide the button. On your page:
protected void Page_Load(object sender, EventArgs e)
{
this.RaiseUpdateParentButtonVisibilityEvent += new UpdateParentButtonVisibilityEventHandler(uc_RaiseUpdatecurrentDisplayPanelRequestEvent);
}
private void uc_RaiseUpdatecurrentDisplayPanelRequestEvent(object sender, ButtonVisiblityEventArgs args)
{
button1.Visible = args.Visiblity;
}
If the problem you are having is that your button lives outside of the update panel, you can do the following. Page codebhind:
protected void Page_Load(object sender, EventArgs e)
{
string hideScript = string.Format("function updateButtonVisibility( visibility ) {{ var button = $('#{0}'); if (visibility) {{ button.show(); }} else {{ button.hide(); }} }}", this.button1.ClientID);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "updateButtonVisibility", hideScript, true);
}
And in your user control command handler:
bool shouldButtonBeVisible = false; //update this appropriately in your logic
ScriptManager.RegisterStartupScript(this, this.GetType(), "upUpdateButtonVisibility", "updateButtonVisibility(" + shouldButtonBeVisible ? "true" : "false" + ");", true);
Please note that this creates a TIGHT dependency between your UC and the page. It requires that any page that consumes this control has registered this script. There are ways to get around this (such as setting a function script callback to call, detecting if that javascript function exists, etc), but this should at least get you moving.
If there is something specific on the page after your update panel finishes that you could key off, it might be better to register an end request handler
$(function() { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(updatePanelEndRequestHandler); } );
function updatePanelEndRequestHandler() {
var shouldBeVisible = $('.MyClassThatSaysIShouldntAllowMoreButtons').length > 0; //do some checking on the grid
updateButtonVisibility(shouldBeVisible);
}
you can put your user controls inside panels on your parent pages and change the visibility.
e.g.
<asp:Panel runat="server" ID="pnlQuote">
...
</asp:Panel>
<asp:Panel runat="server" ID="pnlContact">
<uc1:ContactForm runat="server" ID="ContactForm " />
</asp:Panel>
From the child control you can make a button click event which does something like this
protected void btnBackToQuote_Click(object sender, EventArgs e)
{
Panel pnlQuote = this.Parent.FindControl("pnlQuote") as Panel;
Panel pnlContact = this.Parent.FindControl("pnlContact") as Panel;
pnlQuote .Visible = true;
pnlContact.Visible = false;
}