TextChanged event not firing - c#

I have a GridView and a TextBox in one of its fields:
<asp:GridView ID="NTSBulkEditGridView" runat="server" AutoGenerateColumns="false" AllowSorting="true" Height="500px"
DataKeyNames="BookStem" OnRowDataBound="NTSBulkEditGridView_RowDataBound" DataSourceID="NTSSqlDataSource">
<Columns>
<asp:TemplateField HeaderText="Priority" SortExpression="Priority">
<ItemTemplate>
<asp:TextBox ID="txtPriority" runat="server" Text='<%# Eval("Priority") %>' BorderStyle="None" Width="80%" OnTextChanged="TextBox_Changed" AutoPostBack="true"></asp:TextBox>
<asp:CompareValidator ID="PriorityCompareValidator" runat="server" ControlToValidate="txtPriority" Display="Dynamic" ErrorMessage="Priority must be an integer!" Text="*" Operator="DataTypeCheck" Type="Integer" ValidationGroup="InsertUpdateNewTitlesStatusValidation" ></asp:CompareValidator>
</ItemTemplate>
</asp:TemplateField>
...
Could you please tell me why TextBox_Changed() is never called when I change text and press Enter? I tried to put same kind of a TextBox outside of the GridView, and there it works.
Thanks.

TextChanged of an ASP.NET TextBox translates into blur JavaScript event. And blur occurs when the text of the input element is changed and the input element loses focus. Try to change the text, but don't hit Enter. Simply hit TAB key to go to next field. Does it fire post back?
Update:
Well, I saw that link. Two things. First, if you notice, it goes back to 2005 and ASP.NET 2.0 and we all know that 6 years in the world of computer means 60 years. So that article is obsolete by now. Second, sometimes a pattern only exists in articles, and you rarely see it in real productive systems. How many bulk actions have you seen on the web? And what type do they have? Consider Gmail, or Yahoo mail for example. You can mark 20 or 30 or X number of emails as read in one shot. But, can you respond to 20 mails in one step? Nope, just because it doesn't make sense. I've never seen a bulk action on a text box in web world. I think you'd better stick to use AJAX, with better performance, and more user acceptance. :)

Related

error in the gridview control

I have a Gridview and the columns defined like below.
When I run the program I get the error
Literal content is not allowed within a System.Web.UI.WebControls.DataControlFieldCollection
<Columns>
<asp:CommandField ButtonType="Image"
ControlStyle-Height="20"
ControlStyle-Width="30"
SelectImageUrl="tar.png"
SelectText="Select"
ShowSelectButton="true"/>
<asp:TemplateField HeaderText="Target Date">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("tar_date") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl1" runat="server"
Text='<%# Bind("tar_date") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
Can anyone help me solving this?
Nothing seems to be wrong with your markup.
The only thing I would recommend is ending the Label control immediately and trying it again.
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("tar_date") %>' />
// OR
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("tar_date") %>'></asp:Label>
In the past I have seen issues when Tab, or some unintentional characters come in between some of the templated controls. Check if you have any such characters by redoing every line from scratch.
This question is a little old, but for the others who encounter this problem:
This problem can caused by not putting white space between properties. For example:
<asp:TextBox ID="TextBox1" runat="server"Text='<%# Bind("tar_date") %>'> </asp:TextBox> //wrong (no space before Text)
This was such a frustrating error to run into. I wasted about 4 hours on this and there are surprisingly few resources I could find on Google to help me troubleshoot it. I was updating a legacy application, so the intricacies of a GridView were a bit hazy since I haven't created one from scratch in a while.
At the end of it, the fix was a result of a suggestion by Raja to rewrite the control. Visual Studio wasn't highlighting a very important issue and the ambiguous nature of the error message had me looking at the wrong grid columns. Despite the error pointing to an issue with the TemplateField, the issue for me was actually in a BoundField.
During a conversion from Telerik RadGrid to GridView, the BoundField control had an orphaned <ItemStyle> tag nested inside of it, but the BoundField control doesn't allow this.
Visually, you wouldn't know or even suspect this, unless you have recent familiarity with GridView. You couldn't run into it by debugging. Visual Studio and the compiler were not reporting this either. So troubleshooting it was a beast.
The thing that worked was rewriting the grid, line-by-line. Thanks, Raja!
The autocomplete feature in Visual Studio wouldn't let me close the BoundField control to add any other tag/control of any kind. This is when I finally realized where the issue was.
I hope this helps another unlucky Googler. :)

RadSpell display redlines

I have a multiline checkbox. I want to use RadSpell.
But everytime I insert text on the textbox it doesn't display red lines.
How can i do this?
here is my code.
<telerik:RadTextBox ID="textbox1" runat="server" TextMode="MultiLine" Rows="4"
Columns="51" Width="401px" Font-Size="10" Font-Names="Calibri" />
<telerik:radspell id="RadSpell1" runat="server" DictionaryLanguage="English"
controltocheck="textbox1" spellcheckprovider="PhoneticProvider"
supportedlanguages="en-US,English" ButtonType="None"/>
NOTE: No buttons needed.
I just want to show all the words that are misspelled.
That's a browser feature - inline spellchecking. All modern browsers have it. Well, IE doesn't, but why should we call it modern? ;-)
What RadSpell can do for you is a popup with suggestions and information.
EDIT: Forgot to mention you can trigger RadSpell's check programmatically: http://www.telerik.com/help/aspnet-ajax/spell-client-side-api.html. Look for the startSpellCheck() method. For example in the onblur event.

use Javascript or C# to validate a webform

Would like to validate more than one control on one button click. I would like something to validate whether a textbox has contents if a checkbox is checked or not but the checkbox doesn't necessarily have to be checked and in that case I don't want to check the textbox. I tried validation group but each button needs to control the different groups and i need this all to be under one button.
I'm open to ideas of how to do this c#,javascript...etc. Heres some code: Button3 is the save which validates whether checkbox 1 is checked and if so textbox10 cant be empty. I have about four other instances of this but are independent of each other.
<asp:Button ID="Button3" runat="server" Height="24px"
Text="Save" Visible="False" Width="67px" Font-Bold="True"
causesvalidation="true"
validationgroup="required"
runat="Server" />
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text=" Breach Letter Sent"
ValidationGroup="required" AutoPostBack="True" Enabled="False" />
You want to use the CustomValidator control which can validate both on the server and the client. There is an example in the docs here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
I would never do form validation in JavaScript. Believe it or not, but some people actually turn off JavaScript! Use validators to validate the field content. Of course this means a round trip to the server in most cases, but you get reliable and well integrated validation.
you can use validation with Ajax (in Ajax postback occures but you will not sense)

Tie a custom savebutton to itemtemplate textbox

using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
using Northwind;
N00b here,
Trying to customize a asp.net gridview, and I'm stuck at this part...
In the ItemTemplate, I've got a text-box that has a class applied to it where on .keypress(Clientside JS), a asp:LinkButton appears next to it that's formatted like a Jquery UI icon. I'm stuck at this part. I know that I can "enable editing" for the gridview, but I want to make an edit button only appear on .keypress, which I’ve already achieved. The park I’m stuck at is trying to get the click of that asp:LinkButton to send an update command to the SQLserver to update that db entry accordingly . So far, I’ve got the textbox to be created dynamically for each db item in that column.
Is there a more practical way of doing this while achieving the same results? To be more specific, I'm not really looking for code, cause I'm trying to learn, but more for methods I should take a look at, or other tutorials that I missed on the web.
I would also appreciate some advice, thanks in advance: )
You can try using GridView_RowCommand event to fetch the Command event bubbled by Linkbutton.
You can use CommandName Property of the LinkButton and set it to Update.
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:linkbutton ID="Button1" text="TextBox1" runat="server" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>

Efficient/Easy method to validate for empty text boxes?

I have a about 6 or 7 text boxes which needs to be validated to make sure they are not empty from server side code. So how I usually do this is, check each textbox 1 by 1 to ensure that they are not empty. Are there any other efficient methods to do this? I have searched on SO and have found out that adding all the textboxes to a list and using a for each is a much better method. Are there any other ways that this can be achieved? Thanx a lot in advance :)
Just check them each individually:
if (string.IsNullOrEmpty(this.NameTextBox.Text) ||
string.IsNullOrEmpty(this.AddressLine1TextBox.Text) ||
// etc...
)
{
// Handle me
}
Or possibly:
void CheckTextBox(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
if (string.IsNullOrEmpty(textBox.Text))
{
// Handle me
}
}
void Validate()
{
CheckTextBox(this.FirstNameTextBox);
CheckTextBox(this.AddressLine1TextBox);
CheckTextBox(this.AddressLine2TextBox);
}
7 text boxes really isn't that many - explicitly checking each one keeps it simple and makes sure that others reading your code know what's going on, whereas messing around with collections is adding another layer of indirection, and makes it just slightly less straightforward to debug.
Keep it simple!
I agree with Kragen - your code may look "big" because of all the checks, but you are really writing exactly what the program needs to do in order to validate these things, so any sort of clever approach that reduces the number of lines of code you write isn't actually going to speed things up that much.
Question though: do you have to validate the textbox on the server? If you are only validating that the textbox isn't empty, I'd suggest using client side validation. That will save you server time and bandwidth, since your user won't be allowed to submit the form to your server until their browser has validated that they aren't empty.
You'd still want to validate on the server side (in case they don't have JavaScript enabled on their browser or they are attempting some kind of malicious behaviour).
The native ASP.NET way of client side validation involves adding an ASP.NET validation tag to your ASPX. It's actually quite easy. Here's an example on MSDN:
http://msdn.microsoft.com/en-us/library/aa479013.aspx#aspnet-validateaspnetservercontrols_topic3
I've simplified their code a bit to match your requirements:
<form runat="server">
<asp:TextBox id="TextBox1" runat="server" />
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Name is required!" ControlToValidate="TextBox1" />
<asp:TextBox id="TextBox2" runat="server" />
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="Address is required!" ControlToValidate="TextBox2" />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
</form>

Categories