【发布时间】:2017-01-16 14:34:13
【问题描述】:
我的问题是当我点击产品页面时(在下面的 URL 中)。它工作得很好,但是在第一次单击产品菜单时经过一段时间的空闲时间后,它不会将产品图像从数据库加载到中继器控件。
我正在使用 Bootstrap 而不是更新面板。
请参考以下链接。 MySite(See Product Page On First Time And Click After 5 to 10 minute of idle time)
我的 ASP.net 代码是
<asp:Repeater ID="rptrProduct" runat="server">
<ItemTemplate>
<div class="portfolio-item col-xs-12 col-sm-4 col-md-3">
<div class="recent-work-wrap hovereffect">
<img id="dtimg1" runat="server" class="img-responsive imggray" src='<%#Eval("ImagePath")%>' alt="" style="height: 185px!Important;" />
<div class="overlay1">
<div class="recent-work-inner">
<a id="aimg2" runat="server" href='<%#Eval("ImagePath")%>' rel="prettyPhoto">
<h2>
<%#Eval("ProductName")%></h2>
</a>
<p>
<a id="adtimg1" runat="server" class="preview" href='<%# string.Format("~/BMSubProduct.aspx?pro={0}", Eval("ProductId")) %>'>
<i class="fa fa-eye"></i>View Products</a>
</p>
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
我只是不明白为什么第一次它不会加载任何图像。
我的 C# 页面代码是
if(!Page.IsPostBack)
{
DataTable dt = DBMaster.getQueryDataTableSchema(query);
if (dt.Rows.Count > 0)
{
string path = GlobalFunction.getPath(1);
NoProducts.Visible = false;
for (int i = 0; i < dt.Rows.Count; i++)
dt.Rows[i]["ImagePath"] = path + Convert.ToString(dt.Rows[i] ["ImagePath"]);
rptrProduct.DataSource = dt;
rptrProduct.DataBind();
}
else
{
NoProducts.Visible = true;
rptrProduct.DataSource = null;
rptrProduct.DataBind();
}
}
public static DataTable getQueryDataTableSchema(string query)
{
try
{
using (IDbConnection con = sq.Open())
{
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = query;
IDbDataAdapter adp = sq.GetAdapter(cmd);
DataSet ds = new DataSet();
adp.FillSchema(ds, SchemaType.Source);
adp.Fill(ds);
con.Close();
return ds.Tables[0];
}
}
catch (Exception ex)
{
return new DataTable();
}
}
page_load 中的母版页代码是
if (!Page.IsPostBack)
{
SQLString.Config = myconnectionString;
}
这里NoProduct => No Products Are Available Right Now.
我尝试了很多类似以下的方法:
EnableViewState="true"和EnableViewState="false"用于转发器和页面中。-
更改 web.config 中的连接字符串,如
name="SQLConStr" connectionString="Server=localhost;Database=databasename; Uid = username; Pwd = password; Pooling = false; Connection Timeout = 30"
这里尝试了有无连接超时和池化。
注意:使用MySQL数据库并使用接口打开数据库连接。
提前致谢。
【问题讨论】:
-
理想时间?难道你的意思是“空闲”?
-
您提供的链接得到:“目前没有可用的产品。”。 EnableViewState 必须为真。你在 ImagePath 中得到了什么? “路径”中有什么?也许全局路径和路径之间可以缺少斜线?
-
@Emanuele 但是当第二次点击相同的链接时,它会加载图像..
-
您可以尝试直接从 web.config 使用 ConnectionString 或使用静态类但删除 page.ispostback 在母版页中包装连接字符串分配。
-
第一个 if(!Page.IsPostBack) 在哪里?负载?如果你调试,路径变量是否包含正确的值?
标签: c# mysql webforms bootstrapping