Limit the amount of lines in a multi line asp.net textbox - asp.net

On my asp.net web page I have a large multi line textbox where I want the user to be able to only fill in a maximum of 5 lines of text. (If the user were to press enter while on the 5th line it would simply not let him.)
The code I have for the textbox:
<asp:TextBox textmode="multiline"
runat="server"
ID="TextBox1"
name="TextBox1"
style="OVERFLOW:hidden; height:165px; width:95%; resize:none;">
</asp:TextBox>
I have tried it with the Rows property and several JavaScript functions, with no success.
Is this possible with an asp.net textbox or is there any other type of textbox I could use in this case?
Thanks.

I don't think this is possible with the asp:TextBox without some extra work. FYI, the "Rows" and "Columns" properties are only a display metrics and won't restrict the number of characters or carriage returns. The MaxLength property is also well documented to be disabled when using "multiline" with an asp:Textbox.
However, you do have a few options shown below. Note: typically input is validated by length because you don't want it to exceed the field size in the database tables. Hopefully these examples will help.
1) Roll your own Javascript validator.
(this example is limited to validating length, not carriage returns, but you could customize further.)
<asp:TextBox textmode="multiline"
runat="server"
ID="TextBox1"
name="TextBox1"
onkeypress="return EnforceFieldLengthMax(this,10)"
style="OVERFLOW:hidden; height:165px; width:95%; resize:none;">
</asp:TextBox>
<script language="javascript">
function EnforceFieldLengthMax(txt, maxLen)
{
if (txt.value.length > (maxLen - 1))
{
return false;
}
}
</script>
2) Regular Expression validator
(Unfortunately, this won't fire until the control loses focus, so I don't think this one is what you're looking for, but here is an example just in case)**
<asp:TextBox textmode="multiline"
runat="server"
ID="TextBox2"
name="TextBox2"
style="OVERFLOW:hidden; height:165px; width:95%; resize:none;">
</asp:TextBox>
<asp:Label AssociatedControlID="txtValidateMe"></asp:Label>
<asp:RegularExpressionValidator ID="TextBox2Validator"
ControlToValidate="TextBox2" ErrorMessage="Entry can't exceed 20 characters"
ValidationExpression="^[\s\S]{0,20}$" runat="server" Display="Dynamic" SetFocusOnError="true" />
3) Use the HTML TextArea object which does enforce MaxLength
<textarea maxlength="50"></textarea>

Related

How can I achieve the effect of multiple validation groups on a single validation control?

I need to be able to apply multiple validation groups to some of the controls on my page, because for each of the three submit "modes", there is a different set of required fields.
I have three submit buttons, and each one has its own validation group (reject, approve, and save). "Reject" doesn't actually have any required fields currently, but may in the future. "Save" only requires "first name" and "last name", while "approve" requires "first name", "last name", as well as "group name".
I tried simply putting multiple validation groups in the attribute value (as in the code below), but the validation didn't fire at all.
I also tried not specifying a group at all in an attempt to just have the validation always fire, but again again, it didn't fire at all.
<div>
<label for='<%= txt_FirstName.ClientID %>'>First Name</label>
<asp:TextBox ID="txt_FirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_FirstName" runat="server" text="Required"
ControlToValidate="txt_FirstName" ValidationGroup="approve save">
</asp:RequiredFieldValidator>
</div>
<div>
<label for='<%= txt_LastName.ClientID %>'>Last Name</label>
<asp:TextBox ID="txt_LastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_LastName" runat="server" text="Required"
ControlToValidate="txt_LastName" ValidationGroup="approve save">
</asp:RequiredFieldValidator>
</div>
<div>
<label for='<%= ddl_GroupName.ClientID %>'>Group Name</label>
<asp:DropDownList ID="ddl_GroupName" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfv_GroupName" runat="server" text="Required"
ControlToValidate="ddl_GroupName" ValidationGroup="approve">
</asp:RequiredFieldValidator>
</div>
<div>
<asp:Button ID="btn_Reject" runat="server" Text="Reject"
ValidationGroup="reject" OnClick="btn_Reject_Click" />
<asp:Button ID="btn_Approve" runat="server" Text="Approve"
ValidationGroup="approve" OnClick="btn_Approve_Click" />
<asp:Button ID="btn_Save" runat="server" Text="Save"
ValidationGroup="save" OnClick="btn_Save_Click" />
</div>
I'm certain I can achieve the desired effect with some additional code, or duplicated validator controls for each validation group, but I was hoping the webforms framework would have a simpler, built-in way to do this.
UPDATE (2018-01-02):
The question "Is it possible to assign Multiple Validation Groups to a single Validation Control?" is a special case of my own. It has a single control in multiple groups, whereas I have multiple controls with multiple overlapping groups. Additionally, the answer proposes having a validation control for each input/group combination. This is a terrible solution because, among other things, it violates the DRY (don't repeat yourself) programming principle and makes maintenance/enhancements more difficult.
The above also links the question "How to validate against Multiple validation groups?". That question is a completely different issue (validation groups within validation groups), and its answer proposes custom javascript that would need to be written for every submit button, on every page that requires multiple validation groups.
I am looking for a general case solution that I can write/implement once and apply to the entirety of the web application, and thus I do not consider my question a duplicate of the above mentioned questions.
UPDATE (2018-01-03):
Between other questions/articles I've encountered while researching my problem, as well as the gracious assistance of a.bajorinas and Sunil, I've established that it is not possible to have multiple validation groups on a validation control out-of-the-box, and custom code will be required to achieve the desired effect. I've updated the question accordingly, to assist anyone looking to achieve this in the future.
I'll be testing a solution that incorporates elements from both a.bajorinas' and Sunil's answers and will hopefully be able to provide an update afterward.
The only thing I can think of for your desired use case, is setting ValidationGroup for your validators in code behind on button click, keep in mind that this would first perform a postback and then validate the controls which might not be ok for you.
protected void btn_save_click(object sender, EventArgs e){
rfv_FirstName.ValidationGroup = "save";
Page.Validate("save");
if (Page.IsValid)
{
//logic if validators pass
}
}
This will let you reuse validators for multiple groups.
Also in my linked posted, one of the comments to the selected answer is "It is not possible to assign multiple groups to a validation control"
ASP.Net does not support multiple validation groups. However, with a little bit of JavaScript code you can make it support multiple validation groups.
What you need to do is override the standard JavaScript function of IsValidationGroupMatch. This function is part of the standard validation library in ASP.Net.
At bottom of your aspx page, just add the script tag given below. Note that this script is just before closing form, body and html tags, which is important when overriding a JavaScript function.
<script type="text/javascript">
window["IsValidationGroupMatch"] = function (control, validationGroup) {
if ((typeof (validationGroup) == "undefined") || (validationGroup == null)) {
return true;
}
var controlGroup = "";
var isGroupContained = false;
if (typeof (control.validationGroup) == "string") {
controlGroup = control.validationGroup;
var controlGroupArray = [];
if (validationGroup.indexOf(",") > -1) {
controlGroupArray = validationGroup.split(",");// validationGroup.split(",");
}
for (var i = 0; i < controlGroupArray.length; i++) {
if (controlGroupArray[i].trim() == controlGroup.trim()) {
isGroupContained = true;
}
}
}
return (controlGroup == validationGroup || isGroupContained);
}
</script>
</form>
</body>
</html>
Once you do this, then you can add a comma delimited list of validation groups for the button that validates multiple validation groups as in example below. In example below a button with id of btnMultipleValidationGroups is validating group1 as well as group2.
<div>
TextBox1 :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="TextBox1 needs input" ControlToValidate="TextBox1" ForeColor="Red" ValidationGroup="group1"></asp:RequiredFieldValidator>
<br />
<br />
TextBox2 :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="TextBox2 needs input" ControlToValidate="TextBox2" ForeColor="Red" ValidationGroup="group2"></asp:RequiredFieldValidator>
<br />
<br />
TextBox3 :
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="TextBox3 needs input" ControlToValidate="TextBox3" ForeColor="Red" ValidationGroup="group3"></asp:RequiredFieldValidator>
<br />
<br />
</div>
<asp:Button ID="btnMultipleValidationGroups" runat="server" Text="Validate group1 and group2" ValidationGroup="group1,group2" OnClick="btnMultipleValidationGroups_Click" />
<asp:Button ID="btnGroup1" runat="server" Text="Validate only group1" ValidationGroup="group1" OnClick="btnGroup1_Click" />
<asp:Button ID="btnGroup2" runat="server" Text="Validate only group2" ValidationGroup="group2" OnClick="btnGroup2_Click" />

PNM Sequence & asp.net: GridTemplateColumn should be mandatory

I use PNM Sequence. And I need to make one grid column as the mandatory field.
I know how to make it with any separate control. E.g. I can type:
<sq8:GridBoundColumn DataField="txtField" HeaderText="txtField"
SortExpression="txtField" UniqueName="txtField" FilterControlAltText="">
<ColumnValidationSettings>
<RequiredFieldValidator ForeColor=""></RequiredFieldValidator>
</ColumnValidationSettings>
</sq8:GridBoundColumn>
And I can use this Validator for the TextBox:
<sq8:Label runat="server" Text="Field:" ID="Label1" Width="100%"></sq8:Label>
<nobr>
<sq8:TextBox runat="server" ID="txtField" Width="100%"></sq8:TextBox>
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtField"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
</nobr>
<sq:BindableControl runat="server" TargetControlID="txtField"
DataField="txtField"></sq:BindableControl>
And it works. User can't send the form because he gets an error - the field is empty.
But I need to do the same with grid.
When I open "Edit columns" in Grid Wizard I can't see any property as "mandatory" or something like this.
And the code with RequiredFieldValidator doesn't work with a grid column. If I try to use it:
<Columns>
<sq8:GridBoundColumn DataField="txtFieldGrid" HeaderText="txtFieldGrid"
SortExpression="txtFieldGrid" UniqueName="txtFieldGrid"
FilterControlAltText="">
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtFieldGrid"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
<sq:BindableControl runat="server" TargetControlID="txtFieldGrid"
DataField="txtFieldGrid"></sq:BindableControl>
</sq8:GridBoundColumn>
</Columns>
In this case, I have an error:
Is there some method for grid column validation? Or it's impossible with a grid?
Maybe I can use some javascript?

ASP.NET Provide Styling for TextBox

I am using a multiline textbox as following:
<asp:TextBox runat="server" TextMode="MultiLine" Height="200" Width="500" ReadOnly="true" Font-Names="calibri" Text="Terms and Conditions Next text will go over.. >
What I need to do is to only make Terms and Conditions in bold. Rest of the sentence is not in bold. I tried using a span with a style of bold but that did not affect anything.
How do I do styling for a Textbox? Keep in mind this is a read only textbox.
If i understand you right, u need this
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/HTMLEditorExtender/HTMLEditorExtender.aspx
<script>
function onContentsChange() {
alert('contents changed');
}
</script>
<asp:TextBox runat="server"
ID="txtBox1"
TextMode="MultiLine"
Columns="50"
Rows="10"
Text="Hello <b>world!</b>" />
<ajaxToolkit:HtmlEditorExtender
ID="htmlEditorExtender1"
TargetControlID="txtBox1"
OnClientChange="onContentsChange"
runat="server" >
</ajaxToolkit:HtmlEditorExtender>
what you need is more than a regular textbox, you should either use devexpress or ajax toolikt, or some other ajax libraries.
you might consider looking for library that gives you rich formatting abilities

Getting text out of textarea from a ASP.NET GridView update

i'm loosing my mind. Using ASP.NET in a GridView, amongst other controls, I have the following:
<asp:TemplateField HeaderText="Intention">
<EditItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="true" TextMode="MultiLine" Wrap="true" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="false" TextMode="MultiLine" runat="server" />
</ItemTemplate>
</asp:TemplateField>
I would like to take the value out of this textarea and save in a database. However, server side, I try to pull the value out, like such:
string txt = (TextBox)DonationResultsTable.Rows[e.RowIndex].Cells[6].Controls[1].Text;
... but I keep getting the value that was SENT to the Client.
I wrote this javascript and I can see the values change in the DOM, but still the server keeps taking the old value.
$("textarea").change(function()
{
var txt = $(this).val();
$(this).html(txt).text(txt);
});
So my guess was ViewState, but I disabled it for those controls, like this:
<asp:TextBox ID="IntentionInfo" ViewStateMode="Disabled" Enabled="false" TextMode="MultiLine" runat="server" />
Still nothing! Any ideas?
One option could be to use a hidden field and update it on text changed for the text area. You could do this with jQuery like this:
$("textarea[id$=tbTest]").change(function () {
$("input[id$=hdnVal]").val($("textarea[id$=tbTest]").val());
});
Then on the server side, you can retrieve the hidden field's value and save it to your database.

Using FindControl: Accessing Controls in a Formview

I'm developing a simple Wedding List application, where guests can reserve the present they want to buy for the bride and groom. The Reserve page wraps a few fields inside a couple of panels, all wrapped inside a FormView.
The user enters their name, email and the quantity of items that they want to reserve, and the page will make the necessary reservations in the DB.
My first problem was that in FormView_ItemCommand, I couldn't reference any of the other controls in the FormView.... I figured this was a case for FindControl - but why do I need to for a Formview when I've never needed it for ListViews or DetailViews?
Secondly, I know the following code works..
Dim oCtrl as TextBox = Me.fvwReservation.FindControl("txtEmail")
Dim Test As String = oCtrl.Text
...but why can't I use...
Dim Test As String = Me.fvwReservation.FindControl("txtEmail").Text
??
Finally, I don't think I need it on this occasion, but I've been researching recursive FindControl variants, but I haven't actually found one that actually compiles! Any suggestions?
It's a lot for one post - thanks in advance.
Gratuitous Code Snippet:
<asp:FormView ID="fvwReservation" runat="Server" DataSourceID="dsGift">
<ItemTemplate>
<asp:Panel runat="server" ID="pnlDetails">
<h3>Reserve Item: <%#Eval("ShortDesc")%></h3>
<p>You have chosen to reserve the <em><%#Eval("LongDesc")%></em> gift.</p>
<p>Please enter your details below to confirm the reservation.</p>
</asp:Panel>
<asp:Panel runat="server" ID="pnlConfirm">
<div class="row">
<asp:Label runat="server" CssClass="label">Name:</asp:Label><asp:TextBox ID="txtName" MaxLength="50" runat="server" CssClass="Field" />
<asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="You must specify your Name" ControlToValidate="txtName" />
</div>
<div class="row">
<asp:Label runat="server" CssClass="label">Email:</asp:Label><asp:TextBox ID="txtEmail" MaxLength="100" runat="server" CssClass="Field"/>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="You must specify your Email Address" ControlToValidate="txtEmail" />
<asp:RegularExpressionValidator ID="regexEmail" ValidationExpression="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+#((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$" runat="server" ErrorMessage="Please enter a valid Email Address" ControlToValidate="txtEmail" />
</div>
<div class="row">
<asp:Label runat="server" CssClass="label">Quantity (max <%#Eval("QtyRemaining")%>):</asp:Label><asp:TextBox ID="iQty" MaxLength="2" runat="server" CssClass="Field" />
<asp:RangeValidator ID="rvQty" runat="server" ErrorMessage="The Quantity mmust be between 1 and 10" MinimumValue="1" MaximumValue="10" ControlToValidate="iQty" />
</div>
<div class="row">
<asp:Label runat="server" CssClass="label"> </asp:Label>
<asp:Button ID="btnReserve" Text="Confirm Reservation" CommandName="Reserve" runat="server" />
</div>
</asp:Panel>
</ItemTemplate>
</asp:FormView>
For your second question, FindControl returns a generic Control, and must be cast to the specific type of control in order to obtain access to the properties of that specific type of control.
You can do it in a one liner, like this:
Dim Test As String = CType(Me.fvwReservation.FindControl("txtEmail"), TextBox).Text
Regarding your first question, I would love to know the answer to that as well.
EDIT
Looked through a few other StackOverflow responses (specifically this one and this one). As the controls in the FormView template do not exist until the template is the active template, you cannot directly refer to them in the code behind. Thus, you must use FindControl during an appropriate event to access the controls.
Hmm, even in the FormView templates, I don't think that FindControl will work reliably, typically I would only use that with straight HTML rendered controls, not ASP.net generated ones.
I'm pretty sure that the templated controls should be available in the Server side code, (ie. txtEmail.text) if not, double check the template
A recursive FindControl is also pretty taxing on the Server and potentially unreliable.
You need to use recursive FindControl method in order to access the elements inside the FormView control. There are many implementations available and one of them is linked below:
http://www.highoncoding.com/Articles/606_Creating_a_BetterFindControl_and_MuchBetterFindControl.aspx

Resources