comidos
comidos

Reputation: 123

Checkbox in ASP-GridView with FindControl NEVER checked

I have on a ASPX-WebForm in Classic ASP a GridView with Customer-Informations for CSV-Export.

In this GridView i have in TemplateField a Checkbox for Selection by the User. In CodeBehind on ClickEvent I go with ForEach-Loop through the Rows of the GridView and want to check the self-generated (data comes not from database) Checkbox if checked. With FindCOntrol i unbox the Checkbox and up to there is everything allright. But every Checkbox is NOT checked although it is checked on the Webform.

Can anyone tell me what I´m doing wrong?

ASPX:

<body>
<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridViewAdressen" runat="server" AutoGenerateColumns="false" DataKeyNames="order_id" EnablePersistedSelection="true" style="width:100%;" PagerSettings-Mode="Numeric">
        <Columns>
            <asp:TemplateField HeaderText="Auswahl">
                <ItemTemplate>
                    <input type="checkbox" name="chkSelect" runat="server"/>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField HeaderText="ID" DataField="order_id"/>
            <asp:BoundField HeaderText="Zeit" DataField="zeitpunkt"/>
            <asp:BoundField HeaderText="Preis" DataField="preis"/>
            <asp:BoundField HeaderText="Name" DataField="name"/>
            <asp:BoundField HeaderText="Nick" DataField="username"/>

        </Columns>
    </asp:GridView>

    <asp:Button Text="CSV erstellen" runat="server" OnClick="Unnamed_Click"/>

</div>
</form>

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["security"] != null && Request.QueryString["security"] == token.seitenSicherheit)
    {
        // Anzahl Adressen
        string menge = "10";
        if (Request.QueryString["menge"] != null)
        {
            menge = Request.QueryString["menge"];
        }

        DataTable table = new DataTable();
        using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQLConnStr"].ConnectionString))
        {
            connection.Open();
            using (OdbcCommand command = new OdbcCommand("SELECT * FROM bestellvorgang order by zeitpunkt desc limit " + menge, connection))
            {
                using (OdbcDataAdapter ad = new OdbcDataAdapter(command))
                {
                    ad.Fill(table);
                }
            }
        }
        GridViewAdressen.DataSource = table;
        GridViewAdressen.DataBind();
    }
    else
    {
        Response.Write("Keine Zugangsberechtigung");
    }
}

protected void Unnamed_Click(object sender, EventArgs e)
{
    // string für csv erstellen

    // Grid View durchlaufen und checkBox prüfen
    foreach (GridViewRow row in GridViewAdressen.Rows)
    {
        HtmlGenericControl chk = (HtmlGenericControl)row.FindControl("chkSelect");


        if (chk.Checked)
        {
            // Abfrage für den order_id
            Response.Write("Order-ID: " + GridViewAdressen.DataKeys[row.RowIndex].Value);

        }

    }
}

UNBOXING WITH FINDCONTROL IS NOT NULL, BUT NEVER CHECKED !!!!!!!

Alternatively i have tried to use Html-input Checkbox but thats always NULL when use FindControl und unbox as HtmlGenericControl ....

Upvotes: 0

Views: 449

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You should wrap the DataBinding of the GridView inside an IsPostBack check

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["security"] != null && Request.QueryString["security"] == token.seitenSicherheit)
    {
        if (IsPostBack == false)
        {
            //rest of your code

            GridViewAdressen.DataSource = table;
            GridViewAdressen.DataBind();
        }
    }
    else
    {
        Response.Write("Keine Zugangsberechtigung");
    }
}

If you do not, data is rebound on every PostBack, and that will make any changes (like checking a CheckBox) reset to their default values.

Upvotes: 1

Related Questions