Board index » Web Programming » Objects disappear in TemplateFields on postback

Objects disappear in TemplateFields on postback

Web Programming171
I've got a gridview with BoundFields and TemplateFields and a

linkbutton with a CommandName="Select". When I click on this

linkbutton the TemplateField objects disappear on postback but the

BoundFields still have their data. Also the SelectedIndexChanged

event is never hit. Why is this happening?



-----------------------------------

<asp:GridView ID="gvSearchResults" runat="server"

AutoGenerateColumns="false" AutoGenerateSelectButton="false"

GridLines="none" DataKeyNames="entity_uid" SelectedIndex="0"

OnRowDataBound="gvSearchResults_OnRowDataBound"

OnSelectedIndexChanged="gvSearchResults_SelectedIndexChanged" Font-

Size="x-Small" CellSpacing="1">

<Columns>



<asp:TemplateField>

<ItemTemplate>

<asp:LinkButton ID="lbDetail" runat="server"

CommandName="Select" Text="View Detail" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblRecordInfo" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:Image ID="imgPhoto" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField ReadOnly="true" HeaderText="FName"

DataField="FName" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="LName"

DataField="LName" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="GradYear"

DataField="GradYearDegree" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="&nbsp;Cohort&nbsp;"

DataField="cohort" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="DegreePrgoram"

DataField="DegreeProgram" HeaderStyle-Font-Bold="true" />

<asp:TemplateField HeaderText="PrefEmail" HeaderStyle-Font-

Bold="true">

<ItemTemplate>

<asp:HyperLink ID="hplPrefEmail" runat="server"

ForeColor="darkBlue" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<AlternatingRowStyle BackColor="white" />

<RowStyle BackColor="beige" />

</asp:GridView>

----------------------------------------------------------------



protected void Page_Load(object sender, EventArgs e)

{



if (!IsPostBack)

{

DataSet dsSearchResults =

SQLRoutines.SearchRecords(P);

Trace.Write("Count:",

dsSearchResults.Tables[0].Rows.Count.ToString());

gvSearchResults.DataSource = dsSearchResults;

gvSearchResults.DataBind();

}



}





protected void gvSearchResults_SelectedIndexChanged(object sender,

EventArgs e)

{

SelectedUID = (string)gvSearchResults.SelectedValue;

Trace.Write("SelectedIndexChanged", SelectedUID);

Server.Transfer("SearchResultsBio.asp", true);

}



Thanks,

Robert


-
 

Re:Objects disappear in TemplateFields on postback

First, you have to create the objects early enough. The SelectedIndexChanged

event is not firing because the object doesn't exist yet. You have to call

your databinding code much earlier. You can try the OnInit event, or the

OnPreLoad event insteat. Page Load is just too late in the scheme of things

as it occurs after postback. You'll need to eliminate the test for postback,

the controls will need to be recreated each time.





--

Hope this helps,

Mark Fitzpatrick

Former Microsoft FrontPage MVP 199?-2006



"Robert Fernandez" <robert.fernandez@gmail.com>wrote in message

Quote
I've got a gridview with BoundFields and TemplateFields and a

linkbutton with a CommandName="Select". When I click on this

linkbutton the TemplateField objects disappear on postback but the

BoundFields still have their data. Also the SelectedIndexChanged

event is never hit. Why is this happening?



-----------------------------------

<asp:GridView ID="gvSearchResults" runat="server"

AutoGenerateColumns="false" AutoGenerateSelectButton="false"

GridLines="none" DataKeyNames="entity_uid" SelectedIndex="0"

OnRowDataBound="gvSearchResults_OnRowDataBound"

OnSelectedIndexChanged="gvSearchResults_SelectedIndexChanged" Font-

Size="x-Small" CellSpacing="1">

<Columns>



<asp:TemplateField>

<ItemTemplate>

<asp:LinkButton ID="lbDetail" runat="server"

CommandName="Select" Text="View Detail" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblRecordInfo" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:Image ID="imgPhoto" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField ReadOnly="true" HeaderText="FName"

DataField="FName" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="LName"

DataField="LName" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="GradYear"

DataField="GradYearDegree" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="&nbsp;Cohort&nbsp;"

DataField="cohort" HeaderStyle-Font-Bold="true" />

<asp:BoundField ReadOnly="true" HeaderText="DegreePrgoram"

DataField="DegreeProgram" HeaderStyle-Font-Bold="true" />

<asp:TemplateField HeaderText="PrefEmail" HeaderStyle-Font-

Bold="true">

<ItemTemplate>

<asp:HyperLink ID="hplPrefEmail" runat="server"

ForeColor="darkBlue" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<AlternatingRowStyle BackColor="white" />

<RowStyle BackColor="beige" />

</asp:GridView>

----------------------------------------------------------------



protected void Page_Load(object sender, EventArgs e)

{



if (!IsPostBack)

{

DataSet dsSearchResults =

SQLRoutines.SearchRecords(P);

Trace.Write("Count:",

dsSearchResults.Tables[0].Rows.Count.ToString());

gvSearchResults.DataSource = dsSearchResults;

gvSearchResults.DataBind();

}



}





protected void gvSearchResults_SelectedIndexChanged(object sender,

EventArgs e)

{

SelectedUID = (string)gvSearchResults.SelectedValue;

Trace.Write("SelectedIndexChanged", SelectedUID);

Server.Transfer("SearchResultsBio.asp", true);

}



Thanks,

Robert







-

Re:Objects disappear in TemplateFields on postback

On Apr 2, 12:42 pm, "Mark Fitzpatrick" <markf...@fitzme.com>wrote:

Quote
First, you have to create the objects early enough. The SelectedIndexChanged

event is not firing because the object doesn't exist yet. You have to call

your databinding code much earlier. You can try the OnInit event, or the

OnPreLoad event insteat. Page Load is just too late in the scheme of things

as it occurs after postback. You'll need to eliminate the test for postback,

the controls will need to be recreated each time.



--

Hope this helps,

Mark Fitzpatrick

Former Microsoft FrontPage MVP 199?-2006



"Robert Fernandez" <robert.fernan...@gmail.com>wrote in message



news:1175530753.076521.314290@q75g2000hsh.googlegroups.com...







>I've got a gridview with BoundFields and TemplateFields and a

>linkbutton with a CommandName="Select". When I click on this

>linkbutton the TemplateField objects disappear on postback but the

>BoundFields still have their data. Also the SelectedIndexChanged

>event is never hit. Why is this happening?



>-----------------------------------

><asp:GridView ID="gvSearchResults" runat="server"

>AutoGenerateColumns="false" AutoGenerateSelectButton="false"

>GridLines="none" DataKeyNames="entity_uid" SelectedIndex="0"

>OnRowDataBound="gvSearchResults_OnRowDataBound"

>OnSelectedIndexChanged="gvSearchResults_SelectedIndexChanged" Font-

>Size="x-Small" CellSpacing="1">

><Columns>



><asp:TemplateField>

><ItemTemplate>

><asp:LinkButton ID="lbDetail" runat="server"

>CommandName="Select" Text="View Detail" />

></ItemTemplate>

></asp:TemplateField>

><asp:TemplateField>

><ItemTemplate>

><asp:Label ID="lblRecordInfo" runat="server" />

></ItemTemplate>

></asp:TemplateField>

><asp:TemplateField>

><ItemTemplate>

><asp:Image ID="imgPhoto" runat="server" />

></ItemTemplate>

></asp:TemplateField>

><asp:BoundField ReadOnly="true" HeaderText="FName"

>DataField="FName" HeaderStyle-Font-Bold="true" />

><asp:BoundField ReadOnly="true" HeaderText="LName"

>DataField="LName" HeaderStyle-Font-Bold="true" />

><asp:BoundField ReadOnly="true" HeaderText="GradYear"

>DataField="GradYearDegree" HeaderStyle-Font-Bold="true" />

><asp:BoundField ReadOnly="true" HeaderText="&nbsp;Cohort&nbsp;"

>DataField="cohort" HeaderStyle-Font-Bold="true" />

><asp:BoundField ReadOnly="true" HeaderText="DegreePrgoram"

>DataField="DegreeProgram" HeaderStyle-Font-Bold="true" />

><asp:TemplateField HeaderText="PrefEmail" HeaderStyle-Font-

>Bold="true">

><ItemTemplate>

><asp:HyperLink ID="hplPrefEmail" runat="server"

>ForeColor="darkBlue" />

></ItemTemplate>

></asp:TemplateField>

></Columns>

><AlternatingRowStyle BackColor="white" />

><RowStyle BackColor="beige" />

></asp:GridView>

>----------------------------------------------------------------



>protected void Page_Load(object sender, EventArgs e)

>{



>if (!IsPostBack)

>{

>DataSet dsSearchResults =

>SQLRoutines.SearchRecords(P);

>Trace.Write("Count:",

>dsSearchResults.Tables[0].Rows.Count.ToString());

>gvSearchResults.DataSource = dsSearchResults;

>gvSearchResults.DataBind();

>}



>}



>protected void gvSearchResults_SelectedIndexChanged(object sender,

>EventArgs e)

>{

>SelectedUID = (string)gvSearchResults.SelectedValue;

>Trace.Write("SelectedIndexChanged", SelectedUID);

>Server.Transfer("SearchResultsBio.asp", true);

>}



>Thanks,

>Robert- Hide quoted text -



- Show quoted text -



Mark,



Thanks I got it to work. I had been dynamically removing columns by

doing

code like the following:



gvSearchResults.Columns.Remove(gvSearchResults.Columns[51]);



before doing the databind. I moved this logic to after the databind

and the SelectedIndexChanged event fired and the page works now. I

also decided to do



gvSearchResults.Columns[51].Visible = false;



which is easier to work with.



Thanks!

Happy Monday,

Robert



-