Help: GridView Control Causing 2 Postbacks on Selection

Discussion in 'ASP .Net' started by tacmec, Jan 31, 2006.

  1. tacmec

    tacmec Guest

    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
    }
    tacmec, Jan 31, 2006
    #1
    1. Advertising

  2. tacmec

    Guest

    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
    , Jan 31, 2006
    #2
    1. Advertising

  3. tacmec

    Guest

    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!!!
    , Feb 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,453
    Christian Kaufhold
    Jan 7, 2005
  2. Replies:
    0
    Views:
    436
  3. Joe
    Replies:
    3
    Views:
    546
  4. Rasika WIJAYARATNE
    Replies:
    0
    Views:
    602
    Rasika WIJAYARATNE
    Dec 14, 2007
  5. Replies:
    0
    Views:
    1,443
Loading...

Share This Page