I have a GridView that contains a TemplateField with a TextBox in it (permanently in edit mode, you might say). The data from the GridViewRow is updated in the database automatically when the TextBoxChanged event fires. I am looking for a way to add a RangeValidator to the TextBox so that it will not allow values greater than a BoundField from the same row in the GridView.

I have other validators working on this TextBox, including a manually set RangeValidator. However, in all my googling, I haven't found any examples of making a RangeValidator dynamically pull from a TextBox, so I'm not sure if this is even possible. If it can pull from a TextBox, is there a way to get that value from a GridView's BoundField?

有帮助吗?

解决方案

Figured out how to do it within a GridView (still no idea how it might work outside the GridView). Basically it comes down to adding in a data binding section where the max value would be entered (similar to binding a value to the TextBox itself):

<asp:RangeValidator ID="ShareTextBoxRangeValidator" runat="server" 
    ErrorMessage="!" ControlToValidate="ShareTextBox" 
    MaximumValue='<%# Eval("Cost") %>' MinimumValue="0" 
    Display="Dynamic" ClientIDMode="Predictable" Type="Double"/>

I found that Bind and Eval work equally well, but left it at Eval since it won't be updating the data.

A further note about Validators and GridViews: If you have set your page's ClientIDMode to Static, you must explicitly set it back to Predictable (or AutoID) on both the Validator and the control to be validated. Otherwise you will get key violation errors.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top