Say I have a label inside a gridview template column and I assign the text to the databound item, "OrderID". Is there any way I can call .ToString().PadLeft(7, '0') ? Here's the label as it stands:
<asp:Label ID="lblOrderID" runat="server" Text='<%# "WW" + DataBinder.Eval(Container.DataItem, "OrderID") %>' />
Because PadLeft requires a char, I can't use my single quotes because I've already surrounded the value in single quotes and can't do double quotes because of the DataBinder.Eval expression. Is there another way I can get this to work on one line?
You can do it inline (I'll leave it to someone else to give you the answer as I don't have VS open to test it).
But I would do it by adding a method in the codebehind to return a formatted order id. Or even better, put the format method in a static helper class so it's available to any page that needs a formatted order id.
E.g. if you're binding to a collection of Orders, something like:
public static string GetFormattedOrderId(object dataItem)
{
Order order = (Order) dataItem;
return String.Format("WW{0:N7}", order.OrderId); \
// or return order.OrderId.ToString().PadLeft... if you prefer
}
or if you're binding to a DataTable, something like:
public static string GetFormattedOrderId(object dataItem)
{
DataRow row = (DataRow) dataItem;
return String.Format("WW{0:N7}", row["OrderId"]);
}
then you can have a consistenly formatted order id anywhere in your markup:
'<%# MyFormattingHelper.GetFormattedOrderId(Container.DataItem) %>'
Related
I have a RadTextBox control in my form, and in one of the methods, it sets the text of the control as such:
SecondHalfTB.EmptyMessage = sharedMailbox.MailboxEmail.Replace("CAAS_", string.Empty)
.Replace("#caas.gov.sg", string.Empty);
<td class="ms-formbody">
CAAS_<telerik:RadTextBox ID="SecondHalfTB" runat="server" MaxLength="255">
</telerik:RadTextBox>
#caas.gov.sg
<div>
<asp:Label ID="lbSecondHalfTB" runat="server" CssClass="WarningMessage"></asp:Label>
</div>
</td>
If I did not enter any values in the textbox, will the following statement return an empty string?
string newEmail = SecondHalfTB.Text;
if (newEmail == string.Empty)
{
newEmail = SecondHalfTB.DisplayText;
}
CAAS_<telerik:RadTextBox ID="SecondHalfTB" runat="server" MaxLength="255">
</telerik:RadTextBox>
#caas.gov.sg
The values are actually hardcoded in your markup, aren't they? All you need is just remove them from the HTML.
By definition, the EmptyMessage property lets you specify the appearance of the input control when the user has not entered a value.
Whereas the DisplayText property allows you to set the display value from the Server to a different value the actual value. Similar to the empty message, but shown even if the input is not empty. This text will be cleared once the user changes the input value.
For you updated question - newEmail will be the same value as the DisplayText if the actual value of the textbox is empty.
Here is what I can see when I do e.Item.FindControl for the TextBox I'm trying to get the value for
I have 2 date inputs in a list view and I am trying to retrieve their values but always get an empty string.
If I set the value in the front end then it comes through to the code behind but if I edit the field the original value is returned.
I am able to get the values from and controls without an issue.
I've tried using asp:TextBox and html
<asp:TextBox ID="editArrivalFrom" name="editArrivalFrom" TextMode="Date" runat="server" OnTextChanged="editArrivalFrom_TextChanged"></asp:TextBox>
<input type="date" id="editArrivalTo" name="editArrivalTo" runat="server" />
var arrivalFrom = (TextBox) e.Item.FindControl("editArrivalFrom");
if (!String.IsNullOrWhiteSpace(arrivalFrom.Text))
var arrivalTo = (HtmlInputGenericControl) e.Item.FindControl("editArrivalTo");
if (!String.IsNullOrWhiteSpace(arrivalTo.Value))
I've also tried using Request.Form, all of the below return null:
var test = Request.Form[arrivalFrom.UniqueID];
var test2 = Request.Form[arrivalFrom.ClientID];
var test3 = Request.Form["editArrivalFrom"];
There is also no sign of either of the inputs in Request.Form.AllKeys
The asp control doesn't show up in the designer.cs file and adding it only results in it being null anyway.
Any suggestions would be much appreciated I've been pulling my hair out for hours.
Relevant code for the form update is below. Please note that all of this is contained within an update panel
<asp:ListView ID="MarkupRuleList" runat="server"
OnItemCommand="MarkupRuleList_OnItemCommand">
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" CssClass="lstview_record_bttn button-spacing-7" Text="Update" CommandName="Update" CommandArgument='<%# Eval("Id") %>' runat="server" />
</EditItemTemplate
protected void MarkupRuleList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
int markupId = 0;
int.TryParse(e.CommandArgument.ToString(), out markupId);
var markup = new MarkupRuleDTO();
switch (e.CommandName)
{
case "Update":
// GetMarkupRuleFromPage contains all of the code from the top of this post
markup = GetMarkupRuleFromPage(e, markupId);
break;
}
}
Edit: Solved by ditching the date input and using regular textbox, then performing the following onkeyup JS function
function dateInputHandler(item, event) {
// key code 8 == backspace
if (event.keyCode != 8) {
// insert forward slashes after days and months
if (item.value.length == 2 || item.value.length == 5) {
item.value = item.value + '/'
}
// remove double slashes and prevent length exceeding 10 characters
// format should be dd/mm/yyyy
if (item.value.endsWith("//") || item.value.length > 10) {
item.value = item.value.slice(0, -1);
}
}
}
You may be better off casting the sender object rather than the EventArgs object.
var x = ((TextBox)sender).Text;
But you can access the control directly (or should be able to).
var x = editArrivalFrom.Text;
And to get from the form, use the correct variable name:
var x = Request.Form[editArrivalFrom.ClientID];
How can I get the markup for something I created like this
Div MyDiv = new Div("Test");
I essentially want to get a string with the contents
string SomeString = "<div>Test</div>";
Or maybe even
string SomeString = "<div id="MyDiv">Test</div>;
Is there any way I can do that?
One option is to give the div an id and a runat="server" attribute. If\when the page is posted back to the codebehind, use the InnerHTML property. Example:
<div id="myDiv" runat="server" />
And in your codebehind
string contents = myDiv.InnerHTML;
If you only need this data client side, use javascript, like mentioned in the comments above. You can use the following code:
var x = document.getElementById('myDiv').innerHTML;
If you already have jQuery, then:
var x = $('myDiv').html();
I have listview and String array datasource. How can I bind them using eval ?
<telerik:RadListView ID="lvDevSampTableSelection" runat="server" AllowMultiItemSelection="true">
<ItemTemplate>
<p><%# Eval("??") %></p>
</ItemTemplate>
</telerik:RadListView>
here is code behind
ResultDto<String[]> result = client.GetTableNames("IMAPPUSER");
var source = new BindingSource();
source.DataSource = result.Data;
lvDevSampTableSelection.DataSource = source;
lvDevSampTableSelection.DataBind();
I use <%# Container.DataItem %> instead of eval and get data from string to listview
You can't, it needs to evaluate field names from the object that is the datasource (be it columns in datatable or fields in some List). Thus, I think your best option is to create a custom class with a couple of fields, create a List<> from that class and bind to that list.
I have an asp.net chckebox list:
<asp:EntityDataSource ID="GradeLevelEntityDataSource" runat="server"
ConnectionString="name=NewCourseRequestDataEntities"
DefaultContainerName="NewCourseRequestDataEntities" EnableFlattening="False"
EntitySetName="grade_levels" OrderBy="it.grade_level_index">
</asp:EntityDataSource>
<asp:CheckBoxList ID="GradeLevelCheckBoxList" runat="server" cssClass="horizontalcontrols"
DataSourceID="GradeLevelEntityDataSource"
DataTextField="grade_level_description" DataValueField="grade_level_id" AutoPostBack="True"
OnSelectedIndexChanged="CollegeInstitutionsListboxChange"
RepeatDirection="Horizontal" RepeatLayout="Flow">
</asp:CheckBoxList>
A user can return to the page, it gets the record ID, pulls the record data, and should check the appropriate checkboxes:
CheckBoxList grades = (CheckBoxList)FindControl("GradeLevelCheckBoxList");
foreach (request_grade_college r in CurrentRequest.request_grade_college)
{
ListItem grade = grades.Items.FindByValue(r.grade_level_id.ToString());
if (grade != null)
{
grade.Selected = true;
}
}
the portion of the code r.grade_level_id.ToString() does return the correct GUID, as type String. However, ListItem grade remain null, so none of the GradeLevelCheckboxList get checked.
What am I missing please?
I think that the data is not yet bound when you try to access it using Items.FindByValue(), try calling
grades.DataBind();
before the foreach loop.
Please make sure the listbox item's Value property has the correct value, if Item's Text and Value are different.
Also, Value Property and grade_level_id are exactly of same case and with trimmed spaces.