RowCancelingEdit and RowEditing take two mouse-clicks to work?

D

dev648237923

I have a simple GridView. I need to do the Select and Edit in the code
behind because I have to change schema prefix in the sql statement depending
on the current user.

I have the data grid and I have event handlers for edit and cancel but an
odd thing happes: I have to click the Edit link twice for it to work and I
need to click the Cancel link twice for it to work (the first postback does
call my funcs but does not have any effect on how the grid looks -- I have
to click the second time to get Update, Cancel to showup on the grid?). Is
the below the correct way to do this? Thank you!

<asp:gridview id="gv1" runat="server"
AutoGenerateColumns="False"
OnRowEditing="gv1_RowEditing"
OnRowCancelingEdit="gv1_RowCancelingEdit">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" ReadOnly="True" />
...more bound fields go here
</Columns>
</asp:gridview>

protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
{
gv1.EditIndex = e.NewEditIndex;
//then call my sql to do the edit
}
protected void gv1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
gv1.EditIndex = -1;
}

p.s. I know it would be easier/better to design the db around this (have a
usp and pass what schema to use, etc. but I am stuck with it this way so
must do this on the asp.net side
 
S

Steven Cheng[MSFT]

Hello dev648237923,

From your description, you're gettnig some strange behavior(need two time
to make Gridview change status) when using GridView to display and edit
data from database, correct?

As for the GridView databinding, are you using an associated DataSource
control(SqlDataSource or ObjectDataSource) or manually set the DataSource
property and call DataBind method ?

For Gridview, if you're using associated DataSource control to populate
data, it will automatically handle the selecting, editing and updating
events. However, if you manually bind the data, you need to manually hook
those events (like RowEditing, RowUpdating, RowCancelingEdit ...) in code.
Also, in each of such event, since you manually bind data to the GridView,
you also need to do the databinding in each of those event because after
each of such event, GridView need to repopulate the Rows collection. e.g.

========================
.....................
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource =
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
GridView1.DataBind();
}

protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;

GridView1.DataSource =
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
GridView1.DataBind();
}

...............
===========================

If you feel necessary I can send you a test page for reference. Please feel
free to let me know if you have any further questions or any thing
particular in your scenario.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dev648237923

Thank you for your ideas -- they fixed the problem which was I was not
calling GridView1.DataBind() in th event handlers.
Thank you!
 
S

Steven Cheng[MSFT]

That's great!

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top