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
<aspanel 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>
</aspanel>
..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
}
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
<aspanel 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>
</aspanel>
..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
}