Objects disappear in TemplateFields on postback

  • Thread starter Robert Fernandez
  • Start date
R

Robert Fernandez

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
 
M

Mark Fitzpatrick

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 said:
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
 
R

Robert Fernandez

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




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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top