- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 LinqDataSource 和在 ASP.NET 页面上启用分页的 FormView。我正在尝试访问 PageLoad
上的 FormView 的 DataItem
属性,我在第一页加载时没有遇到任何问题,但是一旦我使用 Next/Prev 页面按钮(导致一个回发)在 FormView 上 DataItem
属性为 null,即使在 FormView 中显示了一条记录。知道为什么它在第一页加载时工作正常但在回发时却不行吗?
如果您对我的 PageLoad
事件感到好奇,请看这里:
protected void Page_Load(object sender, EventArgs e)
{
Label lbl = (Label)fvData.FindControl("AREALabel");
if (fvData.DataItem != null && lbl != null)
{
INSTRUMENT_LOOP_DESCRIPTION record = (INSTRUMENT_LOOP_DESCRIPTION)fvData.DataItem;
var area = db.AREAs.SingleOrDefault(q => q.AREA1 == record.AREA);
if (area != null)
lbl.Text = area.AREA_NAME;
}
}
最佳答案
您绑定(bind)到任何数据绑定(bind)控件的对象不会保留在页面的 ViewState 中
因此,在后续的帖子中,DataItem
属性将为空,除非您重新绑定(bind)控件
绑定(bind)控件时,此属性将包含对该对象的引用。
如果您想在绑定(bind)对象时执行某些操作,通常您需要访问此属性,因此您需要对DataBound
事件使用react
例子:
protected void ds_DataBound(object sender, EventArgs e)
{
var d = this.fv.DataItem as employee;
this.lbl.Text = d.lname;
}
<asp:LinqDataSource ID="lds" runat="server"
ContextTypeName="DataClassesDataContext"
TableName="employees"
>
</asp:LinqDataSource>
<asp:FormView runat="server" ID="fv" DataSourceID="lds" AllowPaging="true"
OnDataBound="ds_DataBound">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("fname") %>' runat="server" ID="txt" />
</ItemTemplate>
</asp:FormView>
<br />
<asp:Label ID="lbl" runat="server" />
关于c# - FormView.DataItem 在回发时为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11547125/
我有一个里面 在 GridView 中:
两个问题: 在回发时,当用户单击“提交”时,如何将错误消息添加到验证摘要中? 是否也可以使用内置的 .net 验证控件突出显示特定文本框? 最佳答案 动态创建 CustomValidator 控件并将
我的 Controller 用一个列表填充我的模型,该列表包含出现在我 View 中的 DropDownList 中的 strings。当 View 回发到我的 Controller 时,该列表突然为
我创建了两个输入框,并设置了 JavaScript,以便在达到最大长度时自动聚焦下一个框。它工作正常。但是当触发任何回发事件时,这些值变得清晰。我将 runat="server"放在输入框上,然后输入
我正在开发 asp.net 4.0(Web 表单),每当页面回发以验证凭据时,我都会在登录页面上遇到错误,它会显示以下错误。 Sys.WebForms.PageRequestManagerServer
我目前正在开发一个在 Sharepoint 中运行的 ASP.Net 网站。在解决问题之前,让我先给您一些背景信息。 我开发了 UserControl,然后将其添加为共享点页面上的 Web 部件。其中
我是一名优秀的程序员,十分优秀!