|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Article Discussion: Reduce Repetitive Code with ASP.NET Custom Controls
Reduce Repetitive Code with ASP.NET Custom Controls If you have any questions or comments about this article please post them here.
You can read the article here . |
|
#2
|
|||
|
|||
|
I'm using a slightly modifed version of your code, and am having problems selecting items in the dropdown list.
I am using: myDropDownList.SelectedValue = myValue; (SelectedValue is a new property in v1.1 of the framework). The drop down list is populated fine, but I cannot seem to select anything from it programatically. Selecting items using the index does not work either. Any ideas? Here is my dropdownlist code: public class DataBoundDropDownList : DropDownList { private string _sqlText; private int _language = 1; public string SqlText { get { return _sqlText; } set { _sqlText = value; } } public int Language { get { return _language; } set { _language = value; } } protected override void OnInit(System.EventArgs e) { if(base.EnableViewState) { if(!Page.IsPostBack) { this.DataBind(); } } else { this.DataBind(); } } protected override void OnLoad(System.EventArgs e) { if(base.EnableViewState) { base.DataBind(); } } public override void DataBind() { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["DataConn"]); conn.Open(); SqlCommand Cmd = new SqlCommand(_sqlText, conn); Cmd.CommandType = CommandType.StoredProcedure; SqlParameter paramLang = new SqlParameter("@Lang", SqlDbType.TinyInt, 2); paramLang.Value = _language; Cmd.Parameters.Add(paramLang); SqlDataAdapter DA = new SqlDataAdapter(); DA.SelectCommand = Cmd; DataSet DS = new DataSet(); DA.Fill(DS); base.DataSource = DS; conn.Close(); base.DataTextField = "TextField"; base.DataValueField = "ValueField"; base.DataBind(); } } |
|
#3
|
|||
|
|||
|
Dynamic loaded controls
I have two questions:
1. When I create my control and drop it on a page, I can;'t move it arouund, or have any control over its size. Why? 2. When using LoadCOntrol, the control doesn't automatically persist across postbacks. Is there a way to register it programatically so that I don't have to recreate it on every postback? |
|
#4
|
|||
|
|||
|
Use in Datalist EditItemTemplate
When I pull up the control with the edititemtemplate, it shows up fine. If I load the itemtemplate first, and then postback and change to the edititemtemplate, it won't load the data.
The data doesn't appear to survive the postback. I've made sure viewstate is on for the datalist and the control. I didn't modify it much at all. I was hoping to use this all over the place, just specifying a codetype value and have it pull up a list of related values from a code table. Still would if I could get it to work. Here's what the code looks like, Thanks, Michael using System; using System.Data; using System.Web; using System.Web.UI.WebControls; using System.Data.OleDb; namespace WebControls { /// <summary> /// Custom Control to populate the DropDownList. /// Taken from example at URL /// </summary> public class CodesDropDownList : DropDownList { private bool _autoLoadData=true; private long _codeTypeID = 0; private System.Data.OleDb.OleDbConnection objConn; public CodesDropDownList() { base.CssClass = "text-default"; base.Width = new System.Web.UI.WebControls.Unit(250); } public bool AutoLoadData { get { return _autoLoadData; } set { _autoLoadData = value; } } public long CodeTypeID { get { return _codeTypeID; } set { _codeTypeID = value; } } public long SelectedCodeID { get { return long.Parse( this.SelectedItem.Value ); } } public string SelectedCodeText { get { return this.SelectedItem.Text; } } public override void DataBind() { if( _autoLoadData ) { DataTable ldt_temp = (DataTable) GetCodes(); base.DataSource = ldt_temp; } base.DataTextField = "string_value"; base.DataValueField = "code_id"; if(base.Items.Count == 0 ) { if( base.Items.Count == 0 ) { base.Items.Add( new ListItem( "No Selection Available.", "-1") ); } } } protected override void OnInit( System.EventArgs e ) { // If data is not being auto loaded then dont do anyting here if( !_autoLoadData ) return; // otherwise, perform databinding if( base.EnableViewState ) { //if using viewstate, only databind the first time the page is loaded. if( !Page.IsPostBack ) this.DataBind(); } else { // if not using viewstate, databind every time the page is loaded. this.DataBind(); } } protected override void OnLoad( System.EventArgs e ) { if( base.EnableViewState && _autoLoadData ) base.DataBind(); // had to comment this out // so I could get it working // right without viewstate. //if( _autoLoadData ) base.DataBind(); } private DataTable GetCodes() { Connection.Open( ref objConn ); string ls_sql = "Select code_id, string_value from code where codetype_id = " + _codeTypeID.ToString(); DataTable ldt_temp = new DataTable( "code" ); OleDbCommand lcmd_temp = new OleDbCommand( ls_sql, objConn ); OleDbDataAdapter lda_temp = new OleDbDataAdapter( lcmd_temp ); lda_temp.Fill( ldt_temp ); objConn.Close(); return ldt_temp; } } } ![]() |
|
#5
|
|||
|
|||
|
Quote:
Hi! I have read the article and found it as very informative. But I have a small problem--I'm building a custom control which has a drop down list which will bounded manually. as mentioned in the article that AutoLoadData is set to false if in case we need to bind the data manually, but could you please guide how to do i go about this? thanks and regards Megha |
![]() |
| Viewing: Dev Articles Community Forums > Community > Development Tutorials > Article Discussion: Reduce Repetitive Code with ASP.NET Custom Controls |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|