Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Unable to retrieve contents on RowEditing (to cancel editing)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Radu - 01 Feb 2008 18:45 GMT
Hi. This is getting extremely frustrating. I have struggled for more
than three hours now with this:

I have a gridview, GridView1, which works great reading/editing/
deleting the data from a table. I need, however, to block editing and
deleting of some special rows (the rows which have an "ActionTaken"
field containing the word "Manually".

So here is a simplified HTML:

<asp:GridView
    ID="GridView1"
    DataSourceID="SqlDataSource1"
    DataKeyNames="ID"
    AutoGenerateColumns="False"
    ShowFooter="True"
    OnRowUpdating="GridView1_RowUpdating"
    OnRowEditing="GridView1_RowEditing"
    OnRowDataBound="GridView1_RowDataBound"
    Runat="server"

<asp:TemplateField HeaderText="Action Type">
         <ItemTemplate>
             <asp:Label ID="ActionTakenLabel" Runat="Server" ><%#
Eval("ActionTaken")%></asp:Label>
         </ItemTemplate>
         <EditItemTemplate>
            <asp:DropDownList
                  ID="ActionTakenComboBox"
                  AutoPostBack="false"
DataSourceID="SqlDataSource2"                      DataTextField="ActionTaken"
                  DataValueField="ActionTaken"
      Runat="server"
                  Text='<%# Eval("ActionTaken")%>'>
            </asp:DropDownList>
         </EditItemTemplate>
</asp:TemplateField>

I would want to CANCEL the editing based on the value in ActionTaken,
so therefore:

protected void GridView1_RowEditing(object sender,
System.Web.UI.WebControls.GridViewEditEventArgs e)
{
               String strActionType = ((DropDownList)
(GridView1.Rows[e.NewEditIndex].FindControl("ActionTakenComboBox"))).SelectedValue;
               if (strActionType.IndexOf("Manually") >= 0)
    {
        // Cancel the edit operation.
        e.Cancel = true;
        String strMessage;
        strMessage = "You cannot edit this record.";
        Response.Write("<script language='javascript'>alert('" + strMessage
+ "');</script>");
    }
}

Even if the gridview reads the data okay, and looks great, in the code-
behind the dropdownlist "ActionTakenComboBox" is ALWAYS NULL, and so
are all the other controls I have tried to read (like
ActionTakenLabel).

I have tried reading them with
  GridView1.Rows[e.NewEditIndex].Cells[any possible cell index]
  GridView1.Rows[any other possible index].Cells[any possible cell
index]
with the same result.

I have also tried to see if I can see those values in the
OnRowDataBound event. I can still not find my value ("Manually Added")
which, however, shows just fine in the gridview.

I have even tried to somehow block the editing in the
GridView1_RowCommand event:
((Label)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("ActionTakenLabel"))).Text
which returns ""
or
((DropDownList)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("ActionTakenComboBoxEdit"))).SelectedValue;
which says
'((System.Web.UI.WebControls.ListControl)
(((System.Web.UI.WebControls.DropDownList)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("ActionTakenComboBoxEdit")))))'
is null

I have also tried wo write code in the OnPreRender event...
What should I do here to block editing of those records ?

Thank you very much
Alex.
Manish - 01 Feb 2008 19:48 GMT
Hi Alex,

You can either try to find the oldvalue for that column in the Row Updating
event and then cancel that updaitng based on the value like

   Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
GridView1.RowUpdating
       If e.OldValues.Item(0) = "Produce" Then
           e.Cancel = True
       End If
   End Sub

or else you can check for the value in the rowdatabound event and then
disable the Update button based on the value in that row like:

If e.Row.RowState = DataControlRowState.Edit Then
           If CType(e.Row.Cells(2).FindControl("TextBox1"), TextBox).Text =
"Produce" Then
               e.Row.Cells(0).Enabled = False
           Else
               e.Row.Cells(0).Enabled = True
           End If
       End If

Regards,
Manish
www.ComponentOne.com

> Hi. This is getting extremely frustrating. I have struggled for more
> than three hours now with this:
[quoted text clipped - 87 lines]
> Thank you very much
> Alex.
Radu - 01 Feb 2008 20:05 GMT
Thank you for answering... Is it possible, though, to not even allow
the user to go that far ? I'm thinking of blocking the execution of
rowEditing using e.cancel=true..... The problem is that I CAN NOT
retrieve the value in the "ActionTaken" field :-(((((((((

> Hi Alex,
>
[quoted text clipped - 117 lines]
>
> - Show quoted text -

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.