Problem with DataTextFormatString

G

Gus Gustafson

I am data binding a checkbox list, cbl_phases, to an SQL database.

After the data has been bound, I determine the number of columns that the
checkbox list should have so as to balance the presentation. The value is
used to update RepeatColumns.

cbl_phases.RepeatColumns = checkbox_columns ;

Then I compute the length of the longest control title. The value is used to
update DataTextFormatString (expecting that will cause the controls to evenly
space the checkboxes horizontally).

cbl_phases.DataTextFormatString =
"{0:" + longest_control_title.ToString() + "}" ;

This does not have any effect.

I tried to pad the strings with spaces to the right but it appears that the
checkbox has trisling spaces trimed before the rendering (when I tried
PadRight with a non-space character, the desired widths appeared).

I have reduced the code to the minumim. Thoughts would be appreciated.

-------- Begin Code

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace current_CNA_phase
{
/// <summary>
/// Summary description for current_CNA_phase.
/// </summary>
public class current_CNA_phase : System.Web.UI.Page
{
protected System.Web.UI.WebControls.CheckBoxList cbl_phases;
:
protected System.Web.UI.HtmlControls.HtmlForm current_CNA_phase_form;

protected SqlDataAdapter SQL_adapter = new SqlDataAdapter();
protected SqlConnection SQL_connection =
new SqlConnection(ConfigurationSettings.AppSettings["connectDB"]);

private void Page_Load ( object sender,
System.EventArgs e )
{

if ( ! Page.IsPostBack )
{
populate_phases ( ) ;
}
}

private void populate_phases ( )
{
try
{

DataSet phase_dataset = new DataSet ( ) ;
SQL_adapter.SelectCommand =
new SqlCommand(
"SELECT phase_ID,phase_text,active_code " +
"FROM Phases " +
"ORDER BY ordinality_quantity ASC",
SQL_connection);
SQL_adapter.Fill(phase_dataset, "Phases");

cbl_phases.DataSource = phase_dataset;
cbl_phases.DataTextField = "phase_text";
cbl_phases.DataValueField = "phase_ID";
cbl_phases.DataBind();

{
int checkbox_columns = ( int ) Math.Sqrt ( ( double )
cbl_phases.Items.Count ) ;

if ( ( checkbox_columns * checkbox_columns ) !=
cbl_phases.Items.Count )
{
checkbox_columns++ ;
}
cbl_phases.RepeatColumns = checkbox_columns ;
}

{
int longest_control_title = 0 ;

for ( int i = 0; ( i < cbl_phases.Items.Count ); i++ )
{
if ( cbl_phases.Items.Text.Length >
longest_control_title )
{
longest_control_title = cbl_phases.Items.Text.Length ;
}
}
cbl_phases.DataTextFormatString =
"{0:" + longest_control_title.ToString() + "}" ;
}

cbl_phases.Visible = true;

}
catch(SqlException sqlex)
{
:
}
catch(Exception ex)
{
:
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}


-------- End Code

TIA

Gus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top