RequiredValidator is not working in asp.net - asp.net

I want to validate some text box and dropdownlist control not to empty, like below highlight part:
and my GridView control code looks like below:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EMPLOYEEID"
DataSourceID="SqlDataSource1" ShowFooter="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEEID" SortExpression="EMPLOYEEID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("EMPLOYEEID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EMPLOYEEID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInsertEmpID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsertEmpID" ControlToValidate="txtInsertEmpID"
Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server" ErrorMessage="EmployeeID is required" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME" SortExpression="NAME">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("NAME") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditName" ControlToValidate="TextBox1" Text="*"
ForeColor="Red" runat="server" ErrorMessage="EmployeeName is required" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("NAME") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInsertName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsertName" ControlToValidate="txtInsertName"
Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server" ErrorMessage="EmployeeName is required" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPTID" SortExpression="DEPTID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" SelectedValue='<%# Bind("DEPTID") %>' runat="server">
<asp:ListItem>Select Department</asp:ListItem>
<asp:ListItem Value="1">SM</asp:ListItem>
<asp:ListItem Value="2">CDS</asp:ListItem>
<asp:ListItem Value="3">AM</asp:ListItem>
<asp:ListItem Value="4">FS</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvEditDept" ControlToValidate="DropDownList1" Text="*"
ForeColor="Red" runat="server" ErrorMessage="Department is required" InitialValue="Select Department" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("DEPTID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlInsertDeptID" runat="server">
<asp:ListItem>Select Department</asp:ListItem>
<asp:ListItem Value="1">SM</asp:ListItem>
<asp:ListItem Value="2">CDS</asp:ListItem>
<asp:ListItem Value="3">AM</asp:ListItem>
<asp:ListItem Value="4">FS</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvInsertDept" ControlToValidate="ddlInsertDeptID"
Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server" ErrorMessage="Department is required"
InitialValue="Select Department" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Insert"
ForeColor="Blue" />
<asp:ValidationSummary ID="ValidationSummary2" runat="server" ForeColor="Red" />
I'm not sure what's the problem so that when I click Insert link button the page was submitted without any error message even though I don't type anything in the bottom of text boxes.
can anybody help me?

You are just Missing Validation Group in Insert LinkButton.
<asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="Insert">Insert</asp:LinkButton>

Please use following code for required field by removing your
InitialValue="Select Department" and ErrorMessage="Department is required"
<asp:RequiredFieldValidator ID="rfvInsertDept" ControlToValidate="ddlInsertDeptID" Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server">Department is required</asp:RequiredFieldValidator>

Related

How to loop through controls within a gridview and store their values in an array?

please excuse this potentially long post as I feel like most of the information I will add below is necessary. As the title states I am trying to loop through a collection of controls that are within a gridview on my page (there are 2 gridviews actually, but they are identical in behavior) and store the values of some of these controls in an array that I can use to perform other functions.
To start I have added the code for the gridview below.
<asp:Panel ID="gridPNL1" runat="server">
Repair / Labor<asp:LinkButton runat="server" ID="lbAddRepair" Text="Add New Repair / Labor" CommandName="AddLabor" OnClick="AddObject_Click" Enabled="false" style="margin-left:20px" Visible="false" />
<asp:GridView ID="GridView1" runat="server"
EmptyDataText="No Claimed Labor" AutoGenerateColumns="False"
ShowHeaderWhenEmpty="True" DataKeyNames="RecID"
ShowFooter="True" DataSourceID="SqlDataSource6" Width="95%">
<RowStyle HorizontalAlign="Center" />
<EmptyDataTemplate>
<asp:DropDownList ID="ddlRepairEquipmentNew" runat="server" DataSourceID="SqlDataSourcePartEquipment" DataValueField="RecID"
DataTextField="EquipmentPart" AppendDataBoundItems="True" AutoPostBack="True" Width="220px" Height="20px"
Style="margin-left: 70px;" > <%-- Removed the OnSelectedIndexChanged (add if needed) --%>
<asp:ListItem Value ="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlRepairNew" runat="server" DataSourceID="SqlDataSourceRepair" DataValueField="RecID"
DataTextField="Description" AppendDataBoundItems="True" Width="220px" Height="20px" AutoPostBack="true"
style="" OnSelectedIndexChanged="ddlRepairNew_SelectedIndexChanged"
OnDataBinding="ddlRepairNew_DataBinding">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<%-- <asp:textbox ID="txtDescOther" runat="server" Width="235px" Visible="false" />--%>
<asp:textbox runat="server" ID="txtRepairHoursNew" Width="95px" style="margin-left: 68px"/>
<%--<asp:Label runat="server" ID="lblRepairHoursAllowed"></asp:Label>--%>
<asp:HiddenField runat="server" ID="hidAllowedRepairHoursNew" />
<%-- <asp:textbox ID="txtAllowedHoursNew" runat="server" Width="90px"/>--%>
<asp:textbox ID="txtRepairCostNew" runat="server" Width="95px" ReadOnly="True"/>
<asp:textbox ID="txtLineTotalNew" runat="server" Width="95px" ReadOnly="true"/>
<asp:textbox ID="txtTaxNew" runat="server" Width="95px" text="0"/>
<asp:Button ID="InsertDetail" runat="server" CommandName="InsertDetail" Height="25px" Text="Add Detail" Width="85px" />
</EmptyDataTemplate>
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:CommandField ShowEditButton="True" footertext="Add -->" ShowDeleteButton="True" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="RecID" HeaderText="RecID" SortExpression="RecID" ReadOnly="True" Visible="False" />
<asp:TemplateField HeaderText="Equipment / Repair / Labor Description" ItemStyle-HorizontalAlign="center" >
<ItemTemplate>
<asp:Label ID="lblRepairEquipmentType" Text='<%# Bind("EquipmentType") %>' runat="server" Enabled="False" Width="220px" />
<asp:label ID="lblRepairType" Text='<%# Bind("RepairType") %>' runat="server" Enabled="False" Width="220px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlRepairEquipmentEdit" runat="server" DataSourceID="SqlDataSourcePartEquipment" DataValueField="RecID" DataTextField="EquipmentPart" SelectedValue='<%# Eval("EquipmentID") %>' AppendDataBoundItems="True" AutoPostBack="True" Width="220px" Height="20px" >
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlRepairEdit" runat="server" DataSourceID="SqlDataSourceRepair" DataValueField="RecID" DataTextField="Description" SelectedValue='<%# Eval("RepairID")%>' AppendDataBoundItems="True" AutoPostBack="true" Width="220px" Height="20px" OnSelectedIndexChanged="ddlRepairEdit_SelectedIndexChanged" />
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlRepairEquipmentInsert" runat="server" DataSourceID="SqlDataSourcePartEquipment" DataValueField="RecID" DataTextField="EquipmentPart" AppendDataBoundItems="True" AutoPostBack="True" Width="220px" Height="20px" Style="margin-left: 29px" >
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlRepairInsert" runat="server" DataSourceID="SqlDataSourceRepair" DataValueField="RecID" DataTextField="Description" AppendDataBoundItems="True" autopostback="true" Width="220px" Height="20px" OnSelectedIndexChanged="ddlRepairInsert_SelectedIndexChanged" OnDataBinding="ddlRepairInsert_DataBinding">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Repair Hours Requested" ItemStyle-HorizontalAlign="center" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:label ID="lblRepairHours" Text='<%# Bind("RepairHours")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:textbox ID="txtRepairHoursEdit" Text='<%# Bind("RepairHours")%>' runat="server" Width="95px" />
<%--<asp:Label ID="lblRepairHoursEdit" runat="server"></asp:Label>--%>
<asp:HiddenField runat="server" ID="hidAllowedRepairHoursEdit" Value='<%# Bind("AllowedHours")%>' />
</EditItemTemplate>
<FooterTemplate>
<asp:textbox ID="txtRepairHoursInsert" runat="server" style="width: 100%; box-sizing: border-box;"/>
<%--<center><asp:Label ID="lblRepairHoursInsert" runat="server"></asp:Label></center>--%>
<asp:HiddenField runat="server" ID="hidAllowedRepairHoursInsert" />
</FooterTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Repair Hours Approved" ItemStyle-HorizontalAlign="center" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:label ID="lblAllowedHours" Text='<%# Bind("AllowedHours")%>' runat="server" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox ID="txtAllowedHoursEdit" Text='<%# Bind("AllowedHours")%>' Enabled="true" runat="server" Width="50px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:textbox ID="txtAllowedHoursInsert" Text='<%# Bind("AllowedHours")%>' runat="server" style="width: 100%; box-sizing: border-box;"/>
</FooterTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Repair Cost" ItemStyle-HorizontalAlign="center" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:label ID="lblRepairCost" Text='<%# Bind("RepairCost", "{0:C}") %>' runat="server" Enabled="False"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox ID="txtRepairCostEdit" Text='<%# Bind("RepairCost") %>' runat="server" ReadOnly="True" Width="95px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:textbox ID="txtRepairCostInsert" Text='<%# Bind("RepairCost") %>' runat="server" ReadOnly="True" style="width: 100%; box-sizing: border-box;"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Repair Total" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:label ID="lblRepairTotal" Text='<%# Bind("LaborRequested", "{0:C}")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:textbox ID="txtRepairTotalEdit" Text='<%# Bind("LaborRequested")%>' Enabled="false" runat="server" Width="95px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:textbox ID="txtRepairTotalInsert" Text='<%# Bind("LaborRequested") %>' runat="server" Enabled="false" style="width: 100%; box-sizing: border-box;"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tax" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:LinkButton ID="lbTaxTotal" Text='<%# Bind("Tax", "{0:C}")%>' runat="server" CommandArgument="Repair" OnClick="lblTaxTotal_Click" />
<asp:HiddenField ID="hidRepairGST" runat="server" Value='<%# Bind("GST")%>' />
<asp:HiddenField ID="hidRepairPST" runat="server" Value='<%# Bind("PST")%>' />
<asp:HiddenField ID="hidRepairQST" runat="server" Value='<%# Bind("QST")%>' />
<asp:HiddenField ID="hidRepairHST" runat="server" Value='<%# Bind("HST")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:textbox ID="lblTaxTotalEdit" Text='<%# Bind("Tax")%>' Enabled="true" runat="server" Width="95px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:textbox ID="txtTaxTotalInsert" runat="server" Enabled="true" text="0" style="width: 100%; box-sizing: border-box;"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Insert New" HeaderStyle-Width="85px">
<ItemTemplate>
<asp:Label ID="lblEmpty" Text="" runat="server" Width="85px"/>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Insert" runat="server" CommandName="InsertNewDetail" Height="22px" Text="Insert" style="width: 100%; box-sizing: border-box;" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#336699" ForeColor="White" />
</asp:GridView>
<asp:Label runat="server" ID="lblErrorRepair" ForeColor="Red" />
<br />
</asp:Panel>
What I am trying to do with the gridview above is loop through some specific controls on the gridview and store their values in an array. Essentially how this gridview works is the user will enter information into a series of drop down lists and textboxes, as seen above, when the user hits the button at the end of the row (labeled "InsertDetail" and "Insert" respectively, it takes the data from the drop down list and textboxes and stores it in the labels (also above) for simpler display (this goes on for however many items the user needs to enter (usually around 2 - 5 items total)). I need a way to loop through the values in however many iterations of "lblRepairEquipmentType" there are at the time the user hits any of the "insert" buttons and store the value of each iteration of "lblRepairEquipmentType" in an array so that I can use that array to perform other procedures.
Any help would be greatly appreciated and if there is something else I can try that would be better, either efficiency wise or just for best practices I would appreciate and welcome any constructive criticism. Thank you all for your patience and willingness to help.

RequiredFieldValidator is not Valid even if there is values

I have a Page Fruit that has a UpdatePanel P1 and a ModalWIndow (outside of the updatepanel). I have a Page Apple with An UpdatePanel P2. ModalWindow on Page fruit will add Page Apple on demand.
Now, In the Apple Page
I have a GridView with EditItemTemplate and FooterItemtemplate for lets say 5 columns. Both of them have RegularExpressionValidator and RequiredFieldValidtor each separated by ValidationGroup. For EditItemTemplate everything is on point.
For FooterItemTemplate, the button won't do postback even though all the validation is met.
The footer template looks like this for the column of interest
<FooterTemplate>
<asp:TextBox ID="vendorAdd" runat="server" Width="95%"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator66" ControlToValidate="vendorAdd" ValidationGroup="AddValidation"
ValidationExpression="\w*" runat="server" ErrorMessage="Invalid VendorName" />
<asp:RequiredFieldValidator id = "RegularExpressionValidator67" ValidationGroup="AddValidation" ControlToValidate="vendorAdd"
runat="server" ErrorMessage="VendorName Required" />
</FooterTemplate>
There is no message saying this failed. I removed the all the other validation on other footer column than shown above. I added on the button to stop validation. And in Server side added this
Page.Validate("AddValidation");
var b = Page.IsValid;
var notValidValidators = Page.Validators.Cast<IValidator>().Where(v => !v.IsValid);`
Here in the debugger the notValidValidator says that RequiredFieldValidator is not Valid. I don't see why though, I have a value and there is no message on the front end.
Update Panel on Apple Page full Code is below
<asp:UpdatePanel ID="ackPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="ackedItems" runat="server" Width="720" Visible="true" DataKeyNames="AckId" RowStyle-Height="10px"
AutoGenerateColumns="False" BorderStyle="Dotted" BorderWidth="1px" CellPadding="2"
BackColor="White" BorderColor="#CCCCCC" HorizontalAlign="Left" AllowPaging="false"
ShowFooter="true" OnRowUpdating="AckedItems_RowUpdating" OnRowDeleting="AckedItems_RowDeleting"
OnRowEditing="AckedItems_RowEditing" OnRowCommand="AckedItems_RowCommand" OnRowDataBound="AckedItems_RowDataBound">
<RowStyle ForeColor="#000066" Font-Size="8pt" />
<HeaderStyle ForeColor="#336699" Font-Size="8pt" CssClass="GridViewHeader" Height="20px" />
<AlternatingRowStyle CssClass="GridViewAltStyle" />
<Columns >
<asp:BoundField DataField="AckId" ReadOnly="true" Visible="false" />
<asp:TemplateField HeaderText="Vendor" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="vendor" runat="server" Text='<%# Bind("VendorName") %>' Width="95%"/>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="vendorEdit" runat="server" Text='<%# Bind("VendorName") %>' Width="95%"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="vendorEdit" ValidationGroup="EditValidation"
ValidationExpression="\w*" runat="server" ErrorMessage="Invalid VendorName" />
<asp:RequiredFieldValidator id = "Ab" ValidationGroup="EditValidation" ControlToValidate="vendorEdit"
runat="server" ErrorMessage="VendorName Required" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="vendorAdd" runat="server" Width="95%"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator66" ControlToValidate="vendorAdd" ValidationGroup="AddValidation"
ValidationExpression="\w*" runat="server" ErrorMessage="Invalid VendorName" />
<asp:RequiredFieldValidator id = "RegularExpressionValidator67" ValidationGroup="AddValidation" ControlToValidate="vendorAdd"
runat="server" ErrorMessage="VendorName Required" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount" HeaderStyle-Width="10px">
<ItemTemplate>
<asp:Label ID="amount" DataFormatString = "{0:C2}" runat="server" Text='<%# Bind("Amount") %>' Width="95%" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="amountEdit" runat="server" Text='<%# Bind("Amount") %>' Width="95%"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" ControlToValidate="amountEdit" ValidationGroup="EditValidation"
ValidationExpression="\d*(\.\d*)?" runat="server" ErrorMessage="Invalid Amount" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="EditValidation" ControlToValidate="amountEdit"
runat="server" ErrorMessage="Amount Required" />
</EditItemTemplate>
<FooterTemplate >
<asp:TextBox ID="amountAdd" runat="server"
Width="95%" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" ControlToValidate="amountAdd" ValidationGroup="AddValidation"
ValidationExpression="\d*(\.\d*)?" runat="server" ErrorMessage="Invalid Amount" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="type" runat="server" Text='<%# Bind("Type") %>' Width="95%" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="typeEdit" runat="server" Text='<%# Bind("Type") %>' Width="95%" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" ControlToValidate="typeEdit" ValidationGroup="EditValidation"
ValidationExpression="\w*" runat="server" ErrorMessage="Invalid Type" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" ValidationGroup="EditValidation" ControlToValidate="typeEdit"
runat="server" ErrorMessage="Type Required" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="typeAdd" runat="server" Width="95%" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="typeAdd"
ValidationGroup="AddValidation" ValidationExpression="\w*" runat="server" ErrorMessage="Invalid Type" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProgNum" ReadOnly="true" HeaderText="ProgNum" ControlStyle-Width="95%" />
<asp:TemplateField HeaderText="AckDate">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Convert.ToDateTime(Eval("AckDate")).ToString("MM/dd/yyyy") %>' Width="95%"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" >
<ItemTemplate>
<asp:Label ID="notes" runat="server" Text='<%# Bind("Notes") %>' Width="95%" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="notesEdit" Text='<%# Bind("Notes") %>' runat="server" Width="95%"/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="notesAdd" runat="server" Width="95
%" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ActivityDate">
<ItemTemplate>
<asp:Label ID="activityDate" runat="server" Text='<%#Convert.ToDateTime(Eval("activityDate")).ToString("MM/dd/yyyy") %>' Width="95%" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="activityDateEdit" Text='<%#Convert.ToDateTime(Eval("activityDate")).ToString("MM/dd/yyyy") %>' runat="server" CssClass="date" Width="95%" />
<asp:CompareValidator ID="CompareValidator1" runat="server" Type="Date" Operator="DataTypeCheck" ValidationGroup="EditValidation"
ControlToValidate="activityDateEdit" ErrorMessage="Invalid Date" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" ValidationGroup="EditValidation" ControlToValidate="activityDateEdit"
runat="server" ErrorMessage="Date Required" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="activityDateAdd" runat="server" CssClass="date" Width="95%"/>
<asp:CompareValidator ID="CompareValidator2" runat="server" Type="Date" Operator="DataTypeCheck" ValidationGroup="AddValidation"
ControlToValidate="activityDateAdd" ErrorMessage="Invalid Date" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IsVoid">
<ItemTemplate>
<asp:CheckBox ID="isVoid" runat="server" Checked='<%# Eval("IsVoid") %>' Width="95%" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isVoidEdit" runat="server" Checked='<%# Eval("IsVoid") %>' Width="95%" />
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="isVoidAdd" runat="server" Width="95%" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VoidReason">
<ItemTemplate>
<asp:Label ID="voidReason" runat="server" Text='<%# Bind("VoidReason") %>' Width="95%" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="voidReasonEdit" runat="server" Width="95%" />
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="voidReasonAdd" runat="server" Width="95%" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="false" CommandName="Edit"
Text="<img border=0 src=../Theme/Main/Images/edit.gif alt=Edit>" />
<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="false" CommandName="Delete"
OnClientClick="if(!confirm('Do You Really want to Delete this Order Item?')) return false;"
Text="<img border=0 src=../Theme/Main/Images/delete.gif alt=Delete>" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbAdd" runat="server" CausesValidation="true" CommandName="Update"
ValidationGroup="EditValidation" Text="<img border=0 src=../Theme/Main/Images/check.gif alt=Update>" />
<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="false" CommandName="Cancel"
Text="<img border=0 src=../Theme/Main/Images/delete.gif alt=Cancel>" />
</EditItemTemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Top" />
<FooterTemplate>
<asp:LinkButton ID="lbAdd" runat="server" CausesValidation="false" CommandName="Add"
ValidationGroup="AddValidation" Text="<img border=0 src=../Theme/Main/Images/check.gif alt=Add>" />
<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="false" CommandName="Cancel"
Text="<img border=0 src=../Theme/Main/Images/delete.gif alt=Cancel>" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:TextBox ID="vendorEdit" runat="server" Text='<%# Bind("Vendor") %>' CssClass="InputTextBox"
Width="100px" />
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lbEdit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

Modifying edited field of a DetailsView before inserting it to database when in "insert mode"

On one of my final year project page I have a DetailsView that I am using to update/edit a GridView.
I have a password field that must show the hashed password.
When I am in "edit" or "insert" mode in the DetailsView and inserting a new password in the field (or editing the field) I would like to hash directly the password before showing it in the GridView/DetailsView and of course before inserting/updating it in the DataBase.
My GridView & DetailsView are both in the same UpdatePanel and I am using a ObjectDataSource.
That a partial code:
<asp:DetailsView ID="DetailsViewSingleUsr" runat="server" Height="50px"
Width="125px" DataSourceID="SingleUserObjectDataSource" DataKeyNames="id"
onitemdeleted="DetailsViewSingleUsr_ItemDeleted"
oniteminserted="DetailsViewSingleUsr_ItemInserted"
onitemupdated="DetailsViewSingleUsr_ItemUpdated" AutoGenerateRows="False"
oniteminserting="DetailsViewSingleUsr_ItemInserting">
<Fields>
<asp:TemplateField HeaderText="User ID" InsertVisible="False"
SortExpression="id">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName" SortExpression="username">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="Username may only contain alphanumeric characters"
ValidationExpression="^[a-zA-Z0-9_]*$" SetFocusOnError="True"></asp:RegularExpressionValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="UserName already exist." SetFocusOnError="True" Display="Dynamic"
ControlToValidate="TextBox1" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lastname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lastname") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("lastname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="pass">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pass") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pass") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("pass") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Score" SortExpression="currentScore">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("currentScore") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("currentScore") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("currentScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Best Score" SortExpression="maxScore">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("maxScore") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("maxScore") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("maxScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True"
CommandName="Update" ImageUrl="~/images/update.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="~/images/cancel.png" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True"
CommandName="Insert" ImageUrl="~/images/insert.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="~/images/cancel.png" />
</InsertItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False"
CommandName="Edit" ImageUrl="~/images/edit.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="New" ImageUrl="~/images/new.png"/>
<asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False"
CommandName="Delete" ImageUrl="~/images/delete.png"/>
</ItemTemplate>
<ControlStyle Height="20px" Width="20px" />
</asp:TemplateField>
</Fields>
</asp:DetailsView>
I am able to access the password textbox in the code behind in the "ItemInserting" Event with that code:
protected void DetailsViewSingleUsr_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
DetailsView myDetailsView = (DetailsView)sender;
if (myDetailsView.CurrentMode == DetailsViewMode.Insert)
{
Label9.Text = ((TextBox)myDetailsView.FindControl("TextBox4")).Text;
}
}
But how can I modify? Am I on the right way? Any suggestions?
Thx a lot guys!
ListViewInsertEventArgs contains property Values, in which you can find all of inserting values, obtained from controls. You may just modify those data. For example
e.Values["password"] = "newpassword";
Thanks! I've finally end up with this code:
protected void DetailsViewSingleUsr_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["pass"] = LoginSecurityLogic.Hash_Password_to_MD5(e.Values["pass"].ToString());
}
protected void DetailsViewSingleUsr_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
e.NewValues["pass"] = LoginSecurityLogic.Hash_Password_to_MD5(e.NewValues["pass"].ToString());
}

Disable editing on specific rows in Gridview asp.net

I have one Gridview that Shows my Customer table. In my gridview it's possible for the user to Insert new customers and also edit existing customers.
Now to my question. I have two customers that shouldnt be able for the user to edit but i want them to be showed in the gridview. Can i somehow disable the edit button or something for this customers? For example by saying that where = custID IN (60,61) edit button disabled?
Here is the whole code for my gridview
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataKeyNames="CustID" DataSourceID="Event" GridLines="Horizontal" ShowFooter="True" AllowPaging="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px">
<AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Center" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="CustID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CustID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CustID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ValidationGroup="INSERT" OnClick="lbInsert_Click" ID="lbInsert" runat="server" >Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Anrede" SortExpression="Gender">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="Gender" DataValueField="Gender" SelectedValue='<%# Bind("Gender") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EventConnectionString %>" SelectCommand="SELECT DISTINCT [Gender] FROM [Customer]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label13" runat="server" Text="Anrede"></asp:Label>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem>Anrede Auswählen</asp:ListItem>
<asp:ListItem>Herr</asp:ListItem>
<asp:ListItem>Frau</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvInsGender" runat="server" ErrorMessage="Anrede ist ein Pflichtfeld"
ControlToValidate="ddlGender" ValidationGroup="INSERT" Text="*" ForeColor="Red" InitialValue="Anrede Auswählen">
</asp:RequiredFieldValidator>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEdLastName" runat="server" ErrorMessage="Name ist ein Pflichtfeld"
ControlToValidate="TextBox2" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label14" runat="server" Text="Name"></asp:Label></div>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsLastName" runat="server" ErrorMessage="Name ist ein Pflichtfeld"
ControlToValidate="txtLastName" ValidationGroup="INSERT" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vorname" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEdFirstName" runat="server" ErrorMessage="VorName ist ein Pflichtfeld"
ControlToValidate="TextBox3" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label15" runat="server" Text="Vorname"></asp:Label></div>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsFirstName" runat="server" ErrorMessage="VorName ist ein Pflichtfeld"
ControlToValidate="txtFirstName" ValidationGroup="INSERT" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Strasse" SortExpression="Street">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Street") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEdStreet" runat="server" ErrorMessage="Strasse ist ein Pflichtfeld"
ControlToValidate="TextBox4" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Street") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label16" runat="server" Text="Strasse"></asp:Label></div>
<asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsStreet" runat="server" ErrorMessage="Strasse ist ein Pflichtfeld"
ControlToValidate="txtStreet" ValidationGroup="INSERT" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="House Nr" SortExpression="HouseNr">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("HouseNr") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEdHouseNr" runat="server" ErrorMessage="House Nr ist ein Pflichtfeld"
ControlToValidate="TextBox5" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("HouseNr") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label17" runat="server" Text="House Nr"></asp:Label></div>
<asp:TextBox ID="txtHouseNr" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsHouseNr" runat="server" ErrorMessage="House Nr ist ein Pflichtfeld"
ControlToValidate="txtHouseNr" ValidationGroup="INSERT" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLZ" SortExpression="Zip">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Zip") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEdZip" runat="server" ErrorMessage="PLZ ist ein Pflichtfeld"
ControlToValidate="TextBox6" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Zip") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label18" runat="server" Text="PLZ"></asp:Label></div>
<asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsZip" runat="server" ErrorMessage="PLZ ist ein Pflichtfeld"
ControlToValidate="txtZip" ValidationGroup="INSERT" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ort" SortExpression="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEdCity" runat="server" ErrorMessage="Ort ist ein Pflichtfeld"
ControlToValidate="TextBox7" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label19" runat="server" Text="Ort"></asp:Label></div>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsCity" runat="server" ErrorMessage="Ort ist ein Pflichtfeld"
ControlToValidate="txtCity" ValidationGroup="INSERT" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Telefon" SortExpression="Phone">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Phone") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Phone") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label20" runat="server" Text="Telefon"></asp:Label></div>
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="E-mail" SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label21" runat="server" Text="E-mail"></asp:Label></div>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Firma" SortExpression="Company">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Company") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("Company") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label22" runat="server" Text="Firma"></asp:Label></div>
<asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="Active">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource2" DataTextField="Active" DataValueField="Active" SelectedValue='<%# Bind("Active") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EventConnectionString %>" SelectCommand="SELECT DISTINCT [Active] FROM [Customer]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center;"><asp:Label ID="Label23" runat="server" Text="Status"></asp:Label></div>
<asp:DropDownList ID="ddlActive" runat="server">
<asp:ListItem>Status Auswählen</asp:ListItem>
<asp:ListItem>Aktiv</asp:ListItem>
<asp:ListItem>Passiv</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvInsActive" runat="server" ErrorMessage="Status ist ein Pflichtfeld"
ControlToValidate="ddlActive" ValidationGroup="INSERT" Text="*" ForeColor="Red" InitialValue="Status Auswählen">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#B5C7DE" Font-Bold="True" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
I would suggest to loop through the records using RowDataBound method and then on the CustID field look for 60 and 61 and then disable the entire Row:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dr = e.Row.DataItem as DataRowView;
if(dr["CustID"].ToString() == "60" || dr["CustID"].ToString() == "61")
{
e.Row.Enabled = false; //OR dr.Enabled = false;
//DISABLED Controls only
//((TextBox)e.Row.FindControl("TextBox1")).Enabled = false;
}
}
}
Remember to add OnRowDataBound="GridView1_RowDataBound" in the GridView markup declaration

data grid with edit option error when click on edit

I am getting the following "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." error. How can I avoid this error?
This is my code for editing the data from grid view:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Panel ID="Panel1" runat="server" ScrollBars="Auto" Width="950px">
<asp:Label ID="lblMessage" runat="Server" ForeColor="Red"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="2"
DataKeyNames="DOC_id" ForeColor="Black" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
onrowcommand="GridView1_AddRow" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" Height="392px" ShowFooter="True">
<FooterStyle BackColor="Tan" />
<AlternatingRowStyle BackColor="pink" />
<Columns>
<asp:templatefield showheader="False">
<edititemtemplate>
<asp:linkbutton causesvalidation="True" commandname="Update"
id="LinkButton1" runat="server" text="Update"></asp:linkbutton>
<asp:linkbutton causesvalidation="False" commandname="Cancel"
id="LinkButton2" runat="server" text="Cancel"></asp:linkbutton>
</edititemtemplate>
<footertemplate>
<asp:linkbutton commandname="AddNew" id="AddNew" runat="server">Add
New</asp:linkbutton>
</footertemplate>
<itemtemplate>
<asp:linkbutton causesvalidation="False" commandname="Edit" id="LinkButton1"
runat="server" text="Edit"></asp:linkbutton>
</itemtemplate>
</asp:templatefield>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the
record?')">
<asp:LinkButton ID="lnkB" runat="Server" Text="Delete"
CommandName="Delete"></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOC_id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DOC_id") %>'>
</asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox22" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator22" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox22">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DOC_id") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_Type">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Dr_Type") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox1">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Dr_Type") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_Title">
<EditItemTemplate>
<asp:DropDownList ID="TextBox2" runat="server" selectedvalue='<%#
Bind("Dr_Title") %>'>
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Mr.</asp:ListItem>
<asp:ListItem>Mrs.</asp:ListItem>
<asp:ListItem>Ms.</asp:ListItem>
<asp:ListItem>Fr.</asp:ListItem>
<asp:ListItem>Sr.</asp:ListItem>
<asp:ListItem>Rev.</asp:ListItem>
<asp:ListItem>Dr.</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="*" ControlToValidate="TextBox2">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewTextBox2" runat="server" >
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Mr.</asp:ListItem>
<asp:ListItem>Mrs.</asp:ListItem>
<asp:ListItem>Ms.</asp:ListItem>
<asp:ListItem>Fr.</asp:ListItem>
<asp:ListItem>Sr.</asp:ListItem>
<asp:ListItem>Rev.</asp:ListItem>
<asp:ListItem>Dr.</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator2" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox2">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Dr_Title") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Dr_FirstName")
%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="*" ControlToValidate="TextBox3">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator3" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox3">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Dr_FirstName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_MiddleName">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Dr_MiddleName")
%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="*" ControlToValidate="TextBox4">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox4" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator4" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox4">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Dr_MiddleName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Dr_LastName") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="*" ControlToValidate="TextBox5">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox5" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator5" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox5">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Dr_LastName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_Suffix">
<EditItemTemplate>
<asp:DropDownList ID="TextBox6" runat="server" selectedvalue='<%#
Bind("Dr_Suffix") %>'>
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Sr</asp:ListItem>
<asp:ListItem>Jr</asp:ListItem>
<asp:ListItem>III</asp:ListItem>
<asp:ListItem>IV</asp:ListItem>
<asp:ListItem>DMD</asp:ListItem>
<asp:ListItem>DO</asp:ListItem>
<asp:ListItem>DDS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>PHD</asp:ListItem>
<asp:ListItem>DVM</asp:ListItem>
<asp:ListItem>Esq</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ErrorMessage="*" ControlToValidate="TextBox6">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewTextBox6" runat="server">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Sr</asp:ListItem>
<asp:ListItem>Jr</asp:ListItem>
<asp:ListItem>III</asp:ListItem>
<asp:ListItem>IV</asp:ListItem>
<asp:ListItem>DMD</asp:ListItem>
<asp:ListItem>DO</asp:ListItem>
<asp:ListItem>DDS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>PHD</asp:ListItem>
<asp:ListItem>DVM</asp:ListItem>
<asp:ListItem>Esq</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator6" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox6">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Dr_Suffix") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_Degree">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Dr_Degree") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ErrorMessage="*" ControlToValidate="TextBox7">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox7" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator7" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox7">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Dr_Degree") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_PreferredUsername">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%#
Bind("Dr_PreferredUsername") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ErrorMessage="*" ControlToValidate="TextBox8">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox8" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator8" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox8">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Dr_PreferredUsername")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_ContactNumber">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Dr_ContactNumber")
%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ErrorMessage="*" ControlToValidate="TextBox9">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3"
runat="server"
ControlToValidate="TextBox9"
ErrorMessage="Invalid Contact Number....!!" ForeColor="Red"
ValidationExpression="^[9,8,7][0-9]{9}$"></asp:RegularExpressionValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox9" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator9" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox9">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4"
runat="server"
ControlToValidate="NewTextBox9"
ErrorMessage="Invalid Contact Number....!!" ForeColor="Red"
ValidationExpression="^[9,8,7][0-9]{9}$"></asp:RegularExpressionValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Dr_ContactNumber")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_FaxNumber">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Dr_FaxNumber")
%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
ErrorMessage="*" ControlToValidate="TextBox10">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox10" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator10" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox10">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("Dr_FaxNumber") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_EmailAddress">
<EditItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("Dr_EmailAddress")
%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server"
ErrorMessage="*" ControlToValidate="TextBox11">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5"
runat="server"
ControlToValidate="TextBox11" ErrorMessage="Invalid Email ID....!!"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewTextBox11" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator11" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox11">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6"
runat="server"
ControlToValidate="NewTextBox11" ErrorMessage="Invalid Email ID....!!"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("Dr_EmailAddress")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dr_SpecialtyArea">
<EditItemTemplate>
<asp:DropDownList ID="TextBox12" runat="server" selectedvalue='<%#
Bind("Dr_SpecialtyArea") %>'
DataSourceID="SqlDataSource3" DataTextField="Speciality"
DataValueField="Speciality">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT [Speciality] FROM [Doc_Speciality]">
</asp:SqlDataSource>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server"
ErrorMessage="*" ControlToValidate="TextBox12">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewTextBox12" runat="server"
DataSourceID="SqlDataSource3" DataTextField="Speciality"
DataValueField="Speciality">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT [Speciality] FROM [Doc_Speciality]">
</asp:SqlDataSource>
<asp:RequiredFieldValidator ID="NewRequiredFieldValidator12" runat="server"
ErrorMessage="*" ControlToValidate="NewTextBox12">
</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("Dr_SpecialtyArea")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
</asp:Panel>
<asp:LinkButton ID="AddNew" runat="server" Text="AddNew"
onclientclick="imgbtn_Click" onclick="AddNew_Click"></asp:LinkButton>
</asp:Content>`
Try this solution:
http://w3ka.blogspot.com/2007/09/dropdownlist-in-gridview.html
You have to create a DataBound event for the DropDownLists that use the SqlDataSource and set the SelectedValue there.

Resources