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.
Related
I am using asp.net 4 and c#.
I have a gridview with a textbox that needs a unique value for the database. This value is to sort the data by precendence with in the database.
All my other validators work but they only check the value in the one box.
How do I validate that the integers are unique in that column.
The only thing I found to do this is "DataKeyNames" but that does not stop it from allowing repeating numbers.
Updating the order is done on button click.
Thanks for info.
You can use a customvalidator for validating this. In the CustomValidator's OnServerValidate function you can repeat through all the rows of the grid view, get the textbox and its value and perform the unique check.
For the Integer validation you can add a CompareValidator with Operator="DataTypeCheck" Type="Integer"
I suppose you could write a JavaScript method that will scan the contents of the grid onclientclick before submit.
I have a details view that is in "insert Mode" so the user just sees blank spaces to enter values. I have two drop down lists and I wanted to have the second ddl change its value by what was selected in the first ddl. I tried setting ddl1 to a label so ddl2 would change when the label changed. The problem I am having now is that I need autopostback to update the value of the label, but selecting "autopostback" on the ddl1 makes my code throw a data binding error.
I was wondering if there was any way I could get around using autopostback and still update values selected in the first ddl to the label.
Thank you.
Try using AjaxControlToolkit. It has a feature to cascade ddlists. Use updatepanel as container for both of ddls so you can omit autopostback.
Your query is not completely clear. But if you want to change the dd2 value on change event of dd1, you can use the following code:
$("#<%= statusDDL.ClientID %>").val($("#<%= dd1.ClientID %>option:selected").text() );
It is not clear whether you want value or text property.
Also I am not 100% that this syntax will work. But obviously it can be done using this concept searching on net for your requirement
in a DetailsView with the mode set to Insert, I had a textbox that is populated by an event, rather than allowing the user to type into it. Problem is, if I set the TextBox to either Enabled="false" or ReadOnly="true", it doesn't seem to populate the SqlDataSource parameter with the value of the textbox, which I need. Is there any other ways to stop users inputting data into that textbox, but not use Enabled/ReadOnly?
Thanks
You could make it readonly with JavaScript:
TextBox1.Attributes.Add("readonly", "readonly")
I'm not sure if this approach solves your problem with the SqlDataSource.
I have a radGrid on the page with the "Add New Record" button. When I click the "Add New Record" button, a textbox appears above each column that allows me to enter values. I want to limit the number of characters that can be entered in the textbox. How do I set the MaxLength of those textboxes?
You're not telling me exactly what the problem is I don't think. You very-well could try to use the MaxLength property (if it's not a multiline TextBox). But is that really your problem? Or do you already know how you can accomplish this, but you're having trouble getting the control via server-side or client-side code so that you can set the MaxLength property??
If you could provide a code snippet or two and some more details regarding your problem, I'd be better-able to help you.
I have a datalist inside a usercontrol that gets loaded into a page where users can customize a report based on some checkboxes.
One of the checkboxes, however, is "Hide Worklog" which should hide the worklog column from the result set because it can be quite long and interfere with the report.
If I do:
datatable1.Columns.Remove("WorkLog");
the code throws an exception because:
<asp:Label ID="WorkLog" runat="server" Text='<%# Bind("WorkLog") %>'></asp:Label></td>
doesn't exist.
Am I going about the usercontrol all wrong? This usercontrol should always be able to show the worklog, so I don't think it's bad to bind it in there, but at the same time I want to be able to hide it if the user wants.
Try removing the label control from your DataList instead of removing the column from the data source (i.e. the DataTable)
DataList1.Controls.Remove(DataList1.FindControl("WorkLog"));
You shouldn't get an error if the data source has more columns than you're displaying on the page, however, you will get an error, as you've discovered, if you're trying to display a column that doesn't exist in the data source.
bind it in code behind after checking some condition. like
if (visible) {
//bind
}
while removing control
visible = false;
you might need to change visible to session var :)