Pergunta

I'm trying to wrap my head around databinding using a sqldatasource. Right now I have a sqldatasource, databound fields, and template fields that I bind using: <%# Bind("ColumnName") %>. I understand databound fields, but when using the "Bind" command on a template field, how does it know the value to put there? Does it find the ID for the row and then use the sqldatasource to get the value?

Also, how would I go about doing the same thing as "Bind" in ASP, but in the C# code behind instead?

Foi útil?

Solução

Check the following article:

ASP.NET data binding overview

Visual C# .NET

<%@ Page language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
void Page_Load(Object sender, EventArgs e) 
{ 
   SqlConnection cnn = new 
       SqlConnection("server=(local);database=pubs;Integrated Security=SSPI"); 
   SqlDataAdapter da = new SqlDataAdapter("select * from authors", cnn); 
   DataSet ds = new DataSet(); 
   da.Fill(ds, "authors"); 
   Repeater1.DataSource = ds.Tables["authors"];
   Repeater1.DataBind();
}
</script>
<html>
<body>
   <form id="WebForm2" method="post" runat="server">
      <asp:Repeater id="Repeater1" runat="server">
         <ItemTemplate>
         <%# DataBinder.Eval(Container.DataItem,"au_id") %><br>
         </ItemTemplate>
      </asp:Repeater>
   </form>
</body>
</html>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top