i have one textbox and it is not readonly..
but by default it has to get filled by 0..
then depending on user requirement, textbox value may be altered...
Does anyone know how to do this?
A question like this was already solved. I personally would do it in the markup: Text="0". Linked the other question below.
Default value of textbox
"Placeholder" property will do the job:
Try the following: This works well with all latest browsers
<asp:TextBox runat="server" ID="TextBox1" placeholder="0">
</asp:TextBox>
Related
I have a relatively simple problem I have not been able to find the solution to.
I have 3 text boxes for user input quantities, 3 corresponding text boxes that display the total, which is quantity * price, and one final texbox that displays the total of the 3 calculated costs.
I have this working, but along the way I was unable to assign a default value to the textboxes. Here is my code for one of the 3 textboxes:
<td>
<asp:TextBox ID="tbQuantity3" runat="server" CssClass="textboxGeneral"
AutoPostBack="True" OnTextChanged="tbQuantity3_TextChanged" Text="1">
</asp:TextBox>
<ajax:FilteredTextBoxExtender
ID="ftbeQuantity3"
TargetControlID="tbQuantity3"
runat="server"
FilterType="Numbers, Custom"
ValidChars=".">
</ajax:FilteredTextBoxExtender>
</td>
Originally, I was trying to set default values of 0 so that the textboxes would not be null and the total of the 3 costs could be calculated if the user had not entered 3 quantities. I realized a better solution was to check for null values in the code behind, but I would still also like to be able to set default values of the textboxes and am not sure why the Text="1" property of the asp textbox is not working.
There was a "ClearActiviityFields()" function running at different points that was setting the textbox back to blank. The Text=1 was in fact working, it was just being undone before I got to that page.
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.
Okay I researched this as much as possible before I posted so I hope this is not a repost but here goes...
I made a datatable, lets say it has a coulmn called "cartype" , then I added a column called "color"
well, I bound a gridview what I want to do is use a label, and have its FORECOLOR be the VALUE in the column "color".
I tried this:
<asp:BoundField DataField="cartype" HeaderText="Cars" ItemStyle-Width="130" ItemStyle-ForeColor='<% Eval("Color") %>' />
but I got an error about
"Cannot create an object of type System.drawing.color from its string representation '<%Eval("color")%>' for the 'ForeColor' Property.
I also tried adding a template field and got the same result.
I was trying not to use the rowdatabound event and use .Cells[3] , because then when I add columns, it's going to change the cell number, and switch up everything! I was hoping I could make it cleaner by binding the color with the data.
Okay I ended up doing this. sad :(
At the VERY END of my grid I added an ASP:Hidden template field
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hColor" runat="server" Value='<%# Eval("Color") %>'/>
</ItemTemplate>
</asp:TemplateField>
Then in the RowDataBoundEvent I did this
Dim hid As HiddenField
hid = e.Row.FindControl("hColor")
If (hid.Value <> Nothing) Then
e.Row.Cells(1).ForeColor = System.Drawing.ColorTranslator.FromHtml("#" + hid.Value)
End If
PS. I heart C#. Dont like VB.NET. lol :)
Have you tried:
ForeColor='<%# System.Drawing.Color.FromName(Eval("Color")) %>'
?
The reason for the error is that the runtime expects the value of ForeColor to be a color, NOT a string. The runtime cannot create "Red" from the string "Red".
You can create a function at code behind using following example to work around your issue .. function will accept string color value and will return System color. Also before populating gridview you can store the color column in the arraylist and use that arraylist to return system color. Not the best way but cant think of anything else.
ForeColor='<%# Convert.ToString(Eval("Color")) == "Blue" ? System.Drawing.Blue>
I am working with datagridview control. I want to validate dataGridView to allow the value in money format in datagridview textbox cell like 123.00,123.50.
That should take numeric values and one . value.
The way that I have used in the past (and the only way that I know how to do it) is to create classes that inherit from DataGridViewTextBoxColumn and DataGridViewTextBoxCell, and use the derived class instead of DataGridViewTextBoxColumn as the ColumnType when creating the grid.
This link has an example of how to do it in VB. I can't post my C# code as it was developed by a company I used to work for, but the VB code is basically the same.
You need
1. There should be a decimal.
2. Only 2 digits after decimal
3. And there should be 2 such numbers separated by a comma(,)
Here is the code:-
aspx file
<asp:TextBox ID="txtMoney" runat="server" Width="100px" />
<asp:RequiredFieldValidator ID="rfvMoney" ValidationGroup="Insert" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Please Enter value for money" ToolTip="Please Enter valid value for money"
SetFocusOnError="true" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="reMoney" runat="server" ControlToValidate="txtMoney"
ErrorMessage="Please Enter a Valid value for Money" ToolTip="Please Enter a Valid Value for money"
SetFocusOnError="true" ForeColor="Red" ValidationExpression="\d+([.])\d\d([,])\d+([.])\d\d"
ValidationGroup="Insert">*</asp:RegularExpressionValidator>
Code Behind i.e. aspx.cs file
if (e.CommandName.Equals("Insert"))
{
eInfo.money = ((TextBox)gvEG.FindControl("txtMoney")).Text;
new mainSQL().insertEmployeeInfo(eInfo); //My cs file for insert values to table
FillEmployeeGrid();
}
I have tried this code in my project and it is working properly.
The main code here is the Regular Expression:
ValidationExpression="\d+([.])\d\d([,])\d+([.])\d\d"
where
\d- denotes 1 digit
\d+ - one or many digits
\d\d - denotes only 2 digits
Since it seems like you want to validate user input in real time, I think the best and the effective way to go about it is creating a custom column type hosting a MaskedtextBox. This article shows how to do it. And you can specify a format there. See this MSDN post to know about Mask Elements.
First of all, I've got a DataTable which stores data in a number of fields. One of these fields stores a value, which can either be '1', '0' or '-1'.
What I'm trying to accomplish is to fill a GridView with all the data contained in this DataTable, except for the previously mentioned field. Instead of this field, I would like to display a column which shows a different icon depending on the value (either 0, 1 or -1).
In other words, if a row contains a value of '0' in this field within the DataTable, I would like a red circle icon to appear in the equivalent cell within the GridView.
Could anyone kindly explain how this may be accomplished? Below is some code which may help to explain the situation further.
The GridView is filled with the data contained in the DataTable.
source = new DataSource();
TableGridView.DataSource = source.FillTable();
TableGridView.DataBind();
What should be done after this?
EDIT: Solution added as a comment below
Well there are different approaches:
1) you could add a field to your DataTable for the image and fill it with the url of the image that corresponds to the value. Add an image to the GridView and set it's ImageUrl to that field.
2) Add a template field to your GridView and add the 3 images. In the Visible property of the each image you set something like <% (int)Eval("MyNumericValue") == x %> (x being 0, 1 or -1) . That way only 1 image is shown, the others hidden (not rendered even).
from the hip:
<TemplateField>
<ItemTemplate>
<asp:image runat="server" imageurl="~/images/0.png" Visible='<%# (int)Eval("MyNumericValue") == 0 %>' />
<asp:image runat="server" imageurl="~/images/1.png" Visible='<%# (int)Eval("MyNumericValue") == 1 %>' />
<asp:image runat="server" imageurl="~/images/-1.png" Visible='<%# (int)Eval("MyNumericValue") == -1 %>' />
</ItemTemplate>
</TemplateField>
Solved: I created the TemplateField in ASP.NET code, and the other fields programmatically in the .cs class. Next, I added a chunk of code which switches round the order of the columns. Hope someone finds this helpful.