By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,018 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,018 IT Pros & Developers. It's quick & easy.

Objects disappear in TemplateFields on postback

P: n/a
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_SelectedIn dexChanged" 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

Apr 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <ro**************@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.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_SelectedIn dexChanged" 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

Apr 2 '07 #2

P: n/a
On Apr 2, 12:42 pm, "Mark Fitzpatrick" <markf...@fitzme.comwrote:
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.comwrote in message

news:11**********************@q75g2000hsh.googlegr oups.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_SelectedIn dexChanged" 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.Col umns[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

Apr 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.