RadGrid Custom Filter - asp.net

I'm trying to add a custom filter to my RadGrid. I have a column, vendNum, which I want to allow users to filter on multiple vendNums with a comma-separated list. Basically, I want the same functionality as an "in" statement in SQL (where vendNum in (X,Y,Z)).
I followed the tutorial on this site and came up with the following code to place in my RadGrid1_ItemCommand event.
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.FilterCommandName)
{
Pair filterPair = (Pair)e.CommandArgument;
switch (filterPair.Second.ToString())
{
case "vendNum":
TextBox tbPattern = (e.Item as GridFilteringItem)["vendNum"].Controls[0] as TextBox;
if (tbPattern.Text.Contains(","))
{
string[] values = tbPattern.Text.Split(',');
if (values.Length >= 2)
{
e.Canceled = true;
StringBuilder newFilter = new StringBuilder();
for (int i = 0; i < values.Length; i++)
{
if (i == values.Length - 1)
newFilter.Append("[vendNum] = " + values[i]);
else
newFilter.Append("[vendNum] = " + values[i] + " OR ");
}
if (RadGrid1.MasterTableView.FilterExpression == "")
RadGrid1.MasterTableView.FilterExpression = newFilter.ToString();
else
RadGrid1.MasterTableView.FilterExpression = "((" + RadGrid1.MasterTableView.FilterExpression + ") AND (" + newFilter.ToString() + "))";
RadGrid1.Rebind();
}
}
break;
default:
break;
}
}
}
Doing this, though, keeps giving me an error "Expression Expected" when I try to filter with a comma separated list. I'm still able to filter a single vendNum. My FilterExpression does come out as expected. The code is failing on the RadGrid1.Rebind() statement. Has anyone dealt with this before? Any help is greatly appreciated.
Thanks,
Aaron
Forgot to Edit This
I solved this problem weeks ago...I had to set the "EnableLinqExpressions" property to false under the RadGrid.

I saw this as a fix for this question somewhere.
Try adding:
RadGrid1.EnableLinqExpressions = false;

Related

Loop program if user enters wrong input

I just started learning C# and while loops are confusing me. Unlike Java, where I can use a while loop to loop a program if a user entered a invalid input, it's not acting the same way in C#.
using System;
namespace first {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hi! What is your name");
string userName = Console.ReadLine();
Console.WriteLine("oh! you are:" + userName);
Console.WriteLine("let play a game");
string answer="Y";
while (answer == "Y") {
Random random = new Random();
int correntNumber = random.Next(1, 2);
int guess = 0;
Console.WriteLine("Guess a number");
while (guess != correntNumber) {
string userGuess = Console.ReadLine();
//validate input method 1
try {
guess = int.Parse(userGuess);
} catch (Exception e) {
Console.WriteLine("Invalid inout", e);
}
//validate input method 2
//if(!int.TryParse(userGuess, out guess)) {
// Console.WriteLine("invalid input");
//}
if (guess != correntNumber) {
Console.WriteLine("try again!");
}
}
Console.WriteLine("Yes! corrector");
Console.WriteLine("Play again?");
//string answer;
answer = Console.ReadLine().ToUpper();
if(answer == "Y") {
continue;
} else if (answer == "N") {
Console.WriteLine("bye");
return;
} else if (answer != "Y" || answer != "N") {
Console.WriteLine("y or n");
answer = Console.ReadLine().ToUpper();
continue;
}
}
}
}
}
When I enter a value other than y or n, the message appears,Console.WriteLine("Y or n only");, but the game restarts while it shouldn't.
I am sorry this is a simple and rather silly question, but I can't pin point where I am going wrong.
the problem is that after printing to the user "y or n only" message you take the input but you don't actually do anything with it
so the loop just restarts regardless of the input , to fix this issue you could replace the last if part with this code
while(answer != 'Y' && answer != 'N'){
Console.WriteLine("y or n only");
answer = Convert.ToChar(Console.ReadLine().ToUpper());
}
if(answer == 'Y')
{
continue;
}
else if(answer == 'N')
{
Console.WriteLine("goodbye");
return;
}
so after you read the first input answer of him for repeating or no you check if it's a valid input or not and if it's not you keep asking him for "y or n only" till he enters "Y" or "N" and then you process this answer for whether it's a "Y" or "N" in the if part

Trying to understand sorting in the Table widget for a Page

I am trying to understand how sorting works in Table widgets works when loading a page. For most of my pages using the Table widget, the page loads sorted by the first column.
I do see the below code in the onAttach event for the Table panel in a page. I am wondering if this is the code that sets the sorting when a page loads.
// GENERATED CODE: modify at your own risk
window._am = window._am || {};
if (!window._am.TableState) {
window._am.TableState = {};
window._am.inFlight = false;
}
if (!window._am.sortTableBy) {
window._am.sortTableBy = function(datasource, field, fieldHeader, tableState) {
if (!field) {
throw "Can't sort the table because specified field was not found.";
}
tableState.inFlight = true;
if (tableState.sortByField === field.name) {
tableState.ascending = !tableState.ascending;
} else {
if (tableState.fieldHeader) {
tableState.fieldHeader.text = tableState.fieldHeaderText;
tableState.fieldHeader.ariaLabel = "";
}
tableState.sortByField = field.name;
tableState.ascending = true;
tableState.fieldHeader = fieldHeader;
tableState.fieldHeaderText = fieldHeader.text;
}
datasource.query.clearSorting();
var sortDirection = tableState.ascending ? "ascending" : "descending";
datasource.query.sorting[field.name]["_" + sortDirection]();
datasource.query.pageIndex = 1;
datasource.load(function() {
tableState.inFlight = false;
fieldHeader.text = fieldHeader.text.replace(/ (\u25B2|\u25BC)/g, "");
if (tableState.sortByField === field.name) {
fieldHeader.ariaLabel = fieldHeader.text + " sort " + sortDirection;
fieldHeader.text = fieldHeader.text + (tableState.ascending ? " \u25B2" : " \u25BC");
app.accessibility.announce(fieldHeader.ariaLabel);
}
});
};
}
Sorting is set on your datasource.
Click the model (top left) that your table is using.
Click Datasources and expand your Datasource (there will only be one, unless you have created additional ones).
Once you choose the field you want the table to use for sorting, you can choose "Ascending" or "Descending".
The problem is with adding new records from a separate create form. The new records always appear at the end of the list , until a subsequent 'load' is performed.

Switching two grids in one page on button click

Im new in asp.net.. i been working on this since yesterday and theres no luck to figure it out.. why the other grid wont appear on the page since i already put a flag on its code behind.
hope someone can help me on this. thanks.
protected void Edit_btn_Click(object sender, EventArgs e)
{
if (FLAG_hiddenfield.Value == "T")
{
BIRPER_EDIT_grid.Visible = true;
BIRPER_grid.Visible = false;
Edit_btn.Text = "View";
BIRPER_grid.DataBind();
BIRPER_EDIT_grid.DataBind();
FLAG_hiddenfield.Value = "F";
}
if (FLAG_hiddenfield.Value == "F")
{
BIRPER_grid.Visible = true;
BIRPER_EDIT_grid.Visible = false;
Edit_btn.Text = "Edit";
BIRPER_grid.DataBind();
BIRPER_EDIT_grid.DataBind();
FLAG_hiddenfield.Value = "T";
}
}
It seems your conditional logic is wrong here, replace the two ifs with if else:
if(FLAG_hiddenfield.Value == "T"){
...
}
else if (FLAG_hiddenfield.Value == "F"){
...
}
Note that if GridView contains no data then it is not shown on webpage.

Show static non-clickable heading in AutoCompleteExtender list

I have an AutoCompleteExtender from the Ajax Control Toolkit. I need to have a heading in the dropdown list that shows how many items found, but it should not be selectable as an item.
I have tried this using jQuery, but even when I just add as a div, it is still selected as an item into the text box when I click on it:
function clientPopulated(sender, e) {
var completionList = $find("AutoCompleteEx").get_completionList();
var completionListNodes = completionList.childNodes;
for (i = 0; i < completionListNodes.length; i++) {
completionListNodes[i].title = completionListNodes[i]._value.split(':')[2];
}
var resultsHeader;
if(completionListNodes.length==1000)
resultsHeader = 'Max count of 1000 reached.<br/>Please refine your search.';
else if(completionListNodes.length>0)
resultsHeader = completionListNodes.length + ' hits.';
else
resultsHeader = msg_NoObjectsFound ;
jQuery(completionListNodes[0]).before('<div>' + resultsHeader + '</div>');
}
Add OnClientItemSelected and OnClientShowing events handlers and try script below:
function itemSelected(sender, args) {
if (args.get_value() == null) {
sender._element.value = "";
}
}
function clientShowing() {
var extender = $find("AutoCompleteEx");
var optionsCount = extender.get_completionSetCount();
var message = "";
if (optionsCount == 1000) {
message = 'Max count of 1000 reached.<br/>Please refine your search.';
}
else if (optionsCount > 0) {
message = optionsCount + " hits."
}
else {
message = "oops."
}
jQuery(extender.get_completionList()).prepend("<li style='background-color:#ccc !important;'>" + message + "</li>");
}
Added:
you even can do this without OnClientItemSelected handler:
function clientShowing() {
var extender = $find("AutoCompleteEx");
var oldSetText = extender._setText;
extender._setText = function (item) {
if (item.rel == "header") {
extender._element.value = "";
return;
}
oldSetText.call(extender, item);
};
var optionsCount = extender.get_completionSetCount();
var message = "";
if (optionsCount == 1000) {
message = 'Max count of 1000 reached.<br/>Please refine your search.';
}
else if (optionsCount > 0) {
message = optionsCount + " hits."
}
else {
message = "oops."
}
jQuery(extender.get_completionList()).prepend("<li rel='header' style='background-color:#ccc !important;'>" + message + "</li>");
}
We can give a better answer if you post the output html of your autocomplete control. Anyway if its a dropdown control;
jQuery(completionListNodes[0]).before('
<option value="-99" disabled="disabled">your message here</option>'
);
The answer by Yuriy helped me in solving it so I give him credit although his sollution needed some changes to work.
First of all, the clientShowing event (mapped by setting OnClientShowing = "clientShowing" in the AutoExtender control) is executed on initialization. Here we override the _setText method to make sure nothing happens when clicking on the header element. I have used the overriding idea from Yuriy's answer that really did the trick for me. I only changed to check on css class instead of a ref attribute value.
function clientShowing(sender, e) {
var extender = sender;
var oldSetText = extender._setText;
extender._setText = function (item) {
if (jQuery(item).hasClass('listHeader')) {
// Do nothing. The original version sets the item text to the search
// textbox here, but I just want to keep the current search text.
return;
}
// Call the original version of the _setText method
oldSetText.call(extender, item);
};
}
So then we need to add the header element to the top of the list. This has to be done in the clientPopulated event (mapped by setting OnClientPopulated = "clientPopulated" in the AutoExtender control). This event is executed each time the search results have been finished populated, so here we have the correct search count available.
function clientPopulated(sender, e) {
var extender = sender;
var completionList = extender.get_completionList();
var completionListCount = completionList.childNodes.length;
var maxCount = extender.get_completionSetCount();
var resultsHeader;
if(completionListCount == maxCount)
resultsHeader = 'Max count of ' + maxCount + ' reached.<br/>'
+ 'Please refine your search.';
else if(completionListCount > 0)
resultsHeader = completionListCount + ' hits.';
else
resultsHeader = 'No objects found';
jQuery(completionList).prepend(
'<li class="listHeader">' + resultsHeader + '</li>');
}
I have also created a new css class to display this properly. I have used !important to make sure this overrides the mousover style added from the AutoExtender control.
.listHeader
{
background-color : #fafffa !important;
color : #061069 !important;
cursor : default !important;
}

How to disable/enable the allowinsert in telerik radscheduler

I have develop a Telerik Radscheduler in that i have write the code like below in that i want disable/Enable the perticuler time-slot for an event,in this disable is working fine but enable not working i can't understand why it is not enable to allow-insert.please help me where i need to change to resolve this problem...
protected void RadScheduler1_TimeSlotCreated1(object sender, TimeSlotCreatedEventArgs e)
{
//Getting Business hour time
mybusinesscalendarEntities objEntity = new mybusinesscalendarEntities();
var Result = from bhours in objEntity.businesshours where bhours.BusinessId == businessid select bhours;
if (Result.Count() > 0)
{
var Hours = (from bhours in objEntity.businesshours where bhours.BusinessId == businessid select bhours).First();
//Get particular day businee hour timings and disable the time slot
string Day = System.DateTime.Today.DayOfWeek.ToString();
if (Day == "Monday")
{
string WorkDay = Hours.MondayFromTime.Value.ToShortTimeString();
string WorkDayStart = WorkDay.Remove(WorkDay.Length - 2, 2);
string WorkDayEnd = Hours.MondayToTime.Value.ToShortTimeString();
string WorkDayEndTime = WorkDayEnd.Remove(WorkDayEnd.Length - 2, 2);
if ((e.TimeSlot.Start.TimeOfDay < TimeSpan.Parse(WorkDayStart.Trim())) || (e.TimeSlot.Start.TimeOfDay > TimeSpan.Parse(WorkDayEndTime.Trim())))
{
e.TimeSlot.CssClass = "Disabled";
RadScheduler1.ReadOnly = true;
}
else
{
RadScheduler1.ReadOnly = false;
RadScheduler1.AllowInsert = true;
RadScheduler1.AllowEdit = true;
RadScheduler1.AllowDelete = true;
}
If when your last timeslot is created (last time event is fired) this part evaluates to true.
if ((e.TimeSlot.Start.TimeOfDay < TimeSpan.Parse(WorkDayStart.Trim())) || (e.TimeSlot.Start.TimeOfDay > TimeSpan.Parse(WorkDayEndTime.Trim())))
{
e.TimeSlot.CssClass = "Disabled";
RadScheduler1.ReadOnly = true;
}
Then your entire scheduler will be in read only mode. This means no editing inserting deleting moving etc.
Your intention seems to be to disabled specific timeslots. I dont think you intend to set these properties in this particular event.
RadScheduler1.ReadOnly = false;
RadScheduler1.AllowInsert = true;
RadScheduler1.AllowEdit = true;
RadScheduler1.AllowDelete = true;
Test by commenting out the lines that set the readonly, allowinster, allowedit, allowdelete properties since they are not per timeslot, rather for the entire scheduler

Resources