On my page I have FormView control, and I am binding Integer database field to the TextBox that's residing inside a FormView's EditItemTemplate.
<FormView ...>
<EditItemTemplate>
<dx:ASPxTextBox ID="txtDiameter" runat="server" Text='<%# Bind("Diameter") %>' />
...
<EditItemTemplate>
...
</FormView>
My problem is that when Diameter field is null, the txtDiameter gets value of empty string. When I click Update command (if I didn't provide any numerical value in txtDiameter), a client error is raised
Sys.WebForms.PageRequestManagerServerErrorException: is not a valid value for Int32.
I found some post from 2005 that claims that this is happening due to some bug. Well, now is 2012. The only way I figured how to deal with this is by using FormView_ItemUpdating event, to circle through all problematic values and convert them from String.Empty to null.
I am just little suspicious that it's maybe not necessary. Is there another way to deal with this problem?
Bind the editor's Value property instead:
<dx:ASPxTextBox ID="txtDiameter" runat="server" Value='<%# Bind("Diameter") %>' />
Does this work?
Related
I need to dynamically change the 'number format' of a textbox that is inside an itemTemplate. Nothing I've tried is working and I would appreciate some ideas.
A very simplified version of the original code is this:
<asp:GridView ID="gv1">
<Columns>
<asp:TemplateField HeaderText="Xfer From">
<ItemTemplate>
<asp:TextBox ID="txtTransferFrom" runat="server" Text='<%# Bind ("FundValueTransferFrom", "{0:0.00}") %>' />
<asp:RangeValidator ID="val1" runat="server" ControlToValidate="txtTransferFrom" Type="Currency" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Please notice the decimal formatting ("{0:0.00}") and the Currency type of the bound field.
Now what I need to do is, based on the setting of a radio button on this page, change that formatting. In one state, it needs to be currency with two decimal places; in the other it must be a percentage out to three decimal places.
It sounds straightforward but I can't make it work. Ideally I would like to put some code in the 'radioButton_SelectedIndexChanged' method but I can't seem to access that formatting from there.
I also tried using an "if" statement in inline code (<% %>) on the ASPX page, but it doesn't respond to the changing of the radio buttons. I must mention that this grid is inside an update panel... I tried calling UP.Update() but it doesn't fix the problem. I did remove the initial formatting from the ASPX page so that it won't interfere -- nothing.
One solution might have been to format the number strings in the code-behind, but I can't -- the grid is bound to an object whose fields are "doubles" not strings... the conversion to strings is happening implicitly in the data binding.
Doesn't anyone have another approach I might try to switch this formatting programmatically? Thanks in advance for any insight!
Dacker, your comment put me on a good path towards an answer--many thanks! I still have other issues to deal with, but the code below appears to be doing the trick for this immediate issue.
In the ASPX (abbreviated tag):
<asp:TextBox ID="txtTransferFrom" ... " Text='<%# Eval("FundValueTransferFrom") != null ? GetFormattedString((double)Eval("FundValueTransferFrom")) : Eval("FundValueTransferFrom") %>' ...">
And in the code-behind:
public string GetFormattedString(double val)
{
string newVal;
if (rblFtmp1.Text == DOLLAR)
{
newVal = String.Format("{0:0.00}", val);
}
else // If not DOLLAR then it's PERCENT
{
newVal = String.Format("{0:0.000}", val);
}
return newVal;
}
I have a DataGridView with ASP.net hyperlink fields inside of it. What I'm trying to do is not display a certain hyperlink based on a condition. I have SQL that determines if the hyperlink should be hidden or not, but I'm having trouble getting this to work in the hyperlinks.
I tried <asp:HyperLinkField....Visible="<%= Eval(Condition) %>" /> where Condition is True or False from my SQL query.
Which of course throws the error Cannot create an object of type 'System.Boolean' from its string representation '<%= Eval(Condition)%>' for the 'Visible' property.
So I understand this from Why will <%= %> expressions as property values on a server-controls lead to a compile errors? and other similar questions.
My question now is: what is the workaround? How can I get the hyperlink to display or not based on my condition?
You cannot change HyperLinkField's Visible on runtime, because it does not have a DataBinding event.
Instead, you should not be changing HyperLinkField's Visible value. The problem is the rest of the column will not be aligned properly if you hide a single cell.
Instead, you want to use TemplateField and HyperLink, and hide a link only (leave the table cell by itself). For example,
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="ConditionHyperLink" runat="server"
Visible='<%# Convert.ToBoolean(Eval("Condition")) %>'
Text="Link" />
</ItemTemplate>
</asp:TemplateField>
FYI: Your syntax is not correct; it should be ='<%# Eval("") %>'
My recommendation would be to handle it in the codebehind, most likely handling the RowCreated event, and setting the Visible property of the control there. There might be more context to your application that I'm missing, but that seems like the simplest way.
This is the event: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated(v=vs.110).aspx
Other events on the gridview in case that one doesn't meet your needs: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events%28v=vs.110%29.aspx
I am using a dropdown within an EditTemplate as such:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server" DataSourceID="BusinessTypeSource" DataTextField="Value" DataValueField="Value" AppendDataBoundItems="true" Text='<%# Bind("BusinessType") %>'>
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
The DataSource has the following value:
Personal
Professional
The problem that I am running into is that the field that I am binding has a blank value.
As a blank value is not in the DataSource I get the following error message:
'ddlBusinessType' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Not sure how to fix this. When I do my binding, if the value is not in the datasource, I like to default it to 'Please Select'
Depends on the circumstances, but one option I'll use often times is as follows:
1) In the lookup table, I'll have an entry titled 'Not Selected',
with a primary key value of 0 (or whatever).
2) In the transaction table, update all of the null values for that field to 0 (or whatever the value is for your 'Not Selected' option). Also, set the default value of that field to 0.
This is nice for a couple of reasons -- one, no special mark-up required (so you know it will always be consistent) -- and two, wherever else you use this data, you'll have access to the 'not selected' output.
It should be like:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server"
DataSourceID="BusinessTypeSource"
DataTextField="BusinessType" DataValueField="Value" AppendDataBoundItems="true" >
<asp:ListItem Value="-1">Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
1) No need to set Text as you already bounded the text field.
2) You need to set a value for default item you have added in markup.
You are trying to select an item before binding process. Capture Binding event and set de selected value on this place.
There isn't the attribute Value :
<asp:CheckBox ID="CheckBox1" runat="server" />
while on standard HTML this is allowed :
<input type="checkbox" ID="CheckBox1" value="My Valyue" />
why?
The Text property is used to render a label for the checkbox.
The control has an InputAttributes property that you can add to:
myChk.InputAttributes.Add("value", "My Value");
I believe that if you simply add the value attribute to the markup, this will also get populated.
You can access the value like so:
myChk.InputAttributes["value"];
To answer the question of why Value is not a build in attribute to the CheckBox control:
A CheckBox in isolation (just by itself) needs no value. By definition it is a boolean and is identified by its ID. All you need to do is check whether it was checked or not.
The value comes into play when you group checkboxes and there is a control for that - the CheckBoxList that uses ListItem - each ListItem does have a Value property.
Instead of using the asp:CheckBox control, use the html input checkbox, and run it at the server.
<input type="checkbox" id="ck" runat="server" value='<%# Eval("Value") %>' />
<asp:Label ID="lbl" runat="server" AssociatedControlID="ck" Text='<%# Eval("Name") %>'></asp:Label>
Now you can reference it from codebehind as an HtmlInputCheckBox (my latest example is inside a repeater, so I can decorate this substitute for a checkbox list with other elements, like a tool tip image).
foreach (RepeaterItem repeaterItem in repCheckboxes.Items)
{
HtmlInputCheckBox listItem = (HtmlInputCheckBox)repeaterItem.FindControl("ck");
if (listItem.Checked)
{
string val = listItem.Value;
...
I know this does not answer the "why" of the OP, but this comes up high in searches for this exact problem, and this is a good solution. As for why, I think MS goofed by leaving it out, since you don't have control over the html in a CheckBoxList
(first of all, it seems this is a subject that is discussed many times before, but I can't find a proper answer for my case)
I have a asp.net FormView with a DropDownList for the selection of a month. The FormView is data bound to an ObjectDataSource.
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Bind("OrderDate.Month") %>' Width="100" runat="server" />
I like to bind the selected value to the nested property 'Month' of 'OrderDate' as shown above. The property OrderDate is of type DateTime. The error I'm getting while binding to a nested property is:
A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.
What is the best solution to be able to bind to a nested propety?
Thanks in advance.
Are you retrieving data from DataSourceID="MonthsListDataSource" and tryingo to bind it to another DataBase field (SelectedValue='<%# Eval("OrderDate.Month") %>') ?
In my application a do this like this:
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("MonthsListDataSource.Month") %>' Width="100" runat="server" />
And when Updating a retrieve de DropDownList with find control, get its selected value, associate it to other table (OrderDate.Month) and save it.
Sorry my answer, but i dont know if a undertand it.
What about using Eval keyword
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("OrderDate.Month") %>' Width="100" runat="server" />