asp.net ObjectDataSource如何访问System.Web.UI.Page对象 [英] asp.net how can ObjectDataSource access System.Web.UI.Page objects
问题描述
我使用ObjectDataSource如下.
I use ObjectDataSource as below.
<asp:ObjectDataSource ID="Item" runat="server"
SelectMethod="Grid_DataBind" TypeName="XXX.XXX.XXX"
DataObjectTypeName="Controller.Items" UpdateMethod="UpdateRow_Grid"
InsertMethod="InsertRow_Grid">
InsertMethod触发时,一切正常,但是...
When InsertMethod fire, everything work fine but ...
public IList<Items> InsertRow_Grid(Items item)
{
item.ID = System.Guid.NewGuid().ToString();
bool contains = GridSource.AsEnumerable()
.Any(row => item.JobID == row.JobID);
if (!contains)
{
GridSource.Add(item);
}
else
{
lblMsg.Text= "This record has already exists.";
}
return GridSource;
}
它不知道在aspx文件中显示的标签对象.
It doesn't know my label object which is presented in my aspx file.
我已阅读此,以便我可以找到合适的解决方案.
I had read this so that I can search proper solution.
但是我仍然不知道该怎么做.
But I still don't get how to do.
每个建议都会受到赞赏.
Every suggestion will be appreciated.
推荐答案
这是因为asp:ObjectDataSource创建了您在"TypeName"属性中指定的对象的新实例要使用当前页面对象而不是创建新页面对象,您需要以下代码:
This is because asp:ObjectDataSource creates new instance of object you specified in "TypeName" property To use current page object instead of creating new, you need this code:
YourObjectDataSource.ObjectCreating += (s, a) => { a.ObjectInstance = this; };
将其放入Page_Load或Page_Init
Place it in Page_Load or Page_Init
这篇关于asp.net ObjectDataSource如何访问System.Web.UI.Page对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!