ItemDataBound / RowDataBound Difference

G

GaryDean

I'm up against something else I used to easily do in a DataGrid that is
different in a GridView....

I used to access cells in non-visible columns in the ItemDataBound event.
The data was there - the column just wasen't visible.
In the Gridview, if a column is not visible, the data is not there in the
cell in the RowdataBound event. This means we don't have the presentation
flexibility we used to have in the DataGrid, no?

Am I missing something? Is there some other way in which I can deal with
non visible data?
 
E

Eliyahu Goldin

Gary,

This didn't change from datagrid to gridvew. In both 1.1 and 2.0 any web
control with the Visible property set to false doesn't get rendered to the
client and doesn't come back in postbacks. If you are interested in getting
hidden values in postbacks, leave Visible=true and use css rule
display:none.
 
S

Steven Cheng[MSFT]

Hello Gary,

I think the behavior for InVisible columns in DataGrid and GridView control
should be identical. You can access them in ItemCreated(RowCreated) and
ItemDataBound(RowDataBound) event. I'm not sure what's the problem in your
certain page, one common issue is that we try finding control without first
detect the RowType(ItemType) of each Item/Row. Anyway, here is a test page
which works on my local side, you can also refer to it for your testing

===========aspx===================
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM
[Categories]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1"
OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:TemplateField HeaderText="HiddenColumn"
Visible="false">
said:
</asp:TextBox>
<asp:Button ID="btn1" runat="server" Text="Button1"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
&nbsp;<asp:DataGrid ID="DataGrid1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
OnItemCreated="DataGrid1_ItemCreated"
OnItemDataBound="DataGrid1_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="CategoryID"></asp:BoundColumn>
<asp:BoundColumn DataField="CategoryName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="HiddenColumn"
Visible="false" >
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:Button ID="btn2" runat="server" Text="Button"
/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></div>
</form>
==================================

==========codebehind=================
public partial class Part1_ASPSQLPAGE : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control txt = e.Row.FindControl("txt1");
Response.Write("<br/>GridView1_RowCreated:" + txt);
}
}

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control txt = e.Row.FindControl("txt1");
Response.Write("<br/>GridView1_RowDataBound:" + txt);
}
}

protected void DataGrid1_ItemCreated(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Control txt = e.Item.FindControl("txt2");
Response.Write("<br/>DataGrid1_ItemCreated:" + txt);
}
}
protected void DataGrid1_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Control txt = e.Item.FindControl("txt2");
Response.Write("<br/>DataGrid1_ItemDataBound: " + txt);
}
}
}
=================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

GaryDean

Maybe my recollection of non-visible columns being there at ItemDataBound
time was a false memory. I don't have time to go check back on my old code
right now but I solved the issue in the GrivView by controlling visibility
at the Cell level in the RowDataBound event of the Gridview (probably the
way I used to do it).

thanks for your help.
Gary Blakely

Steven Cheng said:
Hello Gary,

I think the behavior for InVisible columns in DataGrid and GridView
control
should be identical. You can access them in ItemCreated(RowCreated) and
ItemDataBound(RowDataBound) event. I'm not sure what's the problem in
your
certain page, one common issue is that we try finding control without
first
detect the RowType(ItemType) of each Item/Row. Anyway, here is a test page
which works on my local side, you can also refer to it for your testing

===========aspx===================
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM
[Categories]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1"
OnRowCreated="GridView1_RowCreated"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:TemplateField HeaderText="HiddenColumn"
Visible="false">
</asp:TextBox>
<asp:Button ID="btn1" runat="server" Text="Button1"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
&nbsp;<asp:DataGrid ID="DataGrid1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
OnItemCreated="DataGrid1_ItemCreated"
OnItemDataBound="DataGrid1_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="CategoryID"></asp:BoundColumn>
<asp:BoundColumn
DataField="CategoryName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="HiddenColumn"
Visible="false" >
<ItemTemplate>
<asp:TextBox ID="txt2"
runat="server"></asp:TextBox>
<asp:Button ID="btn2" runat="server" Text="Button"
/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></div>
</form>
==================================

==========codebehind=================
public partial class Part1_ASPSQLPAGE : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control txt = e.Row.FindControl("txt1");
Response.Write("<br/>GridView1_RowCreated:" + txt);
}
}

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control txt = e.Row.FindControl("txt1");
Response.Write("<br/>GridView1_RowDataBound:" + txt);
}
}

protected void DataGrid1_ItemCreated(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Control txt = e.Item.FindControl("txt2");
Response.Write("<br/>DataGrid1_ItemCreated:" + txt);
}
}
protected void DataGrid1_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Control txt = e.Item.FindControl("txt2");
Response.Write("<br/>DataGrid1_ItemDataBound: " + txt);
}
}
}
=================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no
rights.
 
S

Steven Cheng[MSFT]

Thanks for your followup Gary,

If you meet any other problems or anything else we can help, please feel
free to post here.

Good luck!

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top