I want to keep TextBox attribute Read Only in web form. But when I keep this Read Only , On PostBackits values get vanishes and I get empty text. How can I achieve the same functionality without loosing TextBox values.
You can achieve this by preventing user to enter values in the textbox, thus add following attributes to the textbox. It will maintain values even on postback also.
onkeypress="return false;"
Change it by removing the ReadOnly=”true” from the tag , we will add it in the code.
Now in the code add the following :
TextBox1.Attributes.Add(“readonly”, “readonly”);
You probably have it like this right now:
<asp:TextBox ID="MyTextBox" runat="server" Enabled="false" />
Just change this to:
<asp:TextBox ID="MyTextBox" runat="server" ReadOnly="true" />
And it will send its value over postback like all other form elements while still being read only.
Related
I am having trouble with the <asp:RequiredFieldValidator> in this code.
<asp:Label ID="email_Label" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="email_Text" runat="server" MaxLength="40" Width="250"></asp:TextBox> *
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="email_Text" Text="Required"></asp:RequiredFieldValidator>
According to the W3schools documentation here I have used the <asp:RequiredFieldValidator> correctly, however instead of displaying the label and text box the page also displays the error message. This happens on page_load so the value has not had a chance to change from the default yet. I want the error text to display after the user clicks the Save button at the bottom of the form I am building.
What is Being Displayed:
Email[TextBox] * Required
What Should Be displayed:
Email[TextBox] *
Am I missing a parent element for the validator or something. According to the example on w3schools site no parent element should be needed. In fact the way they have their example set-up is exactly what I was expecting for this.
Use Validation Group if want to display Error message on Button click. Like this.
<asp:RequiredFieldValidator ID="rqtxtQName" ValidationGroup="save" ControlToValidate="txtQueueName" runat="server" ErrorMessage="Some required field are missing." SetFocusOnError="True"Display="Dynamic"></asp:RequiredFieldValidator>
and use validation group on Button also.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="save" OnClick="btnSubmit_Click"/>
Hope it will helps.
you should use ErrorMessage instead of Text
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="email_Text" ErrorMessage="Required"></asp:RequiredFieldValidator>
If you want to validation on save button click event then set property ValidationGroup="Group1"
for RequiredFieldValidator and also for save button. So it will check validation when you click save button.
And For display message you can use ErrorMessage property.
Thanks,
Hitesh
Set the RequiredFieldValidator this way. Remove the property: 'TextRequired' & put a * inbetween the opening and closing tag.
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="email_Text">*</asp:RequiredFieldValidator>
Also, Do not set the ErrorMessage property as this will again not fit your requirements.
I have run into the same problem as described here.
Only the question is marked as answered with only an explanation as to why you may have to click twice when using a RequiredFieldValidator on input fields - once as the blur of a textbox(for example) will correct the validation and then again to actually post the form.
I don't want to have to click a button twice! Does anyone know a solution or workaround to this?
You could add EnableClientScript=false to the validator.
That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.
Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.
Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.
The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.
I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.
If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.
Here's a way to reserve about one, vertical line of space for a dynamic validation message:
<div style="height:1.5em;overflow:visible;">
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="Name is required" ControlToValidate="TextBoxName"
Display="Dynamic"></asp:RequiredFieldValidator>
</div>
I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.
Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.
I would suggest adding
causesValidation="true"
to your button to see if that works.
I have a better idea.
Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.
Try this:
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
Here is code that is working fine for me and helping to get rid of double click.
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"
Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});
Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:
the first click will make Page_BlockSubmit become false.
the second click will check the value of Page_BlockSubmit and return true.
if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.
And, you can use google chrome to trace the value of Page_BlockSubmit.
I have a grid view in ASP.NET, one of the column of the grid is time(hh:mm). I have used validators for the field. They look like this
<FooterTemplate>
<asp:TextBox ID="st_timef" runat="server"/>
<asp:RegularExpressionValidator ID="stRegularExpressionValidator" runat="server" ErrorMessage="HH:MM!"
ValidationExpression="/(?:[01]?\d|2[0-3]):(?:[0-5]\d)$/" ControlToValidate="st_timef" />
<asp:RequiredFieldValidator ID="stValidator" runat="server" ControlToValidate="st_timef" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
The validators work,but I have a problem with the regex validator,
Although it shows a error on wrong input format,but it still allows me to press the button which updates the data,(which is not the case of normal validator).
I need the validator to stop running my update function ones it shows a error in validation.
You must have to set the common validation group name of ValidationGroup property for TextBox, Button and RegularExpression validator.
how to make the error disappear once the correct format is inserted?
Set error description to the Text property of validation control (Set empty string to ErrorMessage property).
I am having issues with this GridView. I update it in design mode, and the update does not make it to the code behind section. For example, I add field "xyz". Gridview says "xyz" fields exist in design mode. In code-behind, it does not exist. when you view the page in browser, ofcoure "xyz" field is not shown. After refresh, even the gridview looses this field in design mode. So ok I got around this problem and managed to add a template field which is now working.
Now the problem is, the checkbox that I added in one of the column is not persistent. I have a button which works on the selected values of checkboxes but each time I click the button, the page refereshes and all the checked values are lost (checked values becomes unchecked).
Does anyone has any idea?
Want to mention, I am working with a bit messy code. But dont want to change a lot at this time.
<asp:TemplateField HeaderText="All" >
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" name="chkAll" />
</HeaderTemplate>
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
This took me a couple of days to figure out myself. Since my code was quite messy, it was hard to troubleshoot.
I put in some code in page_load default event that finally fixed. Don't know if I was putting code in the wrong place first. It definitely took some time though.
I have a database with a row that has "TEXTTEXT" inside a "noteText" column and null in that column for a different row. If I run a query that generates 1 rows, then run a different query that has 2 rows, Eval("noteText") returns different information when placed in a TextBox vs. placed in a Label. The TextBox and Label were created specifically for this test; it is not referenced in the codebehind. I think the TextBox for row 1 of the second query is somewhow pulling the data used in row1 of the first query.
I don't think it could be a browser-related issue since the source code of the returned page is itself incorrect.
Aspx file code:
<asp:TemplateField HeaderText ="Notes" SortExpression="noteid">
<ItemTemplate>
STARTXX
<asp:TextBox ID="AtxtNote" ViewStateMode="Disabled" runat="server"
TextMode= "SingleLine" Width="260" Text='<%# Eval("noteText") %>' />
<br />
<asp:Label ID="BtxtNote" runat="server" Width="260"
Text='<%# Eval("noteText") %>' />
<br />
ENDXX
</ItemTemplate>
</asp:TemplateField>
Source code on page:
STARTXX
<input name="ctl00$CPH1$gv$ctl02$AtxtNote" type="text"
value="TEXTTEXT" id="ctl00_ContentPlaceHolder1_gv_ctl02_AtxtNote"
style="width:260px;" />
<br />
<span id="ctl00_CPH1_gv_ctl02_BtxtNote"
style="display:inline-block;width:260px;"></span><br />
ENDXX
Actual spacing is a bit different; I edited the text of the source and output code to prevent horizontal scrolling.
I expected both Eval calls to show an empty string.
We fixed it by adding more calls to DataBind.
The current theory is that the TextBox was being submitted when clicking the button to query the database. After the DataBind happened, Asp.Net repopulated the TextBox with the data that was submitted. The TextBox now logically corresponded to a different row in the database, but was in the same position as the TextBox from the previous page view and thus had the same ClientID.
One reason for this suspicion is that the posts were triggering OnTextChanged events.
I'm still unhappy with this; I don't have full confidence that I know what's going on, even though it is now working.