PedroC88
PedroC88

Reputation: 3829

Checkbox on GridView always returning False

I have a GridView with a Checkbox on the first column:

<asp:GridView ID="dgNumeradores" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ItemID">
    <Columns>
        <asp:TemplateField HeaderText="Seleccionar">
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="chkChecked" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Item" DataField="Description">
        </asp:BoundField>
        <asp:BoundField HeaderText="Plantilla" DataField="Template">
        </asp:BoundField>
    </Columns>
</asp:GridView>

Now in the code behind I try to update the Checked column on the DataTable acting as datasource for the GridView (since, as you can see above, the Checkbox column is not bound to the datasource for reasons you probably know.):

Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Try
        For Each dr As GridViewRow In Me.dgNumeradores.Rows
            Me.itemsNumTable.Select("ItemID = '" & dgNumeradores.DataKeys(dr.RowIndex).Value & "'")(0)("Checked") = DirectCast(dr.Cells(0).FindControl("chkChecked"), CheckBox).Checked
        Next
        'Some more unimportant-for-this-question code
    Catch ex As Exception
        tableInfo.ShowError(ex.Message)
    End Try
End Sub

The thing is that the Checkbox.Checked always returns False.

How can I get the checked state of the Checkboxes in this scenario? Or what would be the best approach into updating the aforementioned column?

P.S. Note that click on the checkboxes doesn't post back. Nothing happens on the page until the user clicks Save (and that is the intended behavior).

Upvotes: 1

Views: 9629

Answers (4)

Pydan
Pydan

Reputation: 67

I have two columns in my GridView. The first column contains filenames, the second column contains Checkboxes. Once the user has selected an arbitrary number of Checkboxes then by clicking a button the selected files can be downloaded.

My markup is as follows

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
            <asp:BoundField HeaderText="Available Schemas" 
                DataField="SchemaFileName" 
                SortExpression="UserId">
            </asp:BoundField>
            <asp:TemplateField HeaderText="Select Schema">
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="SelectedFiles"  checked= '<%# Eval("checkValue") %>'/>
                </ItemTemplate>
            </asp:TemplateField>
    </Columns>
</asp:GridView>

My CodeBehind part is as follows

protected void Page_Load(object sender, EventArgs e)
{
    GenerateDownloadLinks();

    if (!IsPostBack)
    { 
    GridView1.DataSource = listOfData;
    GridView1.DataBind();
    }
}

listOfData is populated in GenerateDownloadLinks() and then it is bind to GridView1.

Once the user selected files and clicks download then my code loops through the rows of the GridView and when the CheckBox is checked it updates the initially false value of the data entry to make sure which files should be made available for download.

protected void GetFiles_Click(object sender, EventArgs e)
{
    int i = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chkRow = (row.Cells[1].FindControl("SelectedFiles") as CheckBox);
        if (chkRow.Checked)
        {
            listOfData[i].CheckValue = true;
        }
        i++;
    }
}

Upvotes: 0

user5102792
user5102792

Reputation: 1

Gridview fills perfectly even when it is not binded in Page.IsPostBack block, but here checkbox will always return false.

Bind gridview in Page.IsPostBack, and it would run perfectly fine.

Use below code IF Not IsPostBack Then DataBind() End IF

And then Checkbox.Checked will return true.

Upvotes: -1

Seany84
Seany84

Reputation: 5596

Should you not have the AutoPostback property set to true?

<asp:CheckBox runat="server" ID="chkChecked" AutoPostback="true" />

Upvotes: 2

Mubarek
Mubarek

Reputation: 2689

Are you binding the GridView in Page Load? If that is the case use IsPostBack

IF Not IsPostBack Then
   DataBind()
End IF

Upvotes: 8

Related Questions