我在aspx页面中显示DataList时遇到了问题。
protected void btnAdminExecuteQuery_Click(object sender, EventArgs e)
{
SqlDataAdapter _SqlDA;
try
{
_SqlConnection.Open();
_SqlCommand = new SqlCommand(txtBoxAdminQuery.Text, _SqlConnection);
_SqlDA = new SqlDataAdapter(_SqlCommand);
DataSet _SqlDS = new DataSet();
_SqlDA.Fill(_SqlDS);
_SqlConnection.Close();
DL.DataSource = _SqlDS.Tables[0];
DL.DataBind();
}
catch (Exception ex)
{
if (this._SqlConnection.State == ConnectionState.Open)
{
this._SqlConnection.Close();
}
txtBoxAdminErrorMsg.Text = ex.Message;
}
}
我确定ConnectionString和命令是正确的,因为我已经测试了它们,我可以在调试器中看到DataList有行,它有数据,但它只是没有显示。 这是我在“管理面板”
上执行查询时看到的内容HTML:
<asp:TextBox ID="txtBoxAdminQuery" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="btnAdminExecuteQuery" runat="server" OnClick="btnAdminExecuteQuery_Click" Text="Execute Query" />
<asp:TextBox ID="txtBoxAdminErrorMsg" runat="server" Enabled="False" Height="68px" ReadOnly="True" TextMode="MultiLine" Width="463px"></asp:TextBox>
<asp:SqlDataSource ID="_SqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:CompanyConnectionString %>" SelectCommand="SELECT * FROM [Accounts]"></asp:SqlDataSource>
<asp:DataList ID="DL" runat="server" CellPadding="1" CellSpacing="1" RepeatColumns="2">
</asp:DataList>
</div>
</form>
我的目标是运行自定义查询并在DataList中显示结果,我知道如何通过Visual Studio Designer配置DataList,但如果我从那里开始查询是静态的,我无法改变它,到目前为止据我所知。