ASP Listview - checkbox event findcontrol - c#

I'm trying to build a Listview EDIT/Insert template where I can use a checkbox to enable updating multiple database tables but to little success.. I managed to get the insert working by performing some foul sorcery on the Listview inserting event. But I'd prefer that it works with the Checkbox OnCheckedChanged event as it feels abit more kosher in my mind, and of course the added benefit on it working for the edittemplate..
protected void checktest_clicked(object sender, EventArgs e)
{
//testlabel.Text = testcheck.Checked.ToString(); <-- exists outside of LW
// so it works
//Label hejha = (Label)lwRapport.FindControl("testlabel");
CheckBox trial = (CheckBox)lwRapport.FindControl("upParameter");
if(trial != null)
{
if(trial.Checked == true)
{ testlabel.Text = "finally"; }
if(trial.Checked == false)
{ testlabel.Text = "Nope, not going to happen"; }
}
if (trial == null)
{ testlabel.Text = "not wanted"; }
}
That's my test snippet for checking how the FindControl works and so far I've been quite unsuccessful making it do what I want it to do..
Any Correction on faults / hack / workaround for this matter would be apritiated
EDIT1*
The checkbox is inside of the listview, more precisely in the inserttemplate. The template looks something on the lines like this:
textbox <bind"table1.element">
textbox2 <bind"table1.element2">
checkbox [_]
textbox3 <bind"table2.element">
Observe that the snippet above is just a pseudocode snippet of my layout not the acctual layout. What I'm attempting is to find the checkbox and bind it's checked value to a parameter which passes a couple of checks in the SPROC then executes the UPDATE command

You seem to be not able to find the check box control from the list view. This is because you are searching for the check box inside the list view, and what you should be doing is searching for it inside the selected item.

You can have a look at this. Although it's GridView, I think it will works too.

Related

Cannot get combobox to return selected item

Dotnet, C#, VS2013
I have a method that retrieves the selected item in a combobox (CBSpecies). The method is called when a button is clicked. My problem is that no matter what I select, the item found by the method is always the first one (default set when I populate the combobox). I have a console application with exactly the same method and it works fine.
private void GetSelectedSpecies()
{
//EurostatSpeciesName = "Fish and Chips";
//EurostatSpeciesName = CBSpecies.SelectedIndex.ToString();
//return;
// CBSpecies.SelectedIndex = 3;
String MySpecies = CBSpecies.SelectedItem.ToString();
for (int i = 0; i < MaxSpecies; i++)
if (SpeciesArray[i].SpeciesName == MySpecies)
{
EurostatSpecies = SpeciesArray[i].SpeciesCode;
EurostatSpeciesName = SpeciesArray[i].SpeciesName;
break;
}
}
Added the following note: I think the problem is that I populate and initialize the combobox in the Page_Load method, so when the button does a postback, it resets everything since it reloads the page. This would not happen in the console version. I tried putting the whole setup (populating the species array, then populating the combobox from that, then setting the first combobox item as the default) by using: if (!Page.IsPostBack) as a condition, but then the app throws a null exception when the button is clicked.
Try .SelectedNode and see if that works.
Solved using a suggestion from this post:
https://www.telerik.com/forums/selectedvalue-lost-on-postback-in-dynamically-added-user-controls
The method referred in the question was moved into:
protected void Page_Init(object sender, EventArgs e)
{}
Page_Init is not created automatically, so must be manually added. Once the combobox checking method is put there, the selection is persistent.
Your combobox is a so-called dynamic control. I.e. you're building it in the code-behind instead of in markup.
Dynamic controls must be initialized prior to the viewstate in order to plug them into the ASP.NET Page Lifecycle events.
As you've found out, Page_Init is the correct place to initialize such a control. You can find more info on the subject here

UWP CustomRenderer for Checkbox: Pointer over Checkbox changes style?

I'm working with Xamarin.Forms and I made a CustomRenderer for Checkbox in UWP. When I set all the Checkboxes of my items in the ListView to true by clicking the button "Alle", the Checkboxes are displayed correctly with the check inside the box:
However, if I hover my mouse over the Checkboxes, they immediately change their appearence (the check disappears but it's still selected). In the following screenshot, I moved my cursor over the 3rd - 7th Checkboxes:
This is my overridden OnElementChanged method in the CustomRenderer:
protected override void OnElementChanged(ElementChangedEventArgs<EvaCheckbox> e)
{
base.OnElementChanged(e);
var model = e.NewElement;
if (model == null)
{
return;
}
nativeCheckbox = new CheckBox();
CheckboxPropertyChanged(model, null);
model.PropertyChanged += OnElementPropertyChanged;
nativeCheckbox.Checked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
nativeCheckbox.Unchecked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
SetNativeControl(nativeCheckbox);
}
I tried to override the PointerEntered event of nativeCheckbox. It works, for example if I set the model.IsChecked to true on this event, it will be set to true:
nativeCheckbox.PointerEntered += (s, args) =>
{
model.IsChecked = true;
};
But I don't know how to (if even at this place) prevent the checkbox from changing it's appearance when moving the cursor above the Checkbox. Just leaving the triggered event with empty code like this won't change anything about the described behaviour:
nativeCheckbox.PointerEntered += (s, args) => { };
How can I prevent the Checkbox from changing it's appearance when I move my cursor over it?
Update:
I've created a sample project for this issue. You can find the repository here: https://github.com/Zure1/CustomCheckbox
It has the exact same described behavior. In the following screenshot I pressed the button "All" on the bottom of the screen and then the checkboxes look like correct with a check inside of them:
After moving the mouse cursor over the bottom 3 checkboxes, their change their appearance:
Information: I'm debugging on my desktop (Windows 10). I don't know if this issue exists on WinPhone. Just in case you're wondering why my checkboxes are red: My system color in Windows is red.
This is a tricky one as I have been struggling with this issue for a while, I'll try my best to answer this.
TL;DR: It's caused by ViewCell.
The issue comes down to Xamarin Forms ListView and ViewCell.
I haven't been able to track down the cause yet for many months and the way I get around this issue is by refreshing the ListView every time a change happens forcing a redraw of the entire ListView which can really impact performance.
My educated guess on what the cause could be is the rendering code for the ViewCell is missing something.
As for your particular issue, I have created a CheckBoxCell which you can use to display a list of checkboxes with a title. I forked your project and made the changes.
This will display something similar to what you are trying to achieve and doesn't have rendering issues so will be a good starting point. You are able to customize this to display images and the like but you'll have to do that in the platform-specific layout code.
Please note that I have only created the code for UWP and that should be enough to get you going for the other platforms.
I hope this helps somewhat.

DevExpress DXGrid column header double click event

I have to check / uncheck all the checkboxes (toggle) in a column when the user double clicks the column header.
How can I implement this behaviour in the DevExpress DxGrid control?
I have searched the DevExpress support forum but I haven't found a solution.
Also, i am working on MVVM Pattern.
This case works for WinForms, not tested in WPF yet, I posted might it direct you to some lights:
There is a workaround to accomplish this behave, you have to implement yourGrid_DoubleClick Event Handler, then calculate the hit Info of the mouse click, the hit info object will tell you if the double click was on a column, something like:
private void yourGridViewName_DoubleClick(object sender, EventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView sndr =
sender as DevExpress.XtraGrid.Views.Grid.GridView;
DevExpress.Utils.DXMouseEventArgs dxMouseEventArgs =
e as DevExpress.Utils.DXMouseEventArgs;
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo =
sndr.CalcHitInfo(dxMouseEventArgs.Location);
if (hitInfo.InColumn)
{
string x = hitInfo.Column.Name;
//Rest of your logic goes here after getting the column name,
//You might now loop over your grid's data and do your logic
}
}
but you have to notice that this action will not prevent the sorting that column's header do, you might need to disable sorting for this grid
Hope this helped.

CheckedListBox - How to programmatically ensure that one and only one item can be checked at any given time?

I want to use a CheckedListBox in an application where each item in the ListBox is the name of a folder on my hard drive and for the purpose of reading and writing text files to and from each of these folders I want to ensure that one and only one item (a folder) can be selected at any one time in the CheckedListBox
How can I achieve this via code in C#?
Thanks for reading :-)
Edit \ Update - 22/10/2010
Thanks to all who took the time to reply - especially Adrift whose updated code as requested is working perfectly.
I do appreciate what some commentators said about my usage of a checkedlistbox in this manner, however I feel it suits my purposes perfectly in that I want there to be no doubt whatsoever as to where the text files will be read from and written to.
All the best.
I agree with the comments that radio buttons would be the usual UI element when only a single item is 'checked', but if you want to stick with a CheckedListBox for your UI, you can try something like this:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
if (checkedIndices.Count > 0 && checkedIndices[0] != e.Index)
{
checkedListBox1.SetItemChecked(checkedIndices[0], false);
}
}
You also might want to set CheckOnClick to true for the CheckedListBox.
Edit
Updated the code per your comment to deselect an item if it is unchecked. The problem is that unchecking the previously checked item causes the event to fire again. I don't know whether there is a standard way to handle this, but in the code below, I detach the handler before calling SetItemCheck, then reattach the handler. It seems like a clean way to handle this, and it works. If I find that there is a recommended way to handle this, I will update my answer.
HTH
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
if (checkedIndices.Count > 0)
{
if (checkedIndices[0] != e.Index)
{
// the checked item is not the one being clicked, so we need to uncheck it.
// this will cause the ItemCheck event to fire again, so we detach the handler,
// uncheck it, and reattach the handler
checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
checkedListBox1.SetItemChecked(checkedIndices[0], false);
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
}
else
{
// the user is unchecking the currently checked item, so deselect it
checkedListBox1.SetSelected(e.Index, false);
}
}
}

C# Checkbox has strange behavior

When a winform first displays, the checkbox is unchecked by default. If when the form first displays, I click on the checkbox to 'check' it, the checkbox appears checked for a split second and then disappears. The checkedchanged event never fires. However, if anytime after the first initial attempt I click on the checkbox, the value changes (checked to unchecked and vice versa) like it should and the event fires.
Any idea why the checkbox would not check on the first attempt? It appears selected the first time when you hover over it so I know it has focus.
Update: it doesn't matter if you enter data into all other controls first and then click on the checkbox, the first time you click on it, it flashes as checked for a second, and then the check disappears. Anytime after the 1st time though it works. Strange...
Hard to tell without seeing a code snippet. When I've had things like this in the past, it's been due to having duplicate control ids, or wiring up event handlers incorrectly. Have you tried disabling portions of your code and seeing what affects the checkbox behaviour?
Strangely, putting code in the CheckedChanged() to set the value (what it gets set to anyway if I trace through it) seems to work:
if (this.chkbox1.Checked == true)
{
this.chkbox1.Value = "1";
this.chkbox1.Text = "Checked";
}
else
{
this.chkbox1.Value = "0";
this.chkbox1.Text = "Un-checked";
}
I also put a focus() in the click():
if (((System.Windows.Forms.MouseEventArgs)(e)).Clicks <= 1)
{
if (this.chkbox1.Focused == false)
{
this.chkbox1.Focus();
}
}
I have no idea why that fixes the problem, but it does.
do this happen with just one CB? or all the CB on the form.
Have you tried deleting the CB then adding it back?
I would suggest you post the code behind the CB?
For your custom code, I would try using different cast styles, to see if it makes any difference.
If you're using the standard C# syntax, and it fails (vanishing checkbox)
CheckBox checkBox = sender as CheckBox;
I would try using the old-style cast on the sender object and see if it gives you the desired result:
CheckBox checkBox = (CheckBox)sender;
This may give you a hint on the root cause.

Categories