DataGrid / Template columns

C

codputer

I have the following datagrid defined (subset of complete definition) where I
have placed a text box in the header template (unbounded). I'm using this
text box to capture filtering criteria relevant to that row. Everything
works great, but I loose the value of the textbox when the page is returned
to the client.

As this is an unbounded text box, I thought that the view state would retain
the text box value. I suspect that because the datagrid ItemTemplate is
bound however, the databind event clears the complete grid, regardless if
countrols or bound or not.

I've tried putting the value in after the databind event, but that doesn't
work. How can I keep the value of these textboxes from disappearing after a
postback? (I've marked the textboxes with ***, see below)

Thanks in advance for your time,
Richard

<form id="Form1" method="post" runat="server">
<P><uc1:bannercontrol id="BannerControl1"
runat="server"></uc1:bannercontrol></P>
<P><asp:datagrid id=BookingNavigatorDataGrid runat="server" Width="896px"
AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False"
DataKeyField="BookingID" DataSource="<%# BookingNavigatorDataView %>">
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn SortExpression="FullName" HeaderText="Patient Name">
<HeaderStyle Width="175px"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="lnkFullNameSort" runat="server" Width="114px"
CommandName="Sort" CommandArgument="FullName">Patient Name</asp:LinkButton>

***this is the text box I wish to preserve!****
<asp:TextBox id="txtFilterOn_FullName" runat="server"
OnTextChanged="txtFilterOn" BackColor="#C0FFFF" EnableViewState="True"
AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id=lnkPatientID runat="server" Width="170px"
CommandName="Select" CommandArgument="PatientInfo" Text='<%#
DataBinder.Eval(BookingNavigatorDataView, "[0].FullName") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.FullName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="HomePhone" HeaderText="Home Phone">
<HeaderStyle Width="125px"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="lnkHomePhoneSort" runat="server" Width="114px"
CommandArgument="HomePhone" CommandName="Sort">Home Phone</asp:LinkButton>

***this is the text box I wish to preserve!****
<asp:TextBox id="txtFilterOn_HomePhone" runat="server" Width="114px"
OnTextChanged="txtFilterOn" AutoPostBack="True"
BackColor="#C0FFFF"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id=lnkHomePhone runat="server" Width="154px"
CommandArgument="PatientInfo" CommandName="Select" Text='<%#
DataBinder.Eval(BookingNavigatorDataView, "[0].HomePhone") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox2 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.HomePhone") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
 
N

Navendu Kumar

Hi,

In the BookingNavigatorDataGrid_ItemDataBound event, write following code:

if (e.Item.ItemType = ListItemType.Header) then

' Find out the reference of the Text Box using FindControl and then paste
the value you want it to display in it.

end if

This might help.

Navendu





codputer said:
I have the following datagrid defined (subset of complete definition) where I
have placed a text box in the header template (unbounded). I'm using this
text box to capture filtering criteria relevant to that row. Everything
works great, but I loose the value of the textbox when the page is returned
to the client.

As this is an unbounded text box, I thought that the view state would retain
the text box value. I suspect that because the datagrid ItemTemplate is
bound however, the databind event clears the complete grid, regardless if
countrols or bound or not.

I've tried putting the value in after the databind event, but that doesn't
work. How can I keep the value of these textboxes from disappearing after a
postback? (I've marked the textboxes with ***, see below)

Thanks in advance for your time,
Richard

<form id="Form1" method="post" runat="server">
<P><uc1:bannercontrol id="BannerControl1"
runat="server"></uc1:bannercontrol></P>
<P><asp:datagrid id=BookingNavigatorDataGrid runat="server" Width="896px"
AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False"
DataKeyField="BookingID" DataSource="<%# BookingNavigatorDataView %>">
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn SortExpression="FullName" HeaderText="Patient Name">
<HeaderStyle Width="175px"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="lnkFullNameSort" runat="server" Width="114px"
CommandName="Sort" CommandArgument="FullName">Patient Name</asp:LinkButton>

***this is the text box I wish to preserve!****
<asp:TextBox id="txtFilterOn_FullName" runat="server"
OnTextChanged="txtFilterOn" BackColor="#C0FFFF" EnableViewState="True"
AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id=lnkPatientID runat="server" Width="170px"
CommandName="Select" CommandArgument="PatientInfo" Text='<%#
DataBinder.Eval(BookingNavigatorDataView, "[0].FullName") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.FullName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="HomePhone" HeaderText="Home Phone">
<HeaderStyle Width="125px"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="lnkHomePhoneSort" runat="server" Width="114px"
CommandArgument="HomePhone" CommandName="Sort">Home Phone</asp:LinkButton>

***this is the text box I wish to preserve!****
<asp:TextBox id="txtFilterOn_HomePhone" runat="server" Width="114px"
OnTextChanged="txtFilterOn" AutoPostBack="True"
BackColor="#C0FFFF"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id=lnkHomePhone runat="server" Width="154px"
CommandArgument="PatientInfo" CommandName="Select" Text='<%#
DataBinder.Eval(BookingNavigatorDataView, "[0].HomePhone") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox2 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.HomePhone") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
 
C

codputer

That worked great! I wonder though why this field is not restored via the
ViewState, as it is unbounded. I also wonder why the ItemDataBound event
occurs for an item that was not databound?

Thanks again,
Richard



Navendu Kumar said:
Hi,

In the BookingNavigatorDataGrid_ItemDataBound event, write following code:

if (e.Item.ItemType = ListItemType.Header) then

' Find out the reference of the Text Box using FindControl and then paste
the value you want it to display in it.

end if

This might help.

Navendu





codputer said:
I have the following datagrid defined (subset of complete definition) where I
have placed a text box in the header template (unbounded). I'm using this
text box to capture filtering criteria relevant to that row. Everything
works great, but I loose the value of the textbox when the page is returned
to the client.

As this is an unbounded text box, I thought that the view state would retain
the text box value. I suspect that because the datagrid ItemTemplate is
bound however, the databind event clears the complete grid, regardless if
countrols or bound or not.

I've tried putting the value in after the databind event, but that doesn't
work. How can I keep the value of these textboxes from disappearing after a
postback? (I've marked the textboxes with ***, see below)

Thanks in advance for your time,
Richard

<form id="Form1" method="post" runat="server">
<P><uc1:bannercontrol id="BannerControl1"
runat="server"></uc1:bannercontrol></P>
<P><asp:datagrid id=BookingNavigatorDataGrid runat="server" Width="896px"
AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False"
DataKeyField="BookingID" DataSource="<%# BookingNavigatorDataView %>">
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn SortExpression="FullName" HeaderText="Patient Name">
<HeaderStyle Width="175px"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="lnkFullNameSort" runat="server" Width="114px"
CommandName="Sort" CommandArgument="FullName">Patient Name</asp:LinkButton>

***this is the text box I wish to preserve!****
<asp:TextBox id="txtFilterOn_FullName" runat="server"
OnTextChanged="txtFilterOn" BackColor="#C0FFFF" EnableViewState="True"
AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id=lnkPatientID runat="server" Width="170px"
CommandName="Select" CommandArgument="PatientInfo" Text='<%#
DataBinder.Eval(BookingNavigatorDataView, "[0].FullName") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.FullName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="HomePhone" HeaderText="Home Phone">
<HeaderStyle Width="125px"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="lnkHomePhoneSort" runat="server" Width="114px"
CommandArgument="HomePhone" CommandName="Sort">Home Phone</asp:LinkButton>

***this is the text box I wish to preserve!****
<asp:TextBox id="txtFilterOn_HomePhone" runat="server" Width="114px"
OnTextChanged="txtFilterOn" AutoPostBack="True"
BackColor="#C0FFFF"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id=lnkHomePhone runat="server" Width="154px"
CommandArgument="PatientInfo" CommandName="Select" Text='<%#
DataBinder.Eval(BookingNavigatorDataView, "[0].HomePhone") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox2 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.HomePhone") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top