Add additional content into a known content placeholder from a user control - asp.net

I have a UserControl that has some javascript I'd like to inject into a known ContentPlaceHolder.
I was hoping to do something like the following except when I append to add the control to found control I get an exception which says I cannot modify the control collection in the Init, Load or PreRender events:
"UserControl.ascx"
<%# Control Language="C#" %>
<asp:Checkbox runat="server" id="checkBox"/>
<app:JavascriptInjector runat="server" InjectInto="ScriptPlaceHolder">
$(function(){ $('#<%= checkBox.ClientID %>').click(function() { ... });
</script></app:JavascriptInjector>
"JavascriptInjector.cs"
using System;
using System.Diagnostics;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
public class JavascriptInjector : PlaceHolder
{
public string InjectInto
{
get { return this.ViewState["InjectInto"] as string; }
set { this.ViewState["InjectInto"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.PreRender += this.__PreRender;
}
private void __PreRender(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.InjectInto))
{
goto performRegularlly;
}
var injectInto = this.FindControlRecursively(this.Page);
if (injectInto == null)
{
goto performRegularlly;
}
injectInto.Controls.Add(this);
return;
performRegularlly:
Debug.WriteLine("performing regularlly");
}
private Control FindControlRecursively(Control current)
{
var found = current.FindControl(this.InjectInto);
if (found != null)
{
return found;
}
foreach (var child in current.Controls.Cast<Control>())
{
return this.FindControlRecursively(child);
}
return null;
}
}

I figured it out. The following JavascriptInjector class will work, although I don't know what the implications are of calling the render method in the PreRender. Also think I may need to do something to figure out which type of HtmlTextWriter that the base application is using.
"JavaScriptInjector.cs"
using System;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
public class JavascriptInjector : PlaceHolder
{
private bool performRegularlly;
public string InjectInto
{
get { return this.ViewState["InjectInto"] as string; }
set { this.ViewState["InjectInto"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.PreRender += this.__PreRender;
}
protected override void Render(HtmlTextWriter writer)
{
if (this.performRegularlly)
{
base.Render(writer);
}
}
private void __PreRender(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.InjectInto))
{
goto performRegularlly;
}
var injectInto = this.FindControlRecursively(this.Page);
if (injectInto == null)
{
goto performRegularlly;
}
performRegularlly = false;
using (var stringWriter = new StringWriter())
using (var writer = new HtmlTextWriter(stringWriter))
{
base.Render(writer);
writer.Flush();
injectInto.Controls.Add(new LiteralControl(stringWriter.GetStringBuilder().ToString()));
}
this.Controls.Clear();
return;
performRegularlly: this.performRegularlly = true;
}
private Control FindControlRecursively(Control current)
{
var found = current.FindControl(this.InjectInto);
if (found != null)
{
return found;
}
foreach (var child in current.Controls.Cast<Control>())
{
return this.FindControlRecursively(child);
}
return null;
}
}

Related

Xamarin.Forms Slider.Clicked event?

I'm making an mp3 player in Xamarin.Forms, in which the slider should both show time expired and allow jumping to locations in the track.
As far as I can tell, only the ValueChanged event is available, with the unfortunate sideeffect that every time my timer updates the slider value, I also trigger my player.SeekTo method, causing broken playback.
Is there any way to specifically pick up touch events on a slider?
If not, does anyone have any suggestions on how to make this work?
In case it's relevant, here's my code for the MainPage:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace MuZor
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
PlayButton.Clicked += PlayButton_Clicked;
PlaylistsButton.Clicked += PlaylistsButton_Clicked;
RandomButton.Clicked += RandomButton_Clicked;
RepeatButton.Clicked += RepeatButton_Clicked;
ChoiceButton.Clicked += ChoiceButton_Clicked;
PreviousButton.Clicked += PreviousButton_Clicked;
NextButton.Clicked += NextButton_Clicked;
TimeSlider.ValueChanged += TimeSlider_ValueChanged;
MessagingCenter.Subscribe<Interfaces.IAudioPlayer>(App.player, "PlayerPrepared", (args) =>
{
System.Diagnostics.Debug.WriteLine("Message received");
PlayerPrepared();
});
}
private void TimeSlider_ValueChanged(object sender, ValueChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Slider value changed, value = " + e.NewValue + "(int = " + (int)e.NewValue);
//App.player.SeekTo((int)e.NewValue);
}
private void NextButton_Clicked(object sender, EventArgs e)
{
if (!App.settings.RandomOn && App.settings.CurrentTrack < App.playlist.Count - 1)
{
Play((int)App.settings.CurrentTrack + 1);
}
}
private void PreviousButton_Clicked(object sender, EventArgs e)
{
if (!App.settings.RandomOn && App.settings.CurrentTrack > 0)
{
Play((int)App.settings.CurrentTrack - 1);
}
}
private void ChoiceButton_Clicked(object sender, EventArgs e)
{
}
private void RepeatButton_Clicked(object sender, EventArgs e)
{
}
private void RandomButton_Clicked(object sender, EventArgs e)
{
}
private void PlaylistsButton_Clicked(object sender, EventArgs e)
{
}
private void PlayButton_Clicked(object sender, EventArgs e)
{
if (App.settings.IsPaused || App.player.IsPlaying())
{
App.player.PauseResume();
if (App.settings.IsPaused)
{
UnPause();
}
else
{
Pause();
}
}
else
{
int trackToplay = App.settings.CurrentTrack != null ? (int)App.settings.CurrentTrack : 0;
Play(trackToplay);
}
}
private void Play(int currentTrack)
{
HelperClasses.SettingsHelper.SaveCurrentTrack(currentTrack);
App.player.LoadAndPlay(App.playlist[currentTrack].Path);
}
private void Pause()
{
HelperClasses.SettingsHelper.SavePausedState(true);
Device.BeginInvokeOnMainThread(() =>
{
PlayButton.Text = "Play";
});
}
private void UnPause()
{
HelperClasses.SettingsHelper.SavePausedState(false);
Device.BeginInvokeOnMainThread(() =>
{
PlayButton.Text = "Pause";
});
StartTimer();
}
private void StartTimer()
{
double position;
Device.StartTimer(new TimeSpan(0, 0, 1), () =>
{
position = App.player.GetCurrentPosition();
TimeSpan runTime = TimeSpan.FromMilliseconds(position);
Device.BeginInvokeOnMainThread(() =>
{
TimeLabel.Text = runTime.ToString(#"mm\:ss");
TimeSlider.Value = position;
});
if (App.player.IsPlaying())
return true;
else
return false;
});
}
private void PlayerPrepared()
{
var totalDurationInMS = App.player.GetDuration();
TimeSlider.Maximum = totalDurationInMS;
TimeSlider.Minimum = 0;
TimeSpan totalDuration = TimeSpan.FromMilliseconds(totalDurationInMS);
Device.BeginInvokeOnMainThread(() =>
{
RemainingTimeLabel.Text = totalDuration.ToString(#"mm\:ss");
TimeLabel.Text = "00:00";
});
UnPause();
}
}
}
I think I've found a workaround. Since the valuechanged event contains both old and new values, I'll only fire SeekTo if the difference is negative or bigger than 2.

asp.net - object reference not set to an instance of an object for master page cs file

This is the code to my Master Page cs file. For some reason when I run my default page, I get an error that says "object reference not set to an instance of an object."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Theming.MasterPages
{
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
selectedTheme = preferredTheme.Value;
}
if (!string.IsNullOrEmpty(selectedTheme))
{
ListItem item = ThemeList.Items.FindByValue(selectedTheme);
if (item != null)
{
item.Selected = true;
}
}
}
switch (Page.Theme.ToLower())
{
case "darkgrey":
Menu1.Visible = false;
TreeView1.Visible = true;
break;
default:
Menu1.Visible = true;
TreeView1.Visible = false;
break;
}
}
protected void ThemeList_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = ThemeList.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
}
}
The error appears to be at the switch statement but I can't figure out why. I've read on other posts that the value is assigning to the variable as null but I don't know why; I've got dark grey as an app theme. If anyone could please help me it would be very much appreciated.
I don't see Menu1 or TreeView1 defined anywhere in your markup. If those are in a content page, you can't access those in the MasterPage code-behind (there may be a way, but you can't directly access them the way you're trying to).

telerik asp radgrid subclassed column adding and getting custom properties

Here are is my column class and i've added this code to my radgrid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
namespace EFDemo.Views.Controls
{
public class HpGridButtonColumn : GridButtonColumn{
public string LinkedTab;
public override void InitializeCell(System.Web.UI.WebControls.TableCell cell, int columnIndex, Telerik.Web.UI.GridItem inItem)
{
//if (inItem is GridHeaderItem)
//{
// cell.Text = this.DataField;
//}
//if (inItem is GridDataItem)
//{
// string ID = (string) inItem.OwnerTableView.DataKeyValues[inItem.ItemIndex]["CustomerID"];
// cell.Controls.Add(new LiteralControl(ID));
//}
base.InitializeCell(cell, columnIndex, inItem);
}
public override GridColumn Clone()
{
HpGridButtonColumn col = new HpGridButtonColumn();
//you should override CopyBaseProperties if you have some column specific properties
col.CopyBaseProperties(this);
return col;
}
protected override void CopyBaseProperties(GridColumn FromColumn)
{
base.CopyBaseProperties(FromColumn);
((HpGridButtonColumn)FromColumn).LinkedTab = LinkedTab;
}
}
}
protected void HPRadgrid_ColumnCreating(object sender, GridColumnCreatingEventArgs e)
{
if ((e.ColumnType == typeof(HpGridButtonColumn).Name))
{
e.Column = new HpGridButtonColumn();
}
}
Problem is the property [linkedTab] is not being restored on postback. It is null.
Also, the clone method on the column is not being called. I assume this is why the property is not being restored.

Find all textbox control in a page

i am trying to use http Module to disable textbox of each page. Here is my sample coding
public void context_OnPreRequestHandlerExecute(object sender, EventArgs args)
{
try
{
HttpApplication app = sender as HttpApplication;
if (app != null)
{
Page page = app.Context.Handler as Page;
if (page != null)
{
page.PreRender += OnPreRender;
page.PreLoad += onPreLoad;
}
}
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
public void OnPreRender(object sender, EventArgs args)
{
Page page = sender as Page;
if (page.IsCrossPagePostBack)
{
DisableAllTextBoxes(page);
}
}
private static void DisableAllTextBoxes(Control parent)
{
foreach (Control c in parent.Controls)
{
var tb = c as Button;
if (tb != null)
{
tb.Enabled = false;
}
DisableAllTextBoxes(c);
}
}
This coding can work very well but when i use server.transer to another page. Button are not able to disable already.
For example webform1 transfer to webform2. Webform 1's button is able to disable but webform2 is not able to disable. Can anyone solve my problem?
Server.Transfer DOES NOT go through all http module pipline (thats why context_OnPreRequestHandlerExecute isn't executed for you )
you should try Server.TransferRequest or response.redirect or HttpContext.Current.RewritePath
Use LINQ to get all your textbox controls.
Don't use Server.Transfer()
Create an extension method on ControlCollection that returns an IEnumerable. That handles the recursion. Then you could use it on your page like this:
var textboxes = this.Controls.FindAll().OfType<TextBox>();
foreach (var t in textboxes)
{
t.Enabled = false;
}
...
public static class Extensions
{
public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
foreach (Control item in collection)
{
yield return item;
if (item.HasControls())
{
foreach (var subItem in item.Controls.FindAll())
{
yield return subItem;
}
}
}
}
}
Taken from this answer.

MS-Sharepoint 2007: Custom Field Control, TemplateContainer.FindControl always returns NULL

I have SharePoint 2007 on Windows Server 2003 SP1 (in VM).
I am running the web application here: http://vspug.com/nicksevens/2007/08/31/create-custom-field-types-for-sharepoint/
Part of it is below:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomControl
{
public class customfieldcontrol : BaseFieldControl
{
protected TextBox txtFirstName;
protected TextBox txtLastName;
protected override string DefaultTemplateName
{
get { return "CustomFieldRendering"; }
}
public override object Value
{
get
{
EnsureChildControls();
return txtFirstName.Text + "%" + txtLastName.Text;
}
set
{
try
{
EnsureChildControls();
txtFirstName.Text = value.ToString().Split('%')[0];
txtLastName.Text = value.ToString().Split('%')[1];
}
catch { }
}
}
public override void Focus()
{
EnsureChildControls();
txtFirstName.Focus();
}
protected override void CreateChildControls()
{
if (Field == null) return;
base.CreateChildControls();
//Don't render the textbox if we are just displaying the field
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display) return;
txtFirstName = (TextBox)TemplateContainer.FindControl("txtFirstName");
txtLastName = (TextBox)TemplateContainer.FindControl("txtLastName");
if (txtFirstName == null) throw new NullReferenceException("txtFirstName is null");
if (txtLastName == null) throw new NullReferenceException("txtLastName is null");
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.New)
{
txtFirstName.Text = "";
txtLastName.Text = "";
}
}
}
}
This line:
txtFirstName = (TextBox)TemplateContainer.FindControl("txtFirstName");
always returns null.
I removed base.CreateChildControls() but it still returns null.
Any assistance would be greatly appreciated.
Place your control's .ascx file right under CONTROLTEMPLATES folder and try.

Resources