Development Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsCommunityDevelopment Tutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 8th, 2003, 08:28 PM
Vantera Vantera is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: South Coast of NSW, Australia
Posts: 108 Vantera User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to Vantera
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 .

Reply With Quote
  #2  
Old June 26th, 2003, 10:28 AM
aczarto aczarto is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 1 aczarto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Problems with SelectedValue

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();
}
}

Reply With Quote
  #3  
Old October 1st, 2003, 07:28 AM
davealessi davealessi is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 1 davealessi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old October 14th, 2003, 04:02 PM
michaelsteven michaelsteven is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 1 michaelsteven User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;
}
}
}


Reply With Quote
  #5  
Old June 16th, 2006, 01:32 AM
megha_bansal megha_bansal is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Posts: 1 megha_bansal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 32 sec
Reputation Power: 0
Question How to work ahead when AutoLoadData Property is set to false

Quote:
Originally Posted by Vantera
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 .


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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsCommunityDevelopment Tutorials > Article Discussion: Reduce Repetitive Code with ASP.NET Custom Controls


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek