HtmlSelect.DataSource Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define a fonte de informação para se ligar ao HtmlSelect controlo.
public:
virtual property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public virtual object DataSource { get; set; }
member this.DataSource : obj with get, set
Public Overridable Property DataSource As Object
Valor de Propriedade
Um IEnumerable ou IListSource que contém uma coleção de valores usados para fornecer dados a este controlo. O valor predefinido é null.
Exceções
A fonte de dados especificada não é compatível com nenhum dos IEnumerable dois ou IListSource, e não nullé .
A fonte de dados não pode ser resolvida porque um valor é especificado tanto para a DataSource propriedade como para a propriedade DataSourceID .
Exemplos
O exemplo de código seguinte demonstra como usar a DataSource propriedade para especificar a fonte de informação a vincular ao HtmlSelect controlo.
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!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>
<title> HtmlSelect Example </title>
<script runat="server">
void Page_Load (Object sender, EventArgs e)
{
// Bind the HtmlSelect control to a data source when the page is initially loaded.
if (!IsPostBack)
{
// Open a connection to the database and run the query.
// Note that the connection string may vary depending on your
// database server settings.
string ConnectString = "server=localhost;database=pubs;integrated security=SSPI";
string QueryString = "select * from authors";
SqlConnection myConnection = new SqlConnection(ConnectString);
SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);
// Create a dataset to store the query results.
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");
// Bind the HtmlSelect control to the data source.
Select1.DataSource = ds;
Select1.DataTextField = "au_fname";
Select1.DataValueField = "au_fname";
Select1.DataBind();
}
}
void Button_Click (Object sender, EventArgs e)
{
// Display the selected items.
Label1.Text = "You selected:";
for (int i=0; i<=Select1.Items.Count - 1; i++)
{
if (Select1.Items[i].Selected)
Label1.Text += "<br /> - " + Select1.Items[i].Text;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> HtmlSelect Example </h3>
Select items from the list. <br />
Use the Control or Shift key to select multiple items. <br /><br />
<select id="Select1"
multiple="true"
runat="server"/>
<br /><br />
<button id="Button1"
onserverclick="Button_Click"
runat="server">
Submit
</button>
<br /><br />
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!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>
<title> HtmlSelect Example </title>
<script runat="server">
Sub Page_Load (sender As Object, e As EventArgs)
' Bind the HtmlSelect control to a data source when the page is initially loaded.
If Not IsPostBack Then
' Open a connection to the database and run the query.
' Note that the connection string may vary depending on your
' database server settings.
Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI"
Dim QueryString As String = "select * from authors"
Dim myConnection As SqlConnection = New SqlConnection(ConnectString)
Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection)
' Create a dataset to store the query results.
Dim ds As DataSet = New DataSet()
myCommand.Fill(ds, "Authors")
' Bind the HtmlSelect control to the data source.
Select1.DataSource = ds
Select1.DataTextField = "au_fname"
Select1.DataValueField = "au_fname"
Select1.DataBind()
End If
End Sub
Sub Button_Click (sender As Object, e As EventArgs)
Dim i As Integer
Label1.Text = "You selected:"
For i = 0 To Select1.Items.Count - 1
If Select1.Items(i).Selected Then
Label1.Text = Label1.Text & "<br /> - " & Select1.Items(i).Text
End If
Next i
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> HtmlSelect Example </h3>
Select items from the list. <br />
Use the Control or Shift key to select multiple items. <br /><br />
<select id="Select1"
multiple="true"
runat="server"/>
<br /><br />
<button id="Button1"
onserverclick="Button_Click"
runat="server">
Submit
</button>
<br /><br />
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
Observações
Use a DataSource propriedade para especificar a fonte de dados a vincular ao HtmlSelect controlo. Uma fonte de dados deve ser uma coleção que implementa ou a System.Collections.IEnumerable interface (como System.Data.DataView, System.Collections.ArrayList, ou System.Collections.Generic.List<T>) ou a IListSource interface. Quando defines a DataSource propriedade, tens de escrever manualmente o código para realizar a ligação de dados.
Se a fonte de dados contiver múltiplos conjuntos de dados, como um System.Data.DataSet objeto com múltiplas tabelas, use a DataMember propriedade para especificar qual conjunto de dados atribuir ao controlo.
Pode especificar quais os campos da fonte de dados a associar às ListItem.Text propriedades e ListItem.Value de cada item no controlo, definindo as DataTextField propriedades e DataValueField respetivamente.
Alternativamente, pode usar a DataSourceID propriedade para ligar automaticamente a uma fonte de dados representada por um controlo de fonte de dados. Quando defines a DataSourceID propriedade, o controlo de listagem de dados liga-se automaticamente ao controlo de fonte de dados especificado. Não precisas de escrever código que chame explicitamente o DataBind método.
Se forem especificados valores tanto para a propriedade DataSource como para a propriedade DataSourceID, ASP.NET não conseguir resolver a fonte de dados e é lançada uma exceção System.Web.HttpException.