Gridview何时具有null DataSource?(When does a Gridview have a null DataSource?)

编程入门 行业动态 更新时间:2024-10-27 04:32:11
Gridview何时具有null DataSource?(When does a Gridview have a null DataSource?)

我读过多个来源,说Gridview不会在回发上保留Gridview.DataSource属性。 我的理解是,在ASP.NET的术语中,回发是任何不是第一个页面加载的页面加载(参见MSDN )。

我有一个2非常相似的网格视图的情况。

回发时GvOne.DataSource为null。 回发时GvTwo.DataSource不为null。

除了几个不同的列之外唯一的重大区别是GvOne填充了Entity Framework和LINQ。 GvTwo由一个由SqlDataAdapter填充的DataTable填充。

此外,GvOne和GvTwo有一个带有TextBox的TemplateField,我用它来收集用户输入。 两者都使用相同的代码在回发时拉动TextBox.Text:

TextBox tb = (TextBox)GvOne.Rows[i].FindControl("actualTxt");

GvOne正确地收集了tb.Text。 GvTwo 总是发现tb.Text值为0

基本Gridview代码:

<asp:GridView ID="GvOne" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Return"> <ItemTemplate> <asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox> </ItemTemplate> </asp:TemplateField> ... </Columns> </asp:GridView> <asp:GridView ID="GvTwo" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Order"> <ItemTemplate> <asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox> </ItemTemplate> </asp:TemplateField> ... </Columns> </asp:GridView>

将GvTwo更改为使用Entity Framework和LINQ是一种潜在的解决方案,尽管这是一项重大任务。 有谁知道这里发生了什么?

更新 (请参阅我对Joel Etherton的答案的评论)由于受欢迎的需求,这里是为GvTwo填充Page_Load事件中的gridview的代码(GvOne类似):

ordersGV.DataSource = dataSetObject.Tables["activeParts"]; ordersGV.DataBind();

搜索后面的代码我发现没有其他对ordersGv.Datasource的引用,也没有其他与页面生命周期相关联的事件。

I've read multiple sources that say Gridview's do not persist the Gridview.DataSource property on postback. My understanding is that in term's of ASP.NET, a postback is any page load that is not the first pageload (see MSDN).

I've got a situation with 2 very similar gridviews.

GvOne.DataSource is null on postback. GvTwo.DataSource is NOT null on postback.

The only big difference outside of a few differing columns is GvOne is populated with the Entity Framework and LINQ. GvTwo is populated by a DataTable filled by a SqlDataAdapter.

Further, GvOne and GvTwo have a TemplateField with a TextBox that I use to gather user input. Both use the same code to pull the TextBox.Text on postback:

TextBox tb = (TextBox)GvOne.Rows[i].FindControl("actualTxt");

GvOne properly gathers tb.Text. GvTwo always finds the tb.Text value to be 0.

Basic Gridview code:

<asp:GridView ID="GvOne" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Return"> <ItemTemplate> <asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox> </ItemTemplate> </asp:TemplateField> ... </Columns> </asp:GridView> <asp:GridView ID="GvTwo" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Order"> <ItemTemplate> <asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox> </ItemTemplate> </asp:TemplateField> ... </Columns> </asp:GridView>

Changing GvTwo to use Entity Framework and LINQ is a potential solution, albeit a major undertaking. Does anyone know what is going on here?

UPDATE (See my comment on Joel Etherton's Answer) Due to popular demand here is the code to populate the gridview within Page_Load event for GvTwo (GvOne is similar):

ordersGV.DataSource = dataSetObject.Tables["activeParts"]; ordersGV.DataBind();

Searching through the code behind I found no other references to ordersGv.Datasource and no other events that are hooked into associated with the page life cycle.

最满意答案

Gridview不会在回发之间保留数据源。 如果您的gridview具有非null数据源,那么您必须在代码中的某处填充该数据源。 通过您的事件周期来查找回发时数据源的确切位置,这将是有益的。

Gridviews do not persist the datasource across postbacks. If you have a gridview that has a non-null datasource then you must be filling that datasource somewhere in your code. It would be instructive to travel through your event cycle to find where exactly the population of the datasource is occuring on postback.

更多推荐

本文发布于:2023-08-07 04:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Gridview   null   DataSource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!