Must click twice on datagrid to make drid row editable

Discussion in 'ASP .Net' started by Diane, Nov 8, 2005.

  1. Diane

    Diane Guest

    Hi-

    I've been struggling with this problem and none of the fixes posted
    seem to help out at all. Yet, it seems like such a simple problem...

    I have a DataGrid, and load it up with data on page load. In my
    EditCommand event handler I set the editItemindex. All good. Except,
    the user needs to click twice to get the DataGrid to become editable.

    It's obvious to me (perhaps worngly) that ASP.NET is storing the data
    form my grid in the viewstate, and setting up the datagrid BEFORE the
    Edit button event. That's why the grid appears editable the next page
    load.

    So, how do I refresh the grid to show the editability? Predictably,
    calling DataBind makes all the data go away... String the data set in
    the session seems redundantg, since the page is definitely storing the
    data for me in the viewstate. Not that I can get to it...

    Any ideas?

    Diane

    <PRE>
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (this.IsPostBack == false)
    {
    sqlConnection1.ConnectionString = "..pubs..";
    string query = "SELECT * FROM stores";
    this.sqlDataAdapter1.SelectCommand.CommandText = query;

    this.sqlDataAdapter1.SelectCommand.Connection =
    sqlConnection1;


    this.sqlConnection1.Open();
    this.sqlDataAdapter1.Fill(this.dataSet1, "storesAll");
    this.DataGrid1.DataSource =
    this.dataSet1.Tables["storesAll"];
    this.DataGrid1.DataBind();


    this.sqlConnection1.Close();
    }



    }


    private void DataGrid1_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex = e.Item.ItemIndex;

    }
     
    Diane, Nov 8, 2005
    #1
    1. Advertising

  2. Diane,

    I'm not certain but this may be a different problem all together. I've
    noticed this type of behaviour with dynamically created controls. If your
    datagrid is being created dynamically or is part of a user control being
    placed on a main page dynamically then try this:

    Give your control an id when it's created. E.g. if this is a dynamic user
    control in the control's page load routine put: Me.Id = "MyDynamicControl1"

    Something in the framework looks for a control's user id to hook up the code
    and doesn't work the first time through if that id isn't there... I'm still
    not positive what causes the behaviour though, but giving it the Id fixes
    it.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Diane" <> wrote in message
    news:...
    > Hi-
    >
    > I've been struggling with this problem and none of the fixes posted
    > seem to help out at all. Yet, it seems like such a simple problem...
    >
    > I have a DataGrid, and load it up with data on page load. In my
    > EditCommand event handler I set the editItemindex. All good. Except,
    > the user needs to click twice to get the DataGrid to become editable.
    >
    > It's obvious to me (perhaps worngly) that ASP.NET is storing the data
    > form my grid in the viewstate, and setting up the datagrid BEFORE the
    > Edit button event. That's why the grid appears editable the next page
    > load.
    >
    > So, how do I refresh the grid to show the editability? Predictably,
    > calling DataBind makes all the data go away... String the data set in
    > the session seems redundantg, since the page is definitely storing the
    > data for me in the viewstate. Not that I can get to it...
    >
    > Any ideas?
    >
    > Diane
    >
    > <PRE>
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (this.IsPostBack == false)
    > {
    > sqlConnection1.ConnectionString = "..pubs..";
    > string query = "SELECT * FROM stores";
    > this.sqlDataAdapter1.SelectCommand.CommandText = query;
    >
    > this.sqlDataAdapter1.SelectCommand.Connection =
    > sqlConnection1;
    >
    >
    > this.sqlConnection1.Open();
    > this.sqlDataAdapter1.Fill(this.dataSet1, "storesAll");
    > this.DataGrid1.DataSource =
    > this.dataSet1.Tables["storesAll"];
    > this.DataGrid1.DataBind();
    >
    >
    > this.sqlConnection1.Close();
    > }
    >
    >
    >
    > }
    >
    >
    > private void DataGrid1_EditCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
    >
    > }
    >
     
    S. Justin Gengo, Nov 8, 2005
    #2
    1. Advertising

  3. Diane

    Diane Guest

    S Justin -

    In my case, the DataGrid is created at design time, not dynamically.
    So it already has an ID. My guess is that it is a timing issue: the
    button click event happens after the DataGrid has been bound already,
    and so the effects don;t show up til the next page load...

    Thanks,
    Diane
     
    Diane, Nov 8, 2005
    #3
  4. Diane,

    You should only need to bind your datagrid on pageload:

    If Not IsPostBack Then
    '---First page load
    '---Call routine to bind grid
    Else
    '---Post back only
    End If

    Then, as long as viewstate is on for the grid your button should work the
    first time.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Diane" <> wrote in message
    news:...
    >S Justin -
    >
    > In my case, the DataGrid is created at design time, not dynamically.
    > So it already has an ID. My guess is that it is a timing issue: the
    > button click event happens after the DataGrid has been bound already,
    > and so the effects don;t show up til the next page load...
    >
    > Thanks,
    > Diane
    >
     
    S. Justin Gengo, Nov 8, 2005
    #4
  5. Diane

    Diane Guest

    Justin-

    Yes, I agree, if the viewstate is on the Edit button should work the
    first time. That is the behavior I would expect, which makes it all
    the more puzzling that things don't behave that way.

    I don't suppose I can expect my users to click twice on the edit
    button, so I guess I will have to settle for making another trip to the
    databse in the event handler for the edit command. It would be cool to
    be able to work with disconnected data until the update command, but
    there seems to be no solution to this problem. Oh well, technology
    can't be perfect.

    Thanks for the support!
    Diane
     
    Diane, Nov 8, 2005
    #5
  6. Diane

    Diane Guest

    Justin-

    Well, I've looked around at lots of examples on MS and other sites, and
    everyone seems to load up the data from the DB again after the
    EditCommand, or they call the Page DataBind method (which in my case
    makes all the data dissapear, as I would expect. Not sure how folks
    get it to behave otherwise).

    Anyhow, it looks like in posted examples, people avoid this issue be
    making an extra trip to the DB, or by saving the datagrid the session.

    Tantalizingly, though, it almost works without a second trip to the DB.


    Sigh,

    Diane
     
    Diane, Nov 8, 2005
    #6
  7. Diane,

    Too bad you didn't get it working with a single trip.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Diane" <> wrote in message
    news:...
    > Justin-
    >
    > Well, I've looked around at lots of examples on MS and other sites, and
    > everyone seems to load up the data from the DB again after the
    > EditCommand, or they call the Page DataBind method (which in my case
    > makes all the data dissapear, as I would expect. Not sure how folks
    > get it to behave otherwise).
    >
    > Anyhow, it looks like in posted examples, people avoid this issue be
    > making an extra trip to the DB, or by saving the datagrid the session.
    >
    > Tantalizingly, though, it almost works without a second trip to the DB.
    >
    >
    > Sigh,
    >
    > Diane
    >
     
    S. Justin Gengo, Nov 9, 2005
    #7
  8. Diane

    Jon Paal Guest

    This article may explain the problem you are having

    http://west-wind.com/weblog/posts/3016.aspx





    -----------------------------------------------------------
    I would much rather chew on tinfoil than try and code in C#.
     
    Jon Paal, Nov 9, 2005
    #8
    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. mike parr
    Replies:
    1
    Views:
    534
    Marina
    May 20, 2004
  2. Angela Chen
    Replies:
    3
    Views:
    9,109
    parshuram
    Dec 6, 2004
  3. Luis Esteban Valencia

    Make a DataGrid row editable after user selects the row

    Luis Esteban Valencia, Jan 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    363
    Luis Esteban Valencia
    Jan 12, 2005
  4. Diane
    Replies:
    5
    Views:
    291
    alvinz_c
    Nov 9, 2005
  5. Jon Prisbe

    must click button twice for event to fire

    Jon Prisbe, Apr 10, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    161
    Phillip Williams
    Apr 10, 2006
Loading...

Share This Page