I need to do a validaton for a textfield in a detailsview. I have used "custom validator" and want to fire the "server_validate" event. but the "server_validate" event does not fire. I have google for sometime and came to know that it is not possible. However
i need to understand why it is not possible??
Any ideas??[8-)]
Where did you find that’s not possible? This is normal practice, so it should work!
kamii47
http://forums.asp.net/t/1102008.aspx
After seeing the following post only i posted for clarity.
I tried my self by adding a detailsview and a customvalidator and "server_valitdate" function for the custom validator. but the point is the "server_validate" function is never hit.. I would like to know why it was not possible.??
Show an example of the detailsview and the server_validate function?
okay here is code
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text=’<%# Eval("Id") %>’></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text=’<%# Bind("Id") %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=’<%# Bind("Name") %>’></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
EnableClientScript="False" ErrorMessage="*">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text=’<%# Bind("Name") %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Position" SortExpression="Position">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text=’<%# Bind("Position") %>’></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text=’<%# Bind("Position") %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [MovieCategories]"
UpdateCommand="UPDATE [MovieCategories] SET [Name] = @Name, [Position] = @Position WHERE [Id] = @Id">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Position" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
code behind
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
TextBox tb = (TextBox)DetailsView1.FindControl("TextBox1");
if (tb.Text.Length > 0)
{
if (args.Value.Length < 1)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}
}
Any help???
Where is the customvallidator that calls the function? [8-|]
And what exactly is it that you want the validator to do?
Yes From mentioned code we can’t see the custom validator with CustomValidator1_ServerValidate function/event .Is any thing missing in your code ?
kamii47
Yes From mentioned code we can’t see the custom validator with CustomValidator1_ServerValidate function/event .Is any thing missing in your code ?
it is there in the post. but i am giving for reference
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
TextBox tb = (TextBox)DetailsView1.FindControl("TextBox1");
if (tb.Text.Length > 0)
{
if (args.Value.Length < 1)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}
}
A, now I understand the problem!
You made a function, but you forgot to put the Custom Validator in the HTML Markup. Just Add:
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="ErrorMessage" OnServerValidate="CustomValidator1_ServerValidate" />
Ganesh Jee ,
I can’t see the CustomValidator where CustomValidator1_ServerValidate is binded.
Is there any asp:CustomValidator control with above server validation function is present in your markup ?
kamii47
Ganesh Jee ,
I can’t see the CustomValidator where CustomValidator1_ServerValidate is binded.
Is there any asp:CustomValidator control with above server validation function is present in your markup ?
here is the code
<EditItemTemplate>
<asp:TextBox
ID="TextBox1"
runat="server"
Text=’<%# Bind("Name") %>‘></asp:TextBox>
<asp:CustomValidator
ID="CustomValidator1"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage="CustomValidator"
OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
Sorry, i might have missed out while pasting the code.
Are you sure this is not firing? Do you have a breakpoint on the first line of your event handler? Maybe the DetailsView1 is in insert mode and your code is working perfectly.
try to set the OldValuesParameterFormatString Property in the SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [MovieCategories]"
OldValuesParameterFormatString="{0}"
UpdateCommand="UPDATE [MovieCategories] SET [Name] = @Name, [Position] = @Position WHERE [Id] = @Id">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Position" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Hi Ganesh ,
Try to add ValidateEmptyText="True"
Otherwise , it will not fire the event hanlder when the textbox is empty.
Samu Zhang – MSFT
Hi Ganesh ,
Try to add ValidateEmptyText="True"
Yes. it is Server side event is fireing nw.
We Are Glad that u have finally solved your problem.
Ganesh Jee Please mark the Post that help you in solving your problem as answer.[not your own post [:D]]
I tried this, but i want to use custom validator in insert mode but still not firing
any suggestions? will it fire inserttemplate?
Yerbol
I tried this, but i want to use custom validator in insert mode but still not firing
any suggestions? will it fire inserttemplate?
If you” ve a question. please start your own thread?
http://www.mikesdotnetting.com/Article/69
I was having the same issue with the CustomValidator not firing while a DetailsView was in Insert mode. Some of the responses above helped to to figure out more of what is going on. Here is what I’ve found: the CustomValidator will only fire after all the
other validator types on the form have fired and validate successfully.
To explain what I mean consider this example:
<asp:GridView ID="gvNews" runat="server" Width="100%" DataSourceID="sqlNews" DataKeyNames="newsId" CellPadding="4" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ButtonType="Button" SelectText="View/Edit" ShowSelectButton="true" ItemStyle-Width="75px" />
<asp:TemplateField HeaderText="Date">
<ItemStyle Width="160px" />
<ItemTemplate>
<asp:Literal ID="gvDate" runat="server" Text=’<%# Eval("newsDate", "{0:MMMM dd, yyyy @ hh:mm tt}") %>’ />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Literal ID="gvTitle" runat="server" Text=’<%# Eval("Title") %>’ />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div style="margin-top: 12px;">
<asp:DetailsView ID="dvNews" runat="server" Width="100%" DataSourceID="sqlNewsDetails" DataKeyNames="newsId" CellPadding="6" AutoGenerateRows="false">
<FieldHeaderStyle Width="100px" />
<Fields>
<asp:CommandField ButtonType="Button" EditText="Edit" UpdateText="Save Changes" CancelText="Cancel" ShowEditButton="true" />
<asp:TemplateField HeaderText="Title*">
<ItemTemplate>
<asp:Literal ID="dvTitle" runat="server" Text=’<%# Eval("title_EN") %>’ />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Width="350px" MaxLength="75" Text=’<%# Bind("title_EN") %>’ />
<asp:RequiredFieldValidator ID="rfvTitle" runat="server" ControlToValidate="txtTitle" CssClass="errorMessage" Display="Dynamic">What is the Title?</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Video">
<ItemTemplate>
<asp:Literal ID="dvVideo" runat="server" Text=’<%# If(String.IsNullOrEmpty(Eval("videoId").ToString), "", String.Format("({0}) {1}", Eval("streamingProviderName"), Eval("videoId"))) %>’ />
</ItemTemplate>
<EditItemTemplate>
<div>
<asp:DropDownList ID="ddlVideoProvider" runat="server" Width="355px" DataSourceID="sqlVideoProviders" DataTextField="streamingProviderName" DataValueField="providerId" SelectedValue=’<%# Bind("videoProviderId") %>’ AppendDataBoundItems="true">
<asp:ListItem Text="Select a Streaming Video Provider from the list…" Value="" />
</asp:DropDownList>
<asp:CustomValidator ID="cvVideoProvider" runat="server" ControlToValidate="ddlVideoProvider" OnServerValidate="validateVideoProvider" ValidateEmptyText="true" CssClass="errorMessage" Display="Dynamic">If you select a Video Provider, you must enter
a Video ID.</asp:CustomValidator>
</div>
<div>
<asp:TextBox ID="txtVideoId" runat="server" Width="350px" MaxLength="25" Text=’<%# Bind("videoId") %>’ />
<asp:CustomValidator ID="cvVideoId" runat="server" ControlToValidate="txtVideoId" OnServerValidate="validateVideoId" ValidateEmptyText="true" CssClass="errorMessage" Display="Dynamic">If you enter a Video Id, you must select a Video Provider.</asp:CustomValidator>
</div>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="sqlNews" runat="server" ConnectionString="<%$ ConnectionStrings: ConnString %>"
SelectCommand="SELECT newsId, Title_EN AS Title, newsDate FROM cms_News ORDER BY newsDate DESC" SelectCommandType="Text">
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlNewsDetails" runat="server" ConnectionString="<%$ ConnectionStrings: ConnString %>"
SelectCommand="SELECT newsId, title_EN, videoId, videoProviderId, streamingProviderName FROM cms_News n LEFT JOIN cms_VideoProviders vp ON n.videoProviderId=vp.providerId WHERE newsId=@newsId" SelectCommandType="Text"
UpdateCommand="UPDATE cms_News SET title_EN=@title_EN, videoId=@videoId, videoProviderId=@videoProviderId WHERE newsId=@newsId" UpdateCommandType="Text">
<SelectParameters>
<asp:ControlParameter Name="newsId" Type="Int32" ControlID="gvNews" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlVideoProviders" runat="server" ConnectionString="<%$ ConnectionStrings: ConnString %>"
SelectCommand="SELECT providerId, streamingProviderName FROM cms_VideoProviders ORDER BY streamingProviderName" SelectCommandType="Text">
</asp:SqlDataSource>
This is a fully functioning demo. All you need to do is create a database with the two tables showing in the SqlDataSource statements. The idea is the GridView shows all of the news items. Each news item can have an embedded video associated with it. In
order to provide flexibility on the embedded videos, the VideoProvider table defines all the available video providers. The CustomValidators make sure that if a Video Provider is selected then a VideoId is entered and visa versa.
Notice how the Title field is marked as a required field and has a RequiredFieldValidator attached to it. If the Title field has an entry in it (and therefore the RequiredFieldValidator is successful) then, and only then will the CustomValidators fire and
make sure the the Video stuff is all there if an entry is made in one or the other field. If the Title field is empty, then only the RequiredFieldValidator will fire and none of the error messages for the DropDownList or the TextBox will fire.
This holds true for Edit mode or Insert mode.
I hope this helps others understand a bit better what is happening and when to expect the CustomValidators to fire.
rweiler
I was having the same issue
And did read my previous answer?