Custom user control data source - asp.net

I have a custom control that has a DropDownList inside and it's created by CreateUserControl. I'm saving data source directly to dropdown control. After page postbacked my binded data disappear. Should i save/restore my binded data myself in some tricky way?
public class EnumDropDownList : UserControl
{
private readonly DropDownList _ddlSelector;
private Dictionary<long, string> _dataSource;
public EnumDropDownList()
{
_ddlSelector = new DropDownList();
_dataSource = new Dictionary<long, string>();
}
public object DataSource
{
set
{
// datasource logic
}
}
public long SelectedValue
{
get { return Convert.ToInt64(_ddlSelector.SelectedValue); }
set { _ddlSelector.SelectedValue = value.ToString(); }
}
public override void DataBind()
{
_ddlSelector.DataSource = _dataSource;
_ddlSelector.DataTextField = "Value";
_ddlSelector.DataValueField = "Key";
_ddlSelector.DataBind();
base.DataBind();
}
[PermissionSet(SecurityAction.Demand, Name = "Execution")]
protected override void CreateChildControls()
{
Controls.Add(_ddlSelector);
}
}

You code is combination of UserControl and CustomServerControl.
It could have be a lot easier if you implement one instead of combination.
I created two controls - UserControl and CustomServerControl.
UserControl
Place the dropdownlist to ASPX instead of loading dymaiclally. If you load dynamically, you'll have to take care of persistence of control.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="EnumDropDownList.ascx.cs"
Inherits="WebApplication2010.EnumDropDownList" %>
<asp:DropDownList runat="server" ID="DropDownList1" />
public partial class EnumDropDownList : System.Web.UI.UserControl
{
private Dictionary<long, string> _dataSource;
public EnumDropDownList()
{
_dataSource = new Dictionary<long, string>();
}
public Dictionary<long, string> DataSource
{
set { _dataSource = value; }
}
public long SelectedValue
{
get { return Convert.ToInt64(DropDownList1.SelectedValue); }
set { DropDownList1.SelectedValue = value.ToString(); }
}
public override void DataBind()
{
DropDownList1.DataSource = _dataSource;
DropDownList1.DataTextField = "Value";
DropDownList1.DataValueField = "Key";
DropDownList1.DataBind();
base.DataBind();
}
}
Custom Server Control (it is a lot easier to implement for your case)
It basically inherits DropDownList web control.
public class MyDropDownList : DropDownList
{
public long SelectedInt64Value
{
get { return Convert.ToInt64(SelectedValue); }
set { SelectedValue = value.ToString(); }
}
public Dictionary<long, string> DataSource
{
get { return (Dictionary<long, string>)ViewState["DataSource"]; }
set { ViewState["DataSource"] = value; }
}
public override void DataBind()
{
foreach (var item in DataSource)
Items.Add(new ListItem(item.Value, item.Key.ToString()));
base.DataBind();
}
}
Usage
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="WebApplication2010.WebForm5" %>
<%# Register Src="EnumDropDownList.ascx" TagName="EnumDropDownList" TagPrefix="uc1" %>
<%# Register TagPrefix="asp" Namespace="WebApplication2010" Assembly="WebApplication2010" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:EnumDropDownList ID="EnumDropDownList1" runat="server" />
<asp:Button runat="server" ID="Button1" Text="Submit" OnClick="Button1_Click" />
<asp:MyDropDownList id="MyDropDownList1" runat="server" />
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Dictionary<long, string> dataSource = new Dictionary<long, string>();
dataSource.Add(1, "One");
dataSource.Add(2, "Two");
EnumDropDownList1.DataSource = dataSource;
EnumDropDownList1.DataBind();
MyDropDownList1.DataSource = dataSource;
MyDropDownList1.DataBind();
}
}

In some cases I have saved the dataset in a session variable. Whicn can then be referenced after postbacks. Something like this:
Session.Add("dataSource", _dataSource);//create the session variable
Then you can reference it, depending on the type the data source is (in example I used a datable)
_ddlSelector.DataSource = (DataTable)Session["dataSource"];

THis Is aspx file :
<telerik:RadComboBox ID="cmbCurrency" runat="server" Width="200px" MaxHeight="200px"
EmptyMessage="Select a currency" AutoPostBack="true" Filter="Contains" EnableLoadOnDemand="true">
</telerik:RadComboBox>
This is code Behind :
if (!IsPostBack)
{
popCurrencyName();
}
public void popCurrencyName()
{
DataTable dtCurrency = objCurrencyBAL.getCurrency(long.MinValue);
if (dtCurrency.Rows.Count > 0)
{
cmbCurrency.DataSource = dtCurrency;
cmbCurrency.DataTextField = "Name";
cmbCurrency.DataValueField = "CurrencyId";
cmbCurrency.DataBind();
}
}
Try this code:

Related

Using ASPxComboBox with ASPxGridView, need to set initial value

I'm trying to use ASPxGridView to display a list of ASPxComboBox controls. Both the rows in the grid and the list of options in the combo boxes are populated from code. I'm having problems setting the initial value of the combo boxes.
I'm looking for it to look similar to the image below.
As you can see in the screenshot, I have been able to get both the grid view & the combo boxes to populate, but I can't figure out how to set the initial values of the combo boxes.
In the Init event of the inner combo boxes, there's no obvious property to retrieve the bound object.
I did find a couple other questions on StackOverflow, for which the answer was to add a bound property to the combo box. However, adding SelectedIndex='<%# Bind("Level") %>' to the declaration for InnerCombo gave me the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Here's what I have so far:
Testing.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Light.master"
AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="MyProject.Testing" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<dx:ASPxGridView ID="MyGridView" runat="server" KeyFieldName="Name">
<Columns>
<dx:GridViewDataColumn FieldName="Name" />
<dx:GridViewDataColumn FieldName="Level">
<DataItemTemplate>
<dx:ASPxComboBox
runat="server"
ID="InnerCombo"
ValueField="ID"
TextField="Desc"
ValueType="System.Int32"
OnInit="InnerCombo_Init" />
</DataItemTemplate>
</dx:GridViewDataColumn>
</Columns>
</dx:ASPxGridView>
<dx:ASPxButton runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" />
</asp:Content>
Testing.aspx.cs:
public partial class Testing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.MyGridView.DataSource = GetDefaultSettings();
this.MyGridView.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Debug.WriteLine("btnSubmit_Click");
for (int i = 0; i < MyGridView.VisibleRowCount; i++)
{
object[] row = (object[])MyGridView.GetRowValues(i, "Name", "Value");
// row[1] is null, but we can get the data by finding the combo box itself.
GridViewDataColumn col = (GridViewDataColumn)MyGridView.Columns["Value"];
ASPxComboBox innerCombo = (ASPxComboBox)MyGridView.FindRowCellTemplateControl(i, col, "InnerCombo");
Debug.WriteLine("{0} = {1}", row[0], innerCombo.Value);
}
}
protected void InnerCombo_Init(object sender, EventArgs e)
{
Debug.WriteLine("InnerCombo_Init");
ASPxComboBox innerCombo = sender as ASPxComboBox;
if (innerCombo != null)
{
innerCombo.DataSource = GetValues();
innerCombo.DataBind();
}
}
private static List<ValueClass> GetValues()
{
// Simple for testing; actual method will be database access.
return new List<ValueClass>()
{
new ValueClass(0, "Zero (default)"),
new ValueClass(1, "One"),
new ValueClass(2, "Two"),
new ValueClass(3, "Three"),
};
}
private static List<SettingClass> GetDefaultSettings()
{
// Simple for testing; actual method will be database access + post-processing.
return new List<SettingClass>()
{
new SettingClass("AA", 0),
new SettingClass("BB", 1),
new SettingClass("CC", 0),
};
}
public class ValueClass
{
public int ID { get; private set; }
public string Desc { get; private set; }
public ValueClass(int id, string desc)
{
this.ID = id;
this.Desc = desc;
}
}
public class SettingClass
{
public string Name { get; set; }
public int Value { get; set; }
public SettingClass(string name, int value)
{
this.Name = name;
this.Value = value;
}
}
}

ASP.net Custom Control with Content Template didn't work for me

With regards to the solution at:
ASP.Net: User control with content area, it's clearly possible but I need some details
I am trying to do the same and here is my code:
The control code behind:
[ParseChildren(true, "Content")]
[PersistChildren(false)]
public partial class SlidingPanelControl : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
phContent.Controls.Add((Control)_content);
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
private PlaceHolder _content;
[PersistenceMode(PersistenceMode.InnerProperty)]
public PlaceHolder Content { get { return _content; } set { _content = value; } }
}
And the control ASPX:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SlidingPanelControl.ascx.cs" Inherits="Photography.Controls.MbExtruder.SlidingPanelControl" %>
<div>
<asp:Panel ID="pnlLockable" runat="server" Visible="False">
<asp:PlaceHolder runat="server" ID="phContent" />
</asp:Panel>
</div>
And this is how I am using the control in my main page:
<uc1:SlidingPanelControl runat="server" ID="SlidingPanelControl"
Title="About" Position="right" Opacity="1" WidthInPixels="600">
<Content><h1>hello world</h1></Content>
</uc1:SlidingPanelControl>
This didn't work for me, it didn't render the html into the control placeholder. Although when I debug OnInit of the control, I can see the _content control having all the html I set (i.e. <h1>Hello World</h1> in this example)
Any guesses what I am doing wrong?
Thanks
Ok, so i checked it on my computer and i found solution that actually works:
[ParseChildren(false)]
[PersistChildren(false)]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Content != null)
{
ContentContainer container = new ContentContainer();
Content.InstantiateIn(container);
phContent.Controls.Add(container);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
private ITemplate _content;
[PersistenceMode(PersistenceMode.InnerDefaultProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
TemplateInstance(TemplateInstance.Single)]
public ITemplate Content
{
get
{
return _content;
}
set
{
_content = value;
}
}
}
public class ContentContainer : Control, INamingContainer
{
}
I checked it myself works like a charm. Code in other sources is the same as yours.

Expose nested HTML property on asp.net UserControl

I have an asp.net UserControl that writes HTML content from a SharePoint list to a Literal control. Currently I insert a title in h5 tags before each SharePoint list item I write to the Literal. Instead of having the title hardcoded to be placed in h5 tags, I'd like to expose a public property of my user control that lets me define the html format for the title. This is a little different from the templated user control questions I've found so many of because it's not really a template for the user control. I just need a string containing html. Here's what I'm looking to do:
public class MyUserControl: UserControl
{
public string TitleFormat { get; set; }
private void ShowContent()
{
...
string output = String.Format(TitleFormat, title) + someContent;
ltlOutput.Text = output.
}
}
In markup:
<UC:MyUserControl id="muc1" runat="server">
<TitleFormat>
<h3>{0}</h3>
</TitleFormat>
</UC:MyUserControl>
How can I set this up?
Here is the answer (provided by Decker Dong in the asp.net forums):
To nest another class into one, you have to declare a new property but
just declare it is an InnerProperty. And set its Design properties.
Now here's a full sample for you:
[ParseChildren(true),PersistChildren(false)]
public partial class MyUserControl : System.Web.UI.UserControl
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public string TitleFormat
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
With these attributes you can use the control as written in the question.
What you need is http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.aspx
HTML
<test:NamingControl runat="server" ID="NamingControl" TitleFormat="This is myTitle">
<TitleFormatTemplate>
My title is <%# Container.TitleFormat %>
</TitleFormatTemplate>
</test:NamingControl>
UserControl
public partial class MyUserControl : System.Web.UI.UserControl
{
private ITemplate template;
protected void Page_Load(object sender, EventArgs e)
{
}
public string TitleFormat
{
get;
set;
}
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TitleFormatTemplate))]
public ITemplate TitleFormatTemplate
{
get { return template; }
set { template = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
TitleFormatTemplate t = new TitleFormatTemplate();
t.TitleFormat = this.TitleFormat;
template.InstantiateIn(t);
this.Controls.Add(t);
this.DataBind();
}
}
Child Control - INamingContainer
public class TitleFormatTemplate : Control, INamingContainer
{
private string _TitleFormat = "";
public string TitleFormat
{
get { return _TitleFormat; }
set { _TitleFormat = value; }
}
}
The simpler approach - No more TitleFormat tag
MyUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs"
Inherits="testgingweb.usrcontrols.MyUserControl" %>
<h3><asp:Label runat="server" ID="PassedValueLabel"></asp:Label</h3>
The Codebehind - MyUserControl.ascx.cs
public string TitleFormat
{
get { return ViewState["TitleFormat"]; }
set { ViewState["TitleFormat"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
PassedValueLabel.Text = String.Format("Whatever {0} here", this.TitleFormat);
}
HTML
<test:MyUserContorl runat="server" ID="NamingControl" TitleFormat="This is myTitle">
</test:MyUserContorl>
Notice that I didn't have the TitleFormat tag anymore.

ASP.NET DynamicData Validate Custom Image FieldTemplate

I've created a custom Image Field Template which works fine. I decided to create some kind of validation for image sizes so I've created a custom validation attribute. here is the code:
public class ImageSizeValidator : ValidationAttribute
{
public long MaxSize { get; set; }
public long MinSize { get; set; }
public override bool IsValid(object value)
{
if (value == null)
return true;
byte[] image = (byte[]) value;
if (image.Length / 1024L > MaxSize || image.Length / 1024L < MinSize)
return false;
return true;
}
}
and here is my FieldTemplate Image_Edit.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Image_Edit.ascx.cs" Inherits="RastinArgham.CRM.Web.DynamicData.FieldTemplates.Image_Edit" %>
<asp:FileUpload ID="FileUpload1" runat="server" OnDataBinding="FileUpload1DataBinding" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" Enabled="false" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" />
and here is Image_Edit.ascx.cs:
public partial class Image_Edit : System.Web.DynamicData.FieldTemplateUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SetUpValidator(RequiredFieldValidator1);
SetUpValidator(DynamicValidator1);
}
protected override void ExtractValues(IOrderedDictionary dictionary)
{
if (FileUpload1.PostedFile == null || String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream.Length == 0)
return;
dictionary[Column.Name] = FileUpload1.FileBytes;
}
public override Control DataControl
{
get
{
return FileUpload1;
}
}
}
and finally my entity meta data class:
[Display(Name = "ServicePhoto", Order = 410, GroupName = "Misc")]
[HideColumnIn(PageTemplate.List)]
[UIHint("Image")]
[ImageSizeValidator(ErrorMessage = "Invalid Photo Size", MinSize = 30, MaxSize = 300)]
public object ServicePhoto { get; set; }
There are two problems:
1- when IsValid(object value) called value is always null!
2- when trying to upload a new image I always get "The value is not valid" Error on client side of
validation.

Nhibernate mapping in asp .net

I am a newbie in NHibernate.I am facing problem in mapping file.I have 2 tables Fetures and Priority.
Feature FeatureID(PK),FeatureName,PriorityID(FK)
Priorty PriorityID(PK),PriorityName
I want to bind a grid to Feature table but but grid should contain PriorityName rather than PriorityID.
I have tried one-to-one,many-to-one and bag.
Please help me how can write mapping file so that I can get PriorityName for particular PriorityID in Feature class.
I know it is a very simple question.But nothing worked for me. After lots of googling I am posting here.
Please help me
Thanks in Advance
Here's a sample using Fluent NHibernate and SQLite:
using System.Data;
using System.IO;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate;
public class Priority
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Feature
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Priority Priority { get; set; }
}
public class PriorityMap : ClassMap<Priority>
{
public PriorityMap()
{
WithTable("Priority");
Id(x => x.Id, "PriorityID");
Map(x => x.Name, "PriorityName");
}
}
public class FeatureMap : ClassMap<Feature>
{
public FeatureMap()
{
WithTable("Feature");
Id(x => x.Id, "FeatureID");
Map(x => x.Name, "FeatureName");
References<Priority>(x => x.Priority, "PriorityID");
}
}
public static class SessionFactoryEx
{
private const string _dbFile = #"C:\data.db3";
static SessionFactoryEx()
{
if (File.Exists(_dbFile))
{
File.Delete(_dbFile);
}
using (var factory = SessionFactoryEx.GetSessionFactory())
using (var connection = factory.ConnectionProvider.GetConnection())
{
SessionFactoryEx.ExecuteQuery("create table Priority(PriorityID int, PriorityName string)", connection);
SessionFactoryEx.ExecuteQuery("create table Feature(FeatureID int, FeatureName string, PriorityID int)", connection);
SessionFactoryEx.ExecuteQuery("insert into Priority (PriorityID, PriorityName) values (1, 'p1')", connection);
SessionFactoryEx.ExecuteQuery("insert into Feature (FeatureID, FeatureName, PriorityID) values (1, 'f1', 1)", connection);
SessionFactoryEx.ExecuteQuery("insert into Feature (FeatureID, FeatureName, PriorityID) values (2, 'f2', 1)", connection);
SessionFactoryEx.ExecuteQuery("insert into Feature (FeatureID, FeatureName, PriorityID) values (3, 'f3', 1)", connection);
}
}
private static ISessionFactory _sessionFactory = null;
public static ISessionFactory GetSessionFactory()
{
if (_sessionFactory == null)
{
_sessionFactory = CreateSessionFactory();
}
return _sessionFactory;
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard.UsingFile(_dbFile).ShowSql()
)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<Priority>()
).BuildSessionFactory();
}
public static void ExecuteQuery(string sql, IDbConnection connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = sql;
command.ExecuteNonQuery();
}
}
}
And in your ASPX page you can bind data:
<%# Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (var factory = SessionFactoryEx.GetSessionFactory())
using (var session = factory.OpenSession())
using (var tx = session.BeginTransaction())
{
var features = session.CreateCriteria(typeof(Feature)).List<Feature>();
featuresGrid.DataSource = features;
featuresGrid.DataBind();
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="featuresGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%# Eval("Id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Priority Name">
<ItemTemplate>
<%# Eval("Priority.Name") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Resources