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>
<aspropDownList
ID="ActionTakenComboBox"
AutoPostBack="false"
DataSourceID="SqlDataSource2" DataTextField="ActionTaken"
DataValueField="ActionTaken"
Runat="server"
Text='<%# Eval("ActionTaken")%>'>
</aspropDownList>
</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.
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>
<aspropDownList
ID="ActionTakenComboBox"
AutoPostBack="false"
DataSourceID="SqlDataSource2" DataTextField="ActionTaken"
DataValueField="ActionTaken"
Runat="server"
Text='<%# Eval("ActionTaken")%>'>
</aspropDownList>
</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.