开发者

When does a Gridview have a null 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">
   <Ite开发者_如何学GomTemplate>
   <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.


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.


what does your Page_load code look like?

GridView does not keep DataSource property populated over the postbacks for performance issues

Maybe the second gridview is rebinding the datasource on postback?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜