Hi. I have a GridView and a DetailsView. When an item is selected in the GridView, it puts the details view in insertmode and sets the pageindex to the selected index in the GridView. Otherwise, the DetailsView is in insert mode.
I expanded one field in the details view (so far) to a TemplateField. I only have an <ItemTemplate> since that field is the same wether I am inserting or updating. I added a validator control, but it doesn't fail validation. It always continues and tries to insert a null value into the database which fails since nulls aren't allowed for that field. I added a "DetailsView1_ItemInserting" handler, and Page.IsValid is always true.
Can anyone see what I am doing wrong?
<asp:TemplateFieldHeaderText="Name:"SortExpression="Name">
<ItemTemplate>
<asp:TextBoxWidth="270px"MaxLength="255"ID="TextBoxName"runat="server"Text='<%# Bind("Name") %>'></asp:TextBox>
<asp:RequiredFieldValidatorID="NameRequired"runat="server"ControlToValidate="TextBoxName"
ErrorMessage="Project name is is required."ToolTip="Name is required."ValidationGroup="ValidationGroup1">*</asp:RequiredFieldValidator>
</ItemTemplate>
<ItemStyleWidth="310px"/>
</asp:TemplateField>
do a trace -> put trace="true" in your @.page directive and go to the bottom the real name of your textbox will appear try that
ctrl_00_textboxname etc...
Looking in the trace the textbox's name is "DetailsView1$TextBoxName". But what do I do with that? That looks like the name of the HTML element:
<input name="DetailsView1$TextBoxName" type="text" ...
The validator is right after that, but it set to invisible. Could it be disabled?
<span id="DetailsView1_NameRequired" title="Name is required." style="color:Red;visibility:hidden;">*</span>
From the trace:
Thanks for your help!
Brian
I had to put this handler in:
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
Validate();
if (!IsValid)
{
e.Cancel = true;
return;
}
}
I cannot see why I had to call "Validate". I added CausesValidation="true" to the textbox control I want validate, but it shouldn't have to. I have validation on a page that creates a new user (expanded to a template, from the CreateUserWizard) and it works.
Brian
No comments:
Post a Comment