Solved

gridview update (find control)

Posted on 2008-11-03
28
2,211 Views
Last Modified: 2013-12-17
I have place an edit/update button on my gridview and I am using the following code to pickup any changes upon editting/updating.

Location_Type = ((TextBox)user_records.Rows[e.RowIndex].FindControl("LocationTypetb")).Text;

The problem is  the code does not read the changes made to the content of the gridview, rather it returns the gridview orginal values.

0
Comment
Question by:Mr_Shaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 12
  • 2
28 Comments
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22865811
Hi,
You should place you code in the gridview's RowEditing or RowUpdating event handlers, which have an e parameter of type GridViewEditEventArgs or GridViewUpdateEventArgs, respectively.
Acessing e properties will give you the old and new values.
0
 

Author Comment

by:Mr_Shaw
ID: 22865820
at the moment it is located in GridViewUpdate
0
 
LVL 7

Expert Comment

by:moseack
ID: 22865835
You seem to be using a valid approach, but remember that if you are doing a data re-bind then you new values are overwritten with the old values.
Make sure you have the if(IsPostback) clause.

if (!IsPostback)
{
  // Do the Databind dance...
}

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22865836
Sorry, but there is no such event handler in a grid view. Can you please recheck?
0
 

Author Comment

by:Mr_Shaw
ID: 22865859
ok I have changed my code to this

    protected void edit_record_updating(object sender, GridViewUpdateEventArgs e)
    {

        update_records_method.Record_ID = ((TextBox)user_records.Rows[e.RowIndex].FindControl("RecordIDtb")).Text.ToString();
        update_records_method.Logon_Name = WindowsIdentity.GetCurrent().Name.ToString();
        update_records_method.Record_Class = ((TextBox)user_records.Rows[e.RowIndex].FindControl("RecordClasstb")).Text.ToString();

  if (!Page.IsPostBack)
        {
         BindData();   // Do the Databind dance...
        }

}

Is this correct
0
 
LVL 7

Expert Comment

by:moseack
ID: 22865899
Not excatly, what I ment.
You need to do this ONLY If you already have Data Binding in any of the previous Event handlers (Page_Init or Page_Load), similar to:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    BindData();   // Do the Databind dance...
  }
}

Open in new window

0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22865900
If all these TextBoxes are part of the row you are updating, you should get the new values from the e parameter of the method (the e.NewValues collection). There is no need to find the controls.
Just have a look at what's returned by "e", and you'll see what I mean.
Also, there is no need to call BindData there.
0
 

Author Comment

by:Mr_Shaw
ID: 22865938
Ok, what i have done is put

if i use e.NewValues  how do i distinguish between the many textboxes in gridview edit.
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22865974
Have a look at this link, it might be useful: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.newvalues.aspx
But, in short, you can get them by index or by key, for example, :
update_records_method.Record_ID = e.NewValues(index_of_the_column_containing_the_record_id).ToString
0
 

Author Comment

by:Mr_Shaw
ID: 22866063
I used the following code

string test = e.NewValues[3].ToString();

i got the following error.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I also used the approach stated in the link ; foreach (DictionaryEntry.........I put a break point on the foreach loop, but the code just skipped through this section.
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866109
Mmm..so what's the lenght of the NewValues collection (e.NewValues.Count)?
If it's 0, then there is something wrong with the update logic. How are you dataBinding the grid? If you try your code in the GridViewUpdated event, is the e.NewValue collection empty  as well?
0
 

Author Comment

by:Mr_Shaw
ID: 22866123
Ahhhhhhhhhhhhhh e.NewValues.Count was equal to 0.

I am binding the grid using

        user_records.DataSource = users;
        user_records.DataBind();
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866159
And if you try your code in the GridViewUpdated event, is the e.NewValue collection empty as well?
0
 

Author Comment

by:Mr_Shaw
ID: 22866189
i put the code in GridViewUpdated and applied a break point. However, the code did not pass through the GridViewUpdated event.
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866209
but did you set GridViewUpdated as a handler? , like:
 

<asp:GridView ID="GridView1" runat="server" OnRowUpdated="GridViewUpdated" moreProperties......>
        </asp:GridView>

Open in new window

0
 

Author Comment

by:Mr_Shaw
ID: 22866249
I have set all the handlers


        <asp:GridView ID="user_records" runat="server" 
            onprerender="edit_record_PreRender" 
            onrowcancelingedit="edit_record_RowCancelingEdit" 
            onrowdeleted="edit_record_RowDeleted" onrowdeleting="edit_record_RowDeleting" 
            onrowediting="edit_record"  
            onrowupdating="edit_record_updating" 
            OnRowUpdated="edit_record_updated"
            onselectedindexchanged="edit_record_SelectedIndexChanged" 
            onselectedindexchanging="edit_record_SelectedIndexChanging" 
            onsorted="edit_record_Sorted" onsorting="edit_record_Sorting" 
            onrowdatabound="edit_record_RowDataBound">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />

Open in new window

0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866271
Can you please then post you markup and the code-behind for the edit / update events?
0
 

Author Comment

by:Mr_Shaw
ID: 22866295
here is my code
    protected void edit_record(object sender, GridViewEditEventArgs e)
    {
 
        user_records.EditIndex = e.NewEditIndex;
        BindData(); 
        
    }
 
 
    protected void edit_record_updated(object sender, GridViewUpdatedEventArgs e)
    {
        Response.Write(e.NewValues.Count.ToString() + " updated");
     }
    protected void edit_record_updating(object sender, GridViewUpdateEventArgs e)
    {
 
       Response.Write( e.NewValues.Count.ToString() + " update");
 
 
    }

Open in new window

0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866331
But, if that worked, is going to give you the number of cells updated, is that what you want?

I'm now a bit confused about how this relates with the original question. If there is anything you can provide us to clarify, it would be appreciated.
0
 

Author Comment

by:Mr_Shaw
ID: 22866348
It does not work. When I update the values in the gridview, the updated values are not picked up by the code.

I have used break point to test my code and the same thing happens every time, the code just does not recognise that I have changed a value.
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866398
All right, so: How are you binding the grid? I can't see it in the markup you posted. Is it in code behind? Can you please post it?

0
 

Author Comment

by:Mr_Shaw
ID: 22866436
in page load i use the following      


      user_records.DataSource = users;
            user_records.DataBind();


0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22866448
do you check for post back?

if (!IsPostback)
{
            user_records.DataSource = users;
            user_records.DataBind();
}
0
 

Author Comment

by:Mr_Shaw
ID: 22866576
I chanegd my code to read

if (!Page.IsPostBack)
            {
                user_records.DataSource = users;
                user_records.DataBind();
            }

now i can't get rid of the update button.
0
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 500 total points
ID: 22866616
Sorry, but I mean to say that you should bind the gridview upon every post-back.
As you are not using DataSourceID, you should create code to do this.

I'll leave you to find out how to do this, it's just a matter of going through the MSDN documentation.

Anyway, now you know in what events and in what parameter you'll find you old / new values.
0
 

Author Comment

by:Mr_Shaw
ID: 22866992
I am still in the same situtaion as before. I know what events do what.

I don't understand why i need to give a datasourceID
0
 

Author Comment

by:Mr_Shaw
ID: 22868212
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31512627
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question