Howto read values in GridView from the row about to be edited inRowEditing ?

R

Radu

Hi. I have looked high and low for a solution to my problem, but I
haven't found an answer. I have the following HTML code:

<asp:GridView
ID="GridView1"
DataSourceID="SqlDataSource1"
DataKeyNames="ID"
AutoGenerateColumns="False"
ShowFooter="True"
OnPreRender="GridView1_Prerender"
OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing"
Runat="server"
>
<Columns>
<asp:TemplateField HeaderText="Action Type">
<ItemTemplate>
<asp:Label ID="ActionTakenLabel" Runat="Server" ><%#
Eval("ActionTaken")%></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList
ID="ActionTakenCboAdd"
AutoPostBack="false"
DataSourceID="SqlDataSource2"
DataTextField="ActionTaken"
DataValueField="ActionTaken"
Runat="server">
</asp:DropDownList>
</FooterTemplate>
<EditItemTemplate>
<asp:DropDownList
ID="ActionTakenCboEdit"
AutoPostBack="false"
DataSourceID="SqlDataSource2"
DataTextField="ActionTaken"
DataValueField="ActionTaken"
Text='<%# Eval("ActionTaken")%>'
Runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete Record" Visible="true">
<ItemTemplate>
<asp:LinkButton
ID = "linkDelete"
runat = "server"
CausesValidation ="false"
CommandName = "Delete"
text = "Delete"
OnClientClick = "return confirm('Pressing OK will delete this
record. Do you want to continue ?')">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField
HeaderText="Edit Record"
ShowEditButton="True"
ValidationGroup="Row-Edit"
>
</asp:CommandField>
</Columns>
</asp:GridView>

I want to prevent the user from editing or deleting a record if that
record has an "ActionTaken" starts with "Manually". Therefore I would
like to add code like this:

protected void GridView1_RowEditing(object sender,
System.Web.UI.WebControls.GridViewEditEventArgs e)
{
String strActionType = ((Label)
(GridView1.Rows[e.NewEditIndex].FindControl("ActionTakenLabel"))).Text;
if (strActionType.IndexOf("Manually") >= 0)
{
// Cancel the edit operation.
e.Cancel = true;
String strMessage;
strMessage = "You cannot edit this record.";
ClientScript.RegisterStartupScript(GetType(), "notEditable",
"alert('" + strMessage + "');", true);
}
}

The problem is that no matter how I try to address the
ActionTakenLabel, I do not see it other than NULL. I have tried in a
lot of ways:
((Label)
(GridView1.Rows[e.NewEditIndex].FindControl("ActionTakenLabel"))).Text;
and
((Label)
(GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("ActionTakenLabel"))).Text
and
((Label)
(GridView1.Rows[GridView1.SelectedIndex].FindControl("ActionTakenLabel"))).Text;
and other such combinations. They all return ""....

If I try with ActionTakenCboEdit, it's even worse, returning an error
message saying that ActionTakenCboEdit is null, which would make
sense, since they say that the "RowEditing" event happens BEFORE the
row actually gets in Edit mode.

For now I have the following code:

protected void GridView1_RowUpdating(object sender,
System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
String strActType = ((DropDownList)
(GridView1.Rows[GridView1.EditIndex].FindControl("ActionTakenCboEdit"))).SelectedValue;
//don't update if the record starts with the word "Manually":
if (strActType.IndexOf("Manually") >= 0)
{
string strMessage;
strMessage = "This action type is not editable !";
e.Cancel = true;
ClientScript.RegisterStartupScript(GetType(), "notEditable",
"alert('" + strMessage + "');", true);
}
}

which is NOT satisfactory because it happens AFTER the fact, and that
would be annoying for the user, wouldn't it ?

Short of making the grid BOUND (which is not acceptable), is there a
way to read the values in the row ABOUT to be edited ? I have spent a
full day trying to find a solution, so I'm really anxious to see this
solved somehow ... :-(

Thanks a lot,
Alex
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top