自定义ListControl.'s datatextfield值

DataTextField属性用于ListControls - RadioButtonList,CheckBoxList等,可在DataSource属性中指定字段以显示为列表控件中列表的项目。但是,有时,您不希望从一个数据库字段中显示的值出现在列表中,并且有几种方法可以实现这一目标,具体取决于您想要执行的操作。

级联

如果您想要出现的值可以从多个数据库字段中派生,则可以在SQL语句中使用Contentation来创建表达式。例如,常见要求是在类别的名称之后提供括号(括号)中的类别中的项目数,例如饮料(12)。使用Northwind数据库(访问版本足够好,对此演示),以下是将实现此类结果的SQL:

SELECT Categories.CategoryID, 
Categories.CategoryName & ' (' & Count(Products.ProductID) & ')' As CountOfCategories  
FROM Categories 
INNER JOIN Products 
ON Categories.CategoryID = Products.CategoryID  
GROUP BY Categories.CategoryID, Categories.CategoryName

countofcategorious是保存我们想要的值的表达式,它包括类名值,后跟一个打开括号,与每个类名的Count(Product.Produred)的值相辅相成,并使用关闭括号完成。将此值应用于DatatextField属性是一个简单的作业:

[c#]
string connect = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb";
OleDbConnection conn = new OleDbConnection(connect);
string query = @"SELECT Categories.CategoryID, 
		Categories.CategoryName & ' (' & Count(Products.ProductID) & ')' 
                As CountOfCategories  
		FROM Categories 
		INNER JOIN Products 
		ON Categories.CategoryID = Products.CategoryID  
		GROUP BY Categories.CategoryID, Categories.CategoryName";
OleDbCommand cmd = new OleDbCommand(query, conn);
conn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
CheckBoxList1.DataSource = rdr;
CheckBoxList1.DataTextField = "CountOfCategories";
CheckBoxList1.DataValueField = "CategoryID";
CheckBoxList1.DataBind();
rdr.Close();
conn.Close();
[VB]
Dim query As String = "SELECT Categories.CategoryID," & _ 
		"Categories.CategoryName & ' (' & Count(Products.ProductID) & ')' " & _
                "As CountOfCategories" & _   
		"FROM Categories " & _ 
		"INNER JOIN Products" & _ 
		"ON Categories.CategoryID = Products.CategoryID" & _   
		"GROUP BY Categories.CategoryID, Categories.CategoryName"
Dim connect As String 
connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb"
Dim conn As New OleDbConnection(connect)
Dim cmd As New OleDbCommand(query, conn)
conn.Open()
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
CheckBoxList1.DataSource = rdr
CheckBoxList1.DataTextField = "CountOfCategories"
CheckBoxList1.DataValueField = "CategoryID"
CheckBoxList1.DataBind()
rdr.Close()
conn.Close()

赫德

在Web服务器上加载到内存之后发生控制的prerender事件,但在其呈现给HTML流之前。因此,在页面的生命周期中,可以对控制的属性进行任何最终调整。因此,如果要自定义DataTeXtfield值,则此时是这样做的。在下面的示例中,列出列表中的每个项目都会枚举,并且将该枚举应用于DATATEXTFIELD值。但首先,需要填充DataTextfield:

[C#]
string connect = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb";
string query = @"SELECT CategoryID, CategoryName FROM Categories";
OleDbConnection conn = new OleDbConnection(connect);
OleDbCommand cmd = new OleDbCommand(query, conn);
conn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
CheckBoxList1.DataSource = rdr;
CheckBoxList1.DataTextField = "CategoryName";
CheckBoxList1.DataValueField = "CategoryID";
CheckBoxList1.DataBind();
rdr.Close();
conn.Close();
[VB]
Dim connect As String 
connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb"
Dim query As String = "SELECT CategoryID, CategoryName FROM Categories"
Dim conn As New OleDbConnection(connect)
Dim cmd As New OleDbCommand(query, conn)
conn.Open()
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
CheckBoxList1.DataSource = rdr
CheckBoxList1.DataTextField = "CategoryName"
CheckBoxList1.DataValueField = "CategoryID"
CheckBoxList1.DataBind()
rdr.Close()
conn.Close()

现在我们为复选框的prerender事件创建一个事件处理程序。在处理程序中,我们定义一个整数并将其设置为1.迭代ListItems的集合,我们获取Text属性,然后通过添加整数变量的值来修改它。然后将整数值提升为1。

[C#]
protected void CheckBoxList1_PreRender(object sender, EventArgs e)
{
  int i = 1;
  foreach (ListItem item in CheckBoxList1.Items)
  {
    string curval = item.Text;
    item.Text = i.ToString() + ". " + curval;
    i++;            
  }
}
[VB]
Protected Sub CheckBoxList1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) _
	Handles CheckBoxList1.PreRender
  Dim i As Integer = 1
  For Each item As ListItem In CheckBoxList1.Items
    Dim curval As String = item.Text
    item.Text = i.ToString() & ". " & curval
    i = i + 1
  Next
End Sub