Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Help: GridView Control Causing 2 Postbacks on Selection

Reply
Thread Tools

Help: GridView Control Causing 2 Postbacks on Selection

 
 
tacmec
Guest
Posts: n/a
 
      01-31-2006
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_Sele ctedIndexChanged"
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
}



 
Reply With Quote
 
 
 
 
chris@cubed-c.com
Guest
Posts: n/a
 
      01-31-2006
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

 
Reply With Quote
 
 
 
 
Anthony.Griff@gmail.com
Guest
Posts: n/a
 
      02-17-2006
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!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio Button Column In GridView Causing Postbacks In Ajax tristan_sehgal@yahoo.co.uk ASP .Net 0 03-23-2009 09:51 AM
GridView databinding twice on postbacks J055 ASP .Net 11 01-15-2009 09:35 AM
GridView's selection isn't causing other GridView's to load data asexpected Rasika WIJAYARATNE ASP .Net 0 12-14-2007 12:25 AM
User control is GridView causing Object doesn't support this property or method exception Joe ASP .Net 3 12-10-2007 01:03 AM
AJAX - Partial Postbacks are now Full Postbacks ?!! gregfocker@earthlink.net ASP .Net 0 01-25-2007 07:04 AM



Advertisments