If my concerns abouttemplating aDetailsView are correct - no support -, any "best practice" to validate data when updating/inserting new records?
I'm guessing this have to be made handling the events Updating/Inserting, but just to verify.
Thanks.You might also want to check out thevalidation controls.
Hello,
well, the presumption about validating theDetailsView in the Inserting/Updating events was because I thought we could not use item templates.
Anyway, if using the default display that this control offers, could you please show me an example of how validate a field of a detailsview using declarative code? How do I declare the attribute ControlToValidate of a Validation control?
<asp:RangeValidator id="rngValDate" runat="server"ControlToValidate="???" ... />
There's anexample in the quickstarts (srchere) that shows how to use validators with databound controls.
Hi again, and thanks about the time you are spending with this.
A final question. Please take into account the following scenario, note that AutoGenerateRows is set to True in my DetailsView control and presume a query returning a record with several fields:
[...]
<asp:SqlDataSource runat="server" Id="sqldtsdv"
connectionString='<%$ connectionStrings:MyconnString%>'
SelectCommand="Select..."
InsertCommand="Insert..."
UpdateCommand="Update..."/>
<asp:DetailsView runat="server" Id="dv" DataSourceId="sqldtsdv" DataKeyNames="mydatakey"
AutoGenerateRows="True"
AllowPaging="True"
AutoGenerateInsertButton="True"
AutoGenerateUpdateButton="True" />
[...]
How can we validate using validation controls in this scenario?
Thanks once more.
It would be a lot harder to get validators to work with autogenerated rows than if use the validators in the templatefields like the examples show. There are a few issues that you'll run into by trying to do it your way. The id of the control to validate isn't going to be predictable. (You can get it by looking at the source when the page is run, and it'll be something like DetailsView1$ctl02, but it could change) And if you do have the id, you have the problem of getting the validator somewhere that only appears in edit mode, since that control won't appear in readonly mode, which will cause an error. You might be able to code out a solution that will work, but it's much easier to do it with templatefields. Another option (without using the validator controls) is to handle the updating event and manually do verification of the values there.
Hi, I was looking for a solution for this and it happens that I think I find one:
ProtectedSub DetailsView_OnItemUpdated(ByVal senderAsObject,ByVal eAs DetailsViewUpdatedEventArgs)
IfNot e.ExceptionIsNothingThen
lblError.Text ="Error: Could not update record! " & (e.Exception.Message.ToString) &"..."
e.ExceptionHandled =True
e.KeepInEditMode =TrueElse
lblError.Text =""
EndIf
EndSub
Insert a label lblError on the page. The code is similar for DetailsView_OnItemInserted.
regards
FN
No comments:
Post a Comment