Eval()显示空值

时间:2012-03-09 06:10:59

标签: asp.net .net

我使用<%#Eval("sectionId") %>数据绑定表达式将数据绑定到我的代码。 我正在使用以下代码

在我的代码behinde中设置此sectionId
public partial class ProductDetails : System.Web.UI.Page
    {
        private string sectionId = string.Empty;           

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString.Count > 0)
                {
                    if (Request.QueryString["secId"] != null || Request.QueryString["prodId"] != null)
                    {
                        sectionId = Request.QueryString["secId"].ToString();

                    }
                }
            }
        }
    }

在我的.aspx页面中,我有这样的代码,

<a href="SectionWiseProduct.aspx?secId=<%#Eval("sectionId") %>">Enviro Section</a>

每次sectionId的值都在代码behinde中成功设置,但它没有反映在UI页面中。每次我都得到链接,

SectionWiseProduct.aspx?secId=

任何人都可以建议我做得对,还是有其他办法做到这一点。 并且page_Load调用了两次,这是因为Eval?

3 个答案:

答案 0 :(得分:0)

在您的代码中进行更改

public string sectionId = string.Empty;   

希望这会有所帮助!!!

答案 1 :(得分:0)

最小化对DataBinder.Eval的调用

DataBinder.Eval方法使用反射来计算传入的参数并返回结果。如果您有一个包含100行和10列的表,如果在每列上使用DataBinder.Eval,则调用DataBinder.Eval 1,000次。在这种情况下,您选择使用DataBinder.Eval乘以1,000次。在数据绑定操作期间限制DataBinder.Eval的使用可显着提高页面性能。使用DataBinder.Eval考虑Repeater控件中的以下ItemTemplate元素。

<ItemTemplate>
  <tr>
    <td><%# DataBinder.Eval(Container.DataItem,"field1") %></td>
    <td><%# DataBinder.Eval(Container.DataItem,"field2") %></td>
  </tr>
</ItemTemplate>

在这种情况下,还有其他方法可以使用DataBinder.Eval。替代方案包括以下内容:

使用显式强制转换。使用显式铸造可以避免反射成本,从而提供更好的性能。将Container.DataItem转换为DataRowView。

<ItemTemplate>
  <tr>
    <td><%# ((DataRowView)Container.DataItem)["field1"] %></td>
    <td><%# ((DataRowView)Container.DataItem)["field2"] %></td>
  </tr>
</ItemTemplate>

如果使用DataReader绑定控件并使用专门方法检索数据,则可以通过显式转换获得更好的性能。将Container.DataItem转换为DbDataRecord。

<ItemTemplate>
  <tr>
     <td><%# ((DbDataRecord)Container.DataItem).GetString(0) %></td>
     <td><%# ((DbDataRecord)Container.DataItem).GetInt(1) %></td>
  </tr>
</ItemTemplate>

显式转换取决于您绑定的数据源的类型;上面的代码说明了一个例子。

使用ItemDataBound事件。如果正在数据绑定的记录包含许多字段,则使用ItemDataBound事件可能更有效。通过使用此事件,您只需执行一次类型转换。以下示例使用DataSet对象。

protected void Repeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
  DataRowView drv = (DataRowView)e.Item.DataItem;
  Response.Write(string.Format("<td>{0}</td>",drv["field1"]));
  Response.Write(string.Format("<td>{0}</td>",drv["field2"]));
  Response.Write(string.Format("<td>{0}</td>",drv["field3"]));
  Response.Write(string.Format("<td>{0}</td>",drv["field4"]));
}

答案 2 :(得分:0)

最后我得到了解决方案

只需使用

<%=sectionId %>

insted of

<%#Eval("sectionId") %>

但是每次在页面加载时都会刷新值,你只需要另外处理它,它对我的​​工作正常:)

向其他人提供帮助。

相关问题