Other ways of 'disabling' a textbox against user input - c#

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.

Related

How do I Compare values in column of a gridview

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.

Determine a RangeValidator max value based on another control

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.

Can values from dropdownlists in detailsview be updated without autopostback?

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

Getting a value in a textbox from a dropdown list

I have a textbox named textbox1 and a dropdownlist. Dropdownlist contains the list of branches and i want that when i select a branch from dropdown i want to get the respective branch code to be generated in a textbox from the Database or from c# coding. How will it be possible ?
Am I missing something, is it more complex that just putting an event handler on the dropdown so that when it's value changes it calls your method that can do whatever it needs to do to generate the string for the textbox?
Skipping passed the "database" portion of your question (and assuming your data isn't bound to the DropDownList)..
One way in ASP.NET, to accomplish this, I believe, is to have your code print DropDownList.SelectedValue's value in the textbox during an event.
Here is a link showing how to bind an event to DropDownList in jQuery

Telerik RadGrid - How do I set the MaxLength for the Insert Textbox

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.

Categories

Resources