2012-03-09 180 views
0

我正在使用<%#Eval("sectionId") %>数据绑定表达式将数据绑定到我的代码前置。 我在我的代码behinde使用下面的代码设置此sectionId,Eval()显示空值

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 page.every时间我得到喜欢的链接,

SectionWiseProduct.aspx?secId= 

任何人可以请建议是我这样做对还是我有没有其他的方式来做到这一点。 而且page_Load会调用两次,这是因为Eval吗?

回答

0

,让您的代码的变化,将工作

public string sectionId = string.Empty; 

希望这有助于!

0

最小化呼叫至的DataBinder.Eval

的方法DataBinder.Eval方法使用反射,以评估在传递和返回结果的争论。如果您有一个包含100行和10列的表,则如果您在每列上使用DataBinder.Eval,则调用DataBinder.Eval 1000次。在这种情况下,您使用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"])); 
}