Unable to retrieve contents on RowEditing (to cancel editing)

Unable to retrieve contents on RowEditing (to cancel editing)

am 01.02.2008 19:45:45 von 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:

ID="GridView1"
DataSourceID="SqlDataSource1"
DataKeyNames="ID"
AutoGenerateColumns="False"
ShowFooter="True"
OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing"
OnRowDataBound="GridView1_RowDataBound"
Runat="server"
>



<%#
Eval("ActionTaken")%>



ID="ActionTakenComboBox"
AutoPostBack="false"
DataSourceID="SqlDataSource2" DataTextField="ActionTaken"
DataValueField="ActionTaken"
Runat="server"
Text='<%# Eval("ActionTaken")%>'>





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("ActionTakenComb oBox"))).SelectedValue;
if (strActionType.IndexOf("Manually") >= 0)
{
// Cancel the edit operation.
e.Cancel = true;
String strMessage;
strMessage = "You cannot edit this record.";
Response.Write("");
}
}

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)].FindCont rol("ActionTakenLabel"))).Text
which returns ""
or
((DropDownList)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindCont rol("ActionTakenComboBoxEdit"))).SelectedValue;
which says
'((System.Web.UI.WebControls.ListControl)
(((System.Web.UI.WebControls.DropDownList)
(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindCont rol("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.

RE: Unable to retrieve contents on RowEditing (to cancel editing)

am 01.02.2008 20:48:07 von 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" wrote:

> 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:
>
> > ID="GridView1"
> DataSourceID="SqlDataSource1"
> DataKeyNames="ID"
> AutoGenerateColumns="False"
> ShowFooter="True"
> OnRowUpdating="GridView1_RowUpdating"
> OnRowEditing="GridView1_RowEditing"
> OnRowDataBound="GridView1_RowDataBound"
> Runat="server"
> >
>
>
>
> <%#
> Eval("ActionTaken")%>

>

>
> > ID="ActionTakenComboBox"
> AutoPostBack="false"
> DataSourceID="SqlDataSource2" DataTextField="ActionTaken"
> DataValueField="ActionTaken"
> Runat="server"
> Text='<%# Eval("ActionTaken")%>'>
>
>

>

>
>
> 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("ActionTakenComb oBox"))).SelectedValue;
> if (strActionType.IndexOf("Manually") >= 0)
> {
> // Cancel the edit operation.
> e.Cancel = true;
> String strMessage;
> strMessage = "You cannot edit this record.";
> Response.Write("");
> }
> }
>
> 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)].FindCont rol("ActionTakenLabel"))).Text
> which returns ""
> or
> ((DropDownList)
> (GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindCont rol("ActionTakenComboBoxEdit"))).SelectedValue;
> which says
> '((System.Web.UI.WebControls.ListControl)
> (((System.Web.UI.WebControls.DropDownList)
> (GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindCont rol("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.
>

Re: Unable to retrieve contents on RowEditing (to cancel editing)

am 01.02.2008 21:05:45 von 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=3Dtrue..... The problem is that I CAN NOT
retrieve the value in the "ActionTaken" field :-(((((((((


On Feb 1, 2:48=A0pm, Manish wrote:
> Hi Alex,
>
> You can either try to find the oldvalue for that column in the Row Updatin=
g
> event and then cancel that updaitng based on the value like
>
> =A0 =A0 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal =
e As
> System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
> GridView1.RowUpdating
> =A0 =A0 =A0 =A0 If e.OldValues.Item(0) =3D "Produce" Then
> =A0 =A0 =A0 =A0 =A0 =A0 e.Cancel =3D True
> =A0 =A0 =A0 =A0 End If
> =A0 =A0 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 =3D DataControlRowState.Edit Then
> =A0 =A0 =A0 =A0 =A0 =A0 If CType(e.Row.Cells(2).FindControl("TextBox1"), T=
extBox).Text =3D
> "Produce" Then
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 e.Row.Cells(0).Enabled =3D False
> =A0 =A0 =A0 =A0 =A0 =A0 Else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 e.Row.Cells(0).Enabled =3D True
> =A0 =A0 =A0 =A0 =A0 =A0 End If
> =A0 =A0 =A0 =A0 End If
>
> Regards,
> Manishwww.ComponentOne.com
>
>
>
> "Radu" wrote:
> > 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:
>
> > > > =A0 =A0ID=3D"GridView1"
> > =A0 =A0DataSourceID=3D"SqlDataSource1"
> > =A0 =A0DataKeyNames=3D"ID"
> > =A0 =A0AutoGenerateColumns=3D"False"
> > =A0 =A0ShowFooter=3D"True"
> > =A0 =A0OnRowUpdating=3D"GridView1_RowUpdating"
> > =A0 =A0OnRowEditing=3D"GridView1_RowEditing"
> > =A0 =A0OnRowDataBound=3D"GridView1_RowDataBound"
> > =A0 =A0Runat=3D"server"
>
> >
> > =A0 =A0 =A0 =A0 =A0
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 Server" ><%#
> > Eval("ActionTaken")%>

> > =A0 =A0 =A0 =A0 =A0

> > =A0 =A0 =A0 =A0 =A0
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ID=3D"ActionTakenComboBox"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0AutoPostBack=3D"false"
> > DataSourceID=3D"SqlDataSource2" =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 DataTextField=3D"ActionTaken"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0DataValueField=3D"ActionTaken"
> > =A0 =A0 =A0 Runat=3D"server"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Text=3D'<%# Eval("ActionTaken")%>=
'>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0
> > =A0 =A0 =A0 =A0 =A0

> >

>
> > 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)
> > {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 String strActionType =3D ((DropDownList)=

> > (GridView1.Rows[e.NewEditIndex].FindControl("ActionTakenComb oBox"))).Sel=
ect=ADedValue;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (strActionType.IndexOf("Manually") >=
=3D 0)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0// Cancel the edit operation.
> > =A0 =A0 =A0 =A0 =A0 =A0e.Cancel =3D true;
> > =A0 =A0 =A0 =A0 =A0 =A0String strMessage;
> > =A0 =A0 =A0 =A0 =A0 =A0strMessage =3D "You cannot edit this record.";
> > =A0 =A0 =A0 =A0 =A0 =A0Response.Write("");
> > =A0 =A0}
> > }
>
> > 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
> > =A0 =A0GridView1.Rows[e.NewEditIndex].Cells[any possible cell index]
> > =A0 =A0GridView1.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)].FindCont rol("ActionT=
ake=ADnLabel"))).Text
> > which returns ""
> > or
> > ((DropDownList)
> > (GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindCont rol("ActionT=
ake=ADnComboBoxEdit"))).SelectedValue;
> > which says
> > '((System.Web.UI.WebControls.ListControl)
> > (((System.Web.UI.WebControls.DropDownList)
> > (GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindCont rol("ActionT=
ake=ADnComboBoxEdit")))))'
> > 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 -