ASP.NET - Get control in RowCommand event - asp.net

Trying to access a RequiredFieldValidator control that's inside a GridView in the RowCommand event and having trouble.
Here's the partial GridView code:
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password" Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
As you can see, there's a RFV for the EditTemplate and FooterTemplate. My issue is this; when the page loads, it has all the records in it, including an emtpy row at the bottom (Footer). If I click Edit on a populated row, the data is populated correctly, then when I hit UPDATE, I get all the error messages from the FOOTER RFV's firing off, which isn't correct. So, in the RowCommand event, I'd like to attempt this: If the user clicks the EDIT button, then disable all the RFV's in the footer row (the add new row), if they click anything else, enable them.
Ideas?
Sorry, meant to put this in the first time. In the RowCommand event, I am able to find the control but when I set the properties to something bogus, it seems to get overridden later, by the RowDataBound event:
RequiredFieldValidator rfv = (RequiredFieldValidator)gvUsers.FooterRow.FindControl("rfvAddWebLogin");
rfv.ControlToValidate = string.Empty;
rfv.ErrorMessage = "sdfgsdfgsdgsdfgsdfgsdfgsdfg";
rfv.Enabled = false;

You should use different ValidationGroups in your EditItemTemplate and FooterItemplate:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="BtnEdit" CausesValidation="False" Text="Edit" CommandName="Edit" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" ValidationGroup="UpdateUser" Text="Update" CommandName="Update" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" ValidationGroup="InsertUser" Text="Add" CommandName="Insert" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password"
Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" ValidationGroup="UpdateUser" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" ValidationGroup="UpdateUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" ValidationGroup="InsertUser" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" ValidationGroup="InsertUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic7
Note: If you're using ValidationSummaries, you need to add the appropriate ValidationGroup to every ValidationSummary. If you leave this property blank, only controls without a specified ValidationGroup will be listed.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.validationgroup.aspx

Related

Required field validation issues in ASP.Net grid view

I have a text box in grid view control,i used required field validator to validate the textbox like
<asp:TemplateField HeaderText="Relationship" ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtRelationShip" runat="server" Text='<%# Eval("RelationShip") %>' Visible="false" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvRelationShip" runat="server" ControlToValidate="txtRelationShip" ForeColor="Red" ClientIDMode="AutoID"
ErrorMessage="Please enter relationship" ValidationGroup="vgUpdateRelationShip" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:LinkButton ID="btnRelationShipUpdate" runat="server" ValidationGroup="vgUpdateRelationShip"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I have many rows in my grid view , when i edit and update the text box value if single text box is empty it shows error in all text box.
How to resolve this problem, i want if single text box is empty shows error only in single text box
You have to make sure that the ValidationGroup is unique per row, like ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1" ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'></asp:RequiredFieldValidator>
<asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'>Update</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Required Field validator in edit item template is not working

This is my code.I am using a gridview in asp.net.I am trying to add a requiredfieldvalidator to the
textbox_id
in gridview edit mode but nothing happens even if I dont put anything in the textbox and click update.
<Columns>
<asp:BoundField DataField="StudentId" HeaderText="Student_ID" />
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox_id" runat="server" Wrap="False" CausesValidation="true" ValidationGroup="a"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="a" runat="server" ControlToValidate="TextBox_id" ErrorMessage="RequiredField" ForeColor="Red"></asp:RequiredFieldValidator>
</EditItemTemplate>
<%--<ItemTemplate>
<asp:Label ID="Label3" runat="server"></asp:Label>
</ItemTemplate>--%>
</asp:TemplateField>
The button that triggers the validation should have
ValidationGroup attribute set to "a" as well.
Try this:
<asp:TemplateField HeaderText="Popolazione residente"
SortExpression="InhabitantsNum">
<EditItemTemplate>
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"InhabitantsNum") %>
</itemtemplate>
<asp:TextBox ID="InsertPopolazioneResidente" runat="server"
Text='<%# Bind("InhabitantsNum") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Inserire un numero positivo" ValidationExpression="^[0-9]+$" ForeColor="Red" ControlToValidate="InsertPopolazioneResidente"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="InsertPopolazioneResidente" runat="server"
Text='<%# Bind("InhabitantsNum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
To summarise, to get validation to work in an Edit Template you need:
A ValidationGroup in the control (Eg TextBox)
The same ValidationGroup in the Validator
The same ValidationGroup in the button etc that triggers the action
CausesValidation="true" in the button etc in step 3

How to place a required field validator inside a GridView TextBox

I have a GridView with some TemplateField items containing TextBox controls. I would like to add a required field validator on it. This is my code:
<asp:TemplateField HeaderText="vid">
<EditItemTemplate>
<asp:TextBox ID="txtvid" runat="server" Width="150px"
Text='<%# Bind("vid") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label
ID="lblvid" runat="server"
Text='<%# Bind("vid") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
How do I place a required field validator on txtvid?
In the Edit template, add a RequiredFieldValidator like this:
<EditItemTemplate>
<asp:TextBox ID="txtvid"
runat="server" Width="150px"
Text='<%# Bind("vid") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator
ControlToValidate="txtvid"
runat="server"
ErrorMessage="Please enter a 'vid' number"
Text="*"/>
</EditItemTemplate>
Here is the reference for the RequiredFieldValidator on MSDN.
UPDATE:
If you wanted a regular expression validator, its pretty much the same, but with the RegularExpressionValidator control:
<asp:RegularExpressionValidator
ControlToValidate="txtvid"
ValidationExpression="\d{10}"
runat="server"
ErrorMessage="Please enter a 'vid' of 10 digits"
Text="*"/>
Here is a complete list of the functionality for the RegularExpressionValidator on MSDN.
Within gridview i assign textbox,requiredfieldvalidator and button,This validator validate all textboxes in gridview when button click without filling textbox. How can i solve this..
<asp:TemplateField HeaderText="vid">
<ItemTemplate>
<asp:TextBox ID="txtvid" runat="server" Width="150px" ValidationGroup ="subgrp">
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvQuantity" ControlToValidate="txtvid" runat="server"
ErrorMessage="Required" ForeColor="Red"
ValidationGroup = "subgrp"></asp:RequiredFieldValidator>
<asp:Label
ID="lblvid" runat="server"
Text='<%# Bind("vid") %>'>
</asp:Label>
<asp:Button ID="btnSelect" runat="server" Text="Select" ValidationGroup ="subgrp"/>
</ItemTemplate>
</asp:TemplateField>
This will validate all textboxs in gridview,When i click button in particular row without filling the textbox in itemtemplate.

In asp.net how to add meno or dropdown control in asp.net?

Data gridview bind the recored with data set.Gird view consist the column "Compnayname"
whenever user click on the column of gridview "Company name" then menu or dropdown control will be display.
so how to add dropdown or menu control in gridview.
I want to data display in grid view control and user click on company name then Menu will be display which contain the information like Send msg, save company detail.
You have to make Template Column
<asp:TemplateField HeaderText="Compnayname">
<ItemTemplate>
<asp:DropDownList ID="ddlCompany" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
here isthe complete GRIDView
<asp:GridView ID="grdList" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="grdList_RowCancelingEdit" OnRowEditing="grdList_RowEditing" OnRowUpdating="grdList_RowUpdating" OnPageIndexChanging="grdList_PageIndexChanging" AllowPaging="true">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label SkinID="OneColLabel" ID="lblName" runat="server" HeaderText="Edit" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnbEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnbUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="lnbCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="drpEditName" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to bind Dropdownlist on code behind

ASP.NET AJAX Control Toolkit ValidatorCallout not working on edit template

I have a page that has a listview that is used for inserting and editing records.
Assigning a RequiredFieldValidator and ValidatorCallOutExtender to the InsertItemTemplate works well.
When I try to do the same on the EditItemTemplate the ValidatorCallOut appears but with no text in the box.
Is there something that I'm doing wrong?
My code for the InsertItemTemplate:
<asp:TextBox ID="date_timeTextBox" runat="server" Text='<%# Bind("date_time") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="insert_into">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1"
runat="server"
TargetControlID="RequiredFieldValidator1">
</cc1:ValidatorCalloutExtender>
And for the EditItemTemplate:
<asp:TextBox
ID="date_timeTextBox"
runat="server"
Text='<%# Bind("date_time","{0:yyyy-MM-dd}") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="reqDTT"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="edit_validate">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender
ID="val_reqDTT"
runat="server"
TargetControlID="reqDTT">
</cc1:ValidatorCalloutExtender>
Make sure your ID's are unique across your Templates, so the ControlToValidate="date_timeTextBox" is different.
InsertTemplate
<asp:TextBox ID="date_timeTextBoxInsert" runat="server" Text='<%# Bind("date_time") %>' />
EditTemplate
<asp:TextBox ID="date_timeTextBoxEdit" runat="server" Text='<%# Bind("date_time") %>' />

Resources