Jurioli / Blazor.WebForm.Components

ASP.NET Web Forms System.Web.UI.WebControls Razor Components For Blazor WebAssembly, Blazor Hybrid, Blazor Server.
MIT License
47 stars 9 forks source link

CheckBox's Parent is null (in Gridview) #16

Closed MG1376 closed 6 months ago

MG1376 commented 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.

Jurioli commented 6 months ago

To obtain the ControlComponent Parent, you can use the ContainerParent property.

        DataControlFieldCell cell = (DataControlFieldCell)chk.ContainerParent;