RadTextBox EmptyMessage and .Replace Equals Empty or With a Value? - c#

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.

Related

Unable to retrieve date input value C# Web Forms

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];

Checking to see if DropDownList selected index has a value

In my C# web application, I have three textboxes, three drop down lists, and one button.
The button is suppose to run a SQL string that will take the values of whatever is in typed in the textboxes, whatever is selected, and be inserted into the database. The values inserted may not be null so I don't want any blank entries. By default, I have the DropDownList like so in the source:
<asp:DropDownList ID="ReadDrop" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:DropDownList>
So there is a blank entry (default), and then the yes/no. There are three of these drop down lists in the application. In my C# code, I have the following to prevent the button from firing if there is a blank entry:
if (UsernameBox.Text != "" & FirstNameBox.Text != "" & LastNameBox.Text != "" /* check for blank dropdownlist? */)
My current issue is that I don't know how to check the dropdownlist for a blank entry. My guess would have been to check to see if ReadDrop.Text is blank, but I am relatively inexperienced in ASP.NET and am wondering if there is a "proper" way to do this.
Thanks!
You could use SelectedIndex > 0:
if (UsernameBox.Text != "" & FirstNameBox.Text != "" & LastNameBox.Text != "" && ReadDrop.SelectedIndex > 0)
{
// ...
}
Note that the SelectedIndex is -1 if no item is selected and yes/no have 1/2.
Another option is to use the SelectedItem:
ListItem selectedItem = ReadDrop.SelectedItem;
if(selectedItem != null && !String.IsNullOrEmpty(selectedItem.Text))
{
// ...
}
However, i think that you actually want to prevent that the user selects no item, or in other words, validate that he selects something. Then use a RequiredFieldValidator, you have to set the InitialValue to "":
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Select Something" ControlToValidate="ReadDrop"
InitialValue=""></asp:RequiredFieldValidator>
You could assign a different Value to your empty item and change the InitialValue appropriately:

populate CheckBoxList from query using FindByValue

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.

Assigning Combobox SelectedItem.Text a String

I am receiving the following exception when trying to assign the value from 'model' into a Combobox named ModelNumTB.
For some reason I cannot get it to copy over the value of the string into the default loading value of the Combobox. How are you supposed to do this?
string model;
<ajaxToolkit:ComboBox ID="ModelNumTB" runat="server"
AutoCompleteMode="Suggest" AutoPostBack="True" DataSourceID="ComboboxDS"
DataTextField="MODEL_NUMBER" DataValueField="MODEL_NUMBER" MaxLength="0"
style="display: inline;"
onselectedindexchanged="ModelNumTB_SelectedIndexChanged"></ajaxToolkit:ComboBox>
Changing it to the following doesn't work either:
Change
ModelNumTB.SelectedItem.Text = model;
to:
ModelNumTB.Text = model;
The SelectedItem property is null

Can you call PadLeft inside a databound server control?

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) %>'

Categories