我看到已经有很多像我的主题,但经过一些研究和努力,我仍然没有找到一个可以帮助解决我的具体问题。显示从数据库检索图像(C#和JS)
我已经编写了从数据库中检索图像引用的C#代码。此代码似乎按预期工作。我还编写了应该使用提到的引用在JS页面上放置“src”的som JS代码。 JS代码肯定是问题,因为它没有做我想做的事情。我不知道如何运行这个JS代码,因为它应该在页面加载时运行,而不是在用户单击按钮或链接时运行。 的代码如下:
的htm代码:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script src="JS/ImageFromDB/ImageDBHandler.js" type="text/javascript"></script>
<asp:Literal ID="literal" runat="server"></asp:Literal>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="slideshow">
<img id="imageView" src="" alt="" class="active" runat="server" />
</div>
</asp:Content>
服务器端代码:
public string LoadImagesFromDB()
{
Sql sql = new Sql();
string query = "SELECT Location FROM GalleryImages";
MySqlCommand cmd = sql.Command(query);
DataTable dt = sql.DataTable(cmd);
string images= "<script type=\"text/javascript\">var images= [";
foreach (DataRow row in dt.Rows)
{
images+= "\"" + HttpUtility.UrlDecode(row["Location"].ToString()) + "\",";
}
images = billeder.Substring(0, billeder.Length - 1);
images += "];</script>";
return images;
}
和JS代码: 而这正是一切分崩离析我想。我不知道如何编写JS代码,以便它能够完成这项工作。 'images [this.length]的结果类似于: 'http:www.host.com/Folder/imageFile.jpg'。我很确定这是错误的,但我不知道该写什么。我很惊讶,图像[this.length]不返回一个数字(长度,当然这将是无用的)。
function imageSlider() {
$("#imageView").attr("src", images[this.length]);
}
---------------------------------以防万一------- ----------------------------
这里是html页面的c#类。这个类调用从数据库进行查询的类。
private WebLogic logic = new WebLogic();
public void GetImageForView()
{
string images= logic.LoadImagesFromDB();
literal.Text = images;
// imageView.Src = HttpUtility.UrlDecode(images);
}
我真的希望你们的专家能够帮助我,我一直在为此奋斗了3天。 在此先感谢
非常感谢,这很有道理。我相信这会有所帮助。我会按照方法 – AomSet
好,那我很高兴。如果你遇到困难,请告诉我。如果你这样做,那么请将我的答案标记为已接受。 – Tobiasz
再次嗨。如何填写var images = [];?我也有点卡住这个方法:imagesUrlsReceived =函数(结果)。 “结果”参数将如何查看,以及将该参数作为参数? – AomSet