How do I accesssqldatasource data values in code behind and bind them to strings or texboxes etc. as oposed to using Eval in the markup?
I can create a new database connection, but I would like to use the data values from the autogenerated sqldatasource control
Many thanks,
Here is one way. It simply creates a table and adds the rows and columns from the data source
<%@.PageLanguage="C#" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
protectedvoid Page_Load(object sender,EventArgs e)
{
Table t =newTable();
PanelData.Controls.Add(t);
System.Data.DataView dv = (System.Data.DataView)ds.Select(newDataSourceSelectArguments());
for (int rowIndex = 0; rowIndex < dv.Count; rowIndex++)
{
TableRow tr =newTableRow();
t.Rows.Add(tr);
System.Data.DataRow dr = dv[rowIndex].Row;
for (int colIndex = 0; colIndex < dr.Table.Columns.Count; colIndex++)
{
TableCell tc =newTableCell();
tr.Cells.Add(tc);
tc.Text = dr[colIndex].ToString();
}
}
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
</head>
<body>
<formid="form1"runat="server">
<asp:PanelID="PanelData"runat="server"/>
<asp:SqlDataSourceID="ds"runat="server"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT ProductID, ProductName FROM Products"></asp:SqlDataSource>
</form>
</body>
</html>
No comments:
Post a Comment