Help: GridView Control Causing 2 Postbacks on Selection

T

tacmec

ASP.NET 2.0 (C#) application. I have a web form with a GridView, which is
populated dynamically. See the code below.

First time to the page, IsPostBack is false. Therefore, DisplayItems() is
called and the GridView is populated. No problems.
When I click the select button in the GridView, I first go to Page_Load and
IsPostBack = true. Therefore, DisplayItems() is not called again. No
problems.
Then the GridViewMasterReports_SelectedIndexChanged event fires and does
it's stuff. No problems.
Then Page_Load is fired again and IsPostBack = true. Then the
GridViewMasterReports_SelectedIndexChanged event fires again. This is the
issue/question.

Why, when I click the select button of my GridView, are two postbacks
occurring and why are Page_Load and
GridViewMasterReports_SelectedIndexChanged fired twice?

Any thoughts?


..aspx
<asp:panel ID="PanelMasterReports" runat="server" ScrollBars="Auto">
<asp:GridView
ID="GridViewMasterReports"
runat="server"
BackColor="White"
BorderColor="#999999"
BorderStyle="None"
BorderWidth="1px"
CaptionAlign="Top"
CellPadding="3"
GridLines="Vertical"
OnSelectedIndexChanged="GridViewMasterReports_SelectedIndexChanged"
ToolTip="Master Reports"
Width="100%">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center"
/>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#F5F5DC" Font-Bold="True" ForeColor="Black"
/>
<Columns>
<asp:CommandField ShowSelectButton="True" ButtonType="Image"
SelectImageUrl="~/Images/16gosearch.gif" ShowCancelButton="False" />
</Columns>
</asp:GridView>
</asp:panel>


..aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Display the reports
DisplayItems();
}
}

private void DisplayItems()
{
System.Data.DataTable tbl = new System.Data.DataTable("MasterReports");
System.Data.DataColumn colName = tbl.Columns.Add("Name", typeof(string));
System.Data.DataColumn colPath = tbl.Columns.Add("Path", typeof(string));

...

foreach (object in collection)
{
// Build a datatable
System.Data.DataRow row = tbl.NewRow();
row["Name"] = item.Name;
row["Path"] = item.Path;
tbl.Rows.Add(row);
}

this.GridViewMasterReports.DataSource = tbl;
this.GridViewMasterReports.DataBind();
}

protected void GridViewMasterReports_SelectedIndexChanged(object sender,
EventArgs e)
{
// Do stuff here
}
 
C

chris

Could it be the AutoEventWireup for the page is set to true? I think
may cause this problem sometimes. I might be mistaken though.

Chris
 
A

Anthony.Griff

Hi,

If you use the image button the Page_Load get fired twice.

If you use any other type i.e. link or button then it fires only once.

It took a while to figure this out but there you go. Looks like a but
to me!!!
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top