Closed MG1376 closed 6 months ago
I have this checkbox in my GridView:
<ItemTemplate TItem="GreenPaperItem"> <asp.CheckBox ID="CheckBox" OnCheckedChanged="((s, e) => this.GVCheckBox_CheckedChanged(s, e/*, context.Id*/))" AutoPostBack="true"> </asp.CheckBox> <asp.Button Text="Delete" CommandName="Delete" CssClass="btn btn-danger"> </asp.Button> <asp.Button Text="Edit" CommandName="Edit" CssClass="btn btn-primary"> </asp.Button> </ItemTemplate>
And here is the event handler:
List<int> selectedRowIds = new List<int>(); protected void GVCheckBox_CheckedChanged(object sender, EventArgs e/*, int id*/) { UI.CheckBox chk = (UI.CheckBox)sender; Task.Delay(100); InvokeAsync(() => StateHasChanged()); UI.GridViewRow gr = (UI.GridViewRow)chk.Parent.Parent; //<< the first parent returns null int id = (int)gv.DataKeys[gr.RowIndex].Value; if (chk.Checked && !selectedRowIds.Contains(id)) selectedRowIds.Add(id); else if (!chk.Checked && selectedRowIds.Contains(id)) selectedRowIds.Remove(id); StateHasChanged(); }
But the parents are null. Is there any way so that this code works? Currently I have to send ites item Id to the method.
To obtain the ControlComponent Parent, you can use the ContainerParent property.
DataControlFieldCell cell = (DataControlFieldCell)chk.ContainerParent;
I have this checkbox in my GridView:
And here is the event handler:
But the parents are null. Is there any way so that this code works? Currently I have to send ites item Id to the method.