Mr_Shaw
asked on
gridview update (find control)
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.Row s[e.RowInd ex].FindCo ntrol("Loc ationTypet b")).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.
Location_Type = ((TextBox)user_records.Row
The problem is the code does not read the changes made to the content of the gridview, rather it returns the gridview orginal values.
ASKER
at the moment it is located in GridViewUpdate
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.
Make sure you have the if(IsPostback) clause.
if (!IsPostback)
{
// Do the Databind dance...
}
Sorry, but there is no such event handler in a grid view. Can you please recheck?
ASKER
ok I have changed my code to this
protected void edit_record_updating(objec t sender, GridViewUpdateEventArgs e)
{
update_records_method.Reco rd_ID = ((TextBox)user_records.Row s[e.RowInd ex].FindCo ntrol("Rec ordIDtb")) .Text.ToSt ring();
update_records_method.Logo n_Name = WindowsIdentity.GetCurrent ().Name.To String();
update_records_method.Reco rd_Class = ((TextBox)user_records.Row s[e.RowInd ex].FindCo ntrol("Rec ordClasstb ")).Text.T oString();
if (!Page.IsPostBack)
{
BindData(); // Do the Databind dance...
}
}
Is this correct
protected void edit_record_updating(objec
{
update_records_method.Reco
update_records_method.Logo
update_records_method.Reco
if (!Page.IsPostBack)
{
BindData(); // Do the Databind dance...
}
}
Is this correct
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:
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...
}
}
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.
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.
ASKER
Ok, what i have done is put
if i use e.NewValues how do i distinguish between the many textboxes in gridview edit.
if i use e.NewValues how do i distinguish between the many textboxes in gridview edit.
Have a look at this link, it might be useful: http://msdn.microsoft.com/ en-us/libr ary/system .web.ui.we bcontrols. gridviewup dateeventa rgs.newval ues.aspx
But, in short, you can get them by index or by key, for example, :
update_records_method.Reco rd_ID = e.NewValues(index_of_the_c olumn_cont aining_the _record_id ).ToString
But, in short, you can get them by index or by key, for example, :
update_records_method.Reco
ASKER
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.
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
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?
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?
ASKER
Ahhhhhhhhhhhhhh e.NewValues.Count was equal to 0.
I am binding the grid using
user_records.DataSource = users;
user_records.DataBind();
I am binding the grid using
user_records.DataSource = users;
user_records.DataBind();
And if you try your code in the GridViewUpdated event, is the e.NewValue collection empty as well?
ASKER
i put the code in GridViewUpdated and applied a break point. However, the code did not pass through the GridViewUpdated event.
but did you set GridViewUpdated as a handler? , like:
<asp:GridView ID="GridView1" runat="server" OnRowUpdated="GridViewUpdated" moreProperties......>
</asp:GridView>
ASKER
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" />
Can you please then post you markup and the code-behind for the edit / update events?
ASKER
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");
}
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.
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.
ASKER
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.
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.
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?
ASKER
in page load i use the following
user_records.DataSource = users;
user_records.DataBind();
user_records.DataSource = users;
user_records.DataBind();
do you check for post back?
if (!IsPostback)
{
user_records.DataSource = users;
user_records.DataBind();
}
if (!IsPostback)
{
user_records.DataSource = users;
user_records.DataBind();
}
ASKER
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.
if (!Page.IsPostBack)
{
user_records.DataSource = users;
user_records.DataBind();
}
now i can't get rid of the update button.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
I don't understand why i need to give a datasourceID
ASKER
ASKER
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.