Unable to retrieve contents on RowEditing (to cancel editing)

R

Radu

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.
 
M

Manish

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




Radu said:
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.
 
R

Radu

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,

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,
Manishwww.ComponentOne.com



Radu said:
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"))).Select­edValue;
                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("ActionTake­nLabel"))).Text
which returns ""
or
((DropDownList)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("ActionTake­nComboBoxEdit"))).SelectedValue;
which says
'((System.Web.UI.WebControls.ListControl)
(((System.Web.UI.WebControls.DropDownList)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("ActionTake­nComboBoxEdit")))))'
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.- Hide quoted text -

- Show quoted text -
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top