Avalonia DataGrid Enter handling - datagrid

I'm using Avalonia.Controls.DataGrid. By default, when the grid has focus and Enter is pressed, it automatically handles the event and moves the selection to the next item. How can I prevent this default behavior? I'd like to have a custom Enter KeyDown handler instead.

So the KeyDown event cannot be used here, because specifically for Enter it's swallowed by the DataGrid before custom code can handle it.
Instead, Key bindings work. You can bind the key to a command like this:
<DataGrid ...>
<DataGrid.KeyBindings>
<KeyBinding Gesture="Enter" Command="{Binding SelectCommand}" />
</DataGrid.KeyBindings>
...
</DataGrid>
This also stops the grid from moving to next item when Enter is pressed.

Try this :
1- Give your datagrid a name (use x:name)
2- put this in your constructor :
yourDataGridName.KeyDown += yourDataGridName_KeyDown;
3- add this handler :
protected void yourDataGridName_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//Put your custom code here
}
}

https://github.com/AvaloniaUI/Avalonia/issues/10130
I use AddHandler for DataGrid. As the developer advised.
dataGrid.AddHandler(KeyDownEvent, dataGrid_PreviewKeyDown, RoutingStrategies.Tunnel);
private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//
e.Handled = true;
}
}

Related

Add/Edit/Delete buttons for Telerik RadTreeView control

Currently, I am using RadTreeView Telerik control for showing hierarchical data along withadd/edit/delete functionalities for each node. Using TreeView - Context Menu, that has been achieved, but I am trying to implement it as shown in below:
It works the following way:
a) When a node is expanded by clicking "+" icon, "Add Group" button is visible at the bottom of its last child.
b) When a node is selected, "Edit" and "Delete" icons appear.
On clicking any of those icons will open a Dialog for respective actions.
So, I need to replace Context Menu with the display shown in mock. I tried to use NodeTemplate something like below:
<NodeTemplate>
<div>
<span>Test</span>
</div>
</NodeTemplate>
but, it makes the text of all nodes as "Test".
Can somebody please help me out?
<script type="text/javascript">
function OnClientContextMenuItemClicking(sender, args)
{
var menuItem = args.get_menuItem();
var treeNode = args.get_node();
menuItem.get_menu().hide();
switch (menuItem.get_value())
{
case "edit":
treeNode.startEdit();
break;
}
}
</script>
Hope this helps,
I implemented the RadTreeView in serverside. I though it is easy to manage in server side, specially when it comes to binding and handling the events
This is my Default.aspx.cs
using Telerik.Web.UI;
public partial class Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
}
protected void RadTreeView1_NodeEdit(object sender, RadTreeNodeEditEventArgs e)
{
// This is the serverside event that is triggered on Edit
RadTreeNode nodeEdited = e.Node; // This is the current node that is clicked
string newText = e.Text;
nodeEdited.Text = newText;
}
//
//.........
//.........
}
This is something extra. In server-side you can also find a node like this,
RadTreeNode node = RadTreeView1.FindNodeByText("TestOne");
// now edit and change any test
node.Text = "Test";
I hope this helps.

Load a dynamic custom user control added during page execution (button click event)

I'm trying to add a custom control to a panel (containing a "form" to input data) during a button click event and I want to access it's methods like .Validate() after this data has been inputted. But when I try to do so the ctrl comes with null value.
Here's part of my code :
protected void btnNext2_Click(object sender, EventArgs e)
{
...
ctrlCompliance = (Compliance)LoadControl("../../ascx/SRM/Compliance.ascx");
ctrlCompliance.ReadOnly = false;
pnlCompliance.Controls.Add(ctrlCompliance);
...
}
protected void btnNext3_Click(object sender, EventArgs e)
{
...
ctrlCompliance = pnlCompliance.Controls[0] as Compliance;
ctrlCompliance.Validate() <- this is allways null
...
}
I cannot use Page_Init as most of the solutions propose and really need to load it only during that button click. Did someone had the same problem as me?
You should load it anyway using Page_Init, but set its visible property to False. Then using javascript or code-behind you can change its visible property to True when you need to show the print button.
So move:
ctrlCompliance = (Compliance)LoadControl("../../ascx/SRM/Compliance.ascx");
ctrlCompliance.ReadOnly = false;
pnlCompliance.Controls.Add(ctrlCompliance);
to Page_Init

asp.net How to get the id of the button user clicked

I have a small problem. I am developing an online seat reservation system which allow users to click and select seats. I am using buttons for seats. I am writing a method for the button click event for all buttons. I need to know how to identify the id of the button, user clicked. thanks in advance.
When you use a button clicked event, the signature of the method that'll handle the event goes like this:
protected void foo (object sender, EventArgs e)
sender in this case is the button that was clicked. Just cast it to Button again and there you have it. Like this:
Button button = (Button)sender;
string buttonId = button.ID;
You can have all your buttons pointing their Click event to the same method like this.
The first parameter of your event handler (object source or sometimes object sender) is a reference to the button that was clicked. All you need to do is cast it and then you can retrieve the id value, e.g.:
void Button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
//get the id here
}
}
I think there's another way to do this method you may not thought of it,
why don't you put an
Image control
you only need switch the image's color for this method, the first one we assume that it's Red and it means free seat, and the second one is Blue and means taken .
here is a link you may need to know about this issue How to change color of Image at runtime
Hope I could gave you a better idea. thank you
I know this is an old thread but hopefully it helps someone out there having the same trouble I was here in 2019! This is my solution using the Tag property of the button with Binding through XAML. Worked great for me, even though it might be a little hacky:
In your button click event:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button EditButton = (Button)e.Source;
int id = Convert.ToInt32(EditButton.Tag);
//do your stuff
}
In your XAML:
<Button Click="Button_Click" Content="Edit" Tag="{Binding whateverInt}" />

button click event in datagridview

I am having a button cell in datagridview.When that button is clicked,another datagridview should be visible .For every button click in the button column,the data in new datagridview should be differed.I dont know how to implement the button click event which differs for every row.Please help me with the sample code.
You can't implement a button clicked event for button cells in a DataGridViewButtonColumn. Instead, you use the DataGridView's CellClicked event and determine if the event fired for a cell in your DataGridViewButtonColumn. Use the event's DataGridViewCellEventArgs.RowIndex property to find out which row was clicked.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
// Ignore clicks that are not in our
if (e.ColumnIndex == dataGridView1.Columns["MyButtonColumn"].Index && e.RowIndex >= 0) {
Console.WriteLine("Button on row {0} clicked", e.RowIndex);
}
}
The MSDN documentation on the DataGridViewButtonColumn class has a more complete example.
use dataGridView1_CellContentClick instead of dataGridView1_CellClick
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 8) //make sure button index here
{
//write your code here
}
}

Link Button on ASP.NET user control not firing

I have a user control, which is added to another user control. The nested user control is built up of a GridView, an image button and a link button. The nested user control is added to the outer control as a collection object based upon the results bound to the GridView.
The problem that I have is that my link button doesn't work. I click on it and the event doesn't fire. Even adding a break point was not reached. As the nested user control is added a number of times, I have set image button to have unique ids and also the link button. Whilst image button works correctly with its JavaScript. The link button needs to fire an event in the code behind, but despite all my efforts, I can't make it work. I am adding the link button to the control dynamically. Below is the relevant code that I am using:
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
public event EventHandler ViewAllClicked;
protected override void CreateChildControls( )
{
base.CreateChildControls( );
string strUniqueID = lnkShowAllCust.UniqueID;
strUniqueID = strUniqueID.Replace('$','_');
this.lnkShowAllCust.ID = strUniqueID;
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
}
I have been stuggling with this problem for the last 3 days and have had no success with it, and I really do need some help.
Can anyone please help me?
My LinkButton wasn't firing it's Click event, and the reason was I had its CausesValidation property set to True. If you don't want the link to validate the form, be sure to set this to False.
Try adding your click event to the linkbutton tag:
<asp:LinkButton runat="server" OnClick="linkShowAllCust_Click" />
Or adding it to your Page_Load:
Page_Load(object sender, EventArgs e)
{
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
Is the usercontrol within the gridview? If so register the event handler on the gridview's onrowcreated event.
It appears that you have a viewstate issue. Because the control isn't there when the viewstate is loaded the application doesn't know how to hook up the event to be fired. Here is how to work around this.
You can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I had the same issue. I had viewstate="false" on the page I was adding the control to. (on the aspx page)

Resources