I read all entries from an entity Framework/DB for a web page (ASP.NET) and in a repeater on my page i set the value of a TextBox like that:
<td>
<owiw:CustomTextBox runat="server" ReadOnly="true" ID="txt_FieldType" Text='<%# DataBinder.Eval(Container.DataItem, "iFieldType") %>' />
</td>
To iFieldType exists an enumeration which lies in a static class.
Can i parse the int value using Enum.Parse?
Must be something like that, isn't it?
Text='<%# Enum.Parse(typeof(OneWhoIsWhoModel.Database.DatabaseHelper.CustomFieldTypes), (Container.DataItem, "iFieldType")) %>'
Got it!
I have forgotten to get the value with DataBinder.Eval and then it must be converted to the type string.
The solution is always simple, just the way to it is troublesome.
<owiw:CustomTextBox runat="server" ReadOnly="true" ID="txt_FieldType"
Text='<%# Enum.Parse(typeof(OneWhoIsWhoModel.Database.DatabaseHelper.CustomFieldTypes),
Convert.ToString(DataBinder.Eval(Container.DataItem, "iFieldType"))) %>' />
Related
I have a date TextBox that is bound to a DateTime property:
<asp:TemplateField HeaderText="Date1">
<ItemTemplate>
<asp:TextBox ID="Date1TextBox" Text='<%# Bind("Date1", "{0: yyyy-MM-dd}")%>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
What is the easiest way to make the format dynamic, meaning that I want to be able to specify which format I want to use dependent on the current ui culture / language. Is it possible to do this in markup (I want to avoid to do this in Code if possible)?
I tried the following but Bind doesn't seem to support methods to get the format string:
<asp:TextBox ID="Date1TextBox" Text='<%# Bind("Date1", GetCurrentFormat())%>' runat="server"/>
try this:
<asp:TextBox ID="Date1TextBox" Text='<%# Eval("Date1","{0:yyyy-MM-dd}") %>'
runat="server"/>
EDIT
protected string GetCurrentFormat(string myDate)
{
//Retrive Current Format from DB
string MyFormat="yyyy-MM-dd";
return Convert.ToDateTime(myDate).ToString(MyFormat);
}
<asp:TextBox ID="Date1TextBox"
Text='<%# GetCurrentFormat(Convert.ToString(Eval("Date1"))) %>'
runat="server"/>
I have a basic ASP.NET Web Forms application.
I give the user the possibility to create the records by using a Web Form and to update them by using a DetailsView.
The web form stores the empty field on the TextBoxes as *empty strin*g in the DB correctly.
The DetailsView instead stores them as null fields in the DB.
Moreover in the DetailsView_ItemUpdated event if I check the e.newvalues and e.oldvalues arguments the corresponding fields are null as well (throwing an exception) that means the data are already sent as null to the server upon submit.
In the DetailsView I use templates and I set all Update parameters in the SQLDataSource as ConvertEmptyStringToNull="false".
SQLDataSource parameter:
<asp:Parameter Name="SHELF" Type="String" ConvertEmptyStringToNull="false"/>
DetailsView
<asp:DetailsView runat="server"
ID="GvProductDetail" AutoGenerateRows="False" DataKeyNames="rowid"
DataSourceID="ProductDetailData"
OnItemUpdated="gvProductDetail_ItemUpdated">
<asp:TemplateField>
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate>
<%# Eval("Shelf") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Is that possible that Bind converts the empty values to null? I thought it was enough to set the UpdateParameters rule not to convert empty string to null but apprently there is some other surprise. anybody might help?
I solved it by placing ConvertEmptyStringToNull="false" to every <asp:TemplateField> containing <asp:TextBox>:
<asp:TemplateField ConvertEmptyStringToNull="false">
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate> <%# Eval("Shelf") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
In Dynamics ax Enterprise Portal I have created a templatefield in AxGridView. Seems everything ok, but when i try to enter some value to this textbox (manually or through lookup), it doesnt bind to ReqPo!ItemId field. Checked that with info(strfmt("%1", ReqPo.ItemId))); in validateWrite method on ReqPo dataset - it prints nothing; What i'm missing?
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="<%$ AxLabel:#SYS12836 %>" Visible="true">
<EditItemTemplate>
<asp:TextBox runat="server" ID="TextBoxFilterItemId" CssClass="AxInputField"
Columns="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.DisplayLength %>"
Enabled="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.AllowEdit %>"
MaxLength="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.StringSize %>"
Text='<%# Bind("[ReqPo!ItemId]") %>'>
</asp:TextBox>
<dynamics:AxLookup
ID="AxLookup3" runat="server" OnLookup="Item_lookup" TargetControlId="TextBoxFilterItemId"
CssClass="AxLookupButtonBF" HoverCssClass="AxLookupButtonHoverBF" ShowFilter="True">
</dynamics:AxLookup>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemIdLabel" runat="server" Text='<%# Bind("[ReqPo!ItemId]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I assume you are using the ItemId field from the table ReqPo. If you locate that field in the Data Dictionary you will discover that the field does not allow editing.
As AxGridView only supports editing, which leads me to the conclusion that you are trying the break the data integrity rules of the application.
if this is the case, it is obviously not possible.
Figure there is a simple solution to this problem but I have been unable to find it.
I have databinding in an ASP.Net application to a GridView. This gridview is bound to an ObjectDataSource as per standard usage.
The problem I have is that one of my bound fields uses the property DataFormatString="{0:C}" and due to the currency format being displayed when an update is attempted and the object recreated I get a error as such "$13.00 is not a valid value for Decimal."
Clearly this is a result of the column using a FormatString and then attempting to bind it back to a decimal property I have in my object called UnitPrice.
I am assuming there is some markup I can set that can specify how the value is translated back?
Thanks in advance for any help.
For anyone curious the solution ended up looking like this...
<asp:TemplateField>
<HeaderTemplate>
UnitPrice
</HeaderTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditItem" runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' Enabled="false" ></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="lblUnitPrice"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Do not include the format string in the EditItemTemplate. Just bind the raw value.
Something like this:
<asp:TemplateField SortExpression="UnitPrice" HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="editUnitPrice" Runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
I have an asp page that renders controls based on a Request parameter. Simplified example:
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } else { %>
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% } %>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="TextBoxName" ErrorMessage="Please enter an name." />
However, the compiler and runtime are complaining that the TextBoxName is not a unque ID. But surely it is, if the output is conditional and exclusive?
I could rename them TextBoxNameA (if block) and TextBoxNameB (else block), but the odd thing is that the validator still seems to fire on TextBoxNameB, even if the validator is inside the else block and the code runs through the first block at runtime
How can I set the page to render one or the other boxes, without conflicting IDs and conflicting Validators?
Thanks for any help
Ryan
How about moving around your logic?
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } %>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="TextBoxName" ErrorMessage="Please enter an name." />
But server side, how is the parser supposed to distinguish the two?
And while parsing it will not be able to determine that one should be displayed and one shouldn't.
You need to give each server side control a different ID, regardless of it being displayed to the client.
You can restructure you code like this, as TextBoxName will always display (and with the same text):
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } %>
And no more problem.
Since the Name textbox will show up regardless of the condition, you can just take that out of your if block all together and then just conditionally show the SurName textbox and that should solve your problem.