Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox autopostback question

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
I have this in my aspx

Code:
<asp:TemplateField HeaderText="Date Analyzed" SortExpression="Date Analyzed" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate >
        <asp:Label ID="lblDateAnalyzed" runat="server" Text='<%# Eval("Date Analyzed", "{0:d}") %>' />
    </ItemTemplate>
    <EditItemTemplate >
        <asp:Checkbox ID="DateAnalyzed" runat="server" Text='<%# Eval("Date Analyzed", "{0:d}")%>' AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged" />
    </EditItemTemplate>
</asp:TemplateField>

and then this in my code behind

Code:
        protected void chkSelect_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            GridViewRow row = (GridViewRow)chk.NamingContainer;
            HiddenField hiddenID = (HiddenField)row.FindControl("ID");

            string sql = "UPDATE CreditReports.dbo.Customers SET [Date Analyzed]=convert(date, getdate()) WHERE ID=@ID";
            string connStrII = ConfigurationManager.ConnectionStrings["visa"].ConnectionString;
            using (var con = new SqlConnection(connStrII))
            using (var updateCommand = new SqlCommand(sql, con))
            {
                updateCommand.Parameters.AddWithValue("@ID", int.Parse(hiddenID.Value));
                updateCommand.Parameters.AddWithValue("@Status", chk.Checked);
                con.Open();
                int updated = updateCommand.ExecuteNonQuery();
            }
            this.BindGridView();
        }

which all works, however... The point of this code is to enter today's date when the checkbox is checked, which it does. The caveat is that when it re-binds the gridview it shows both the checkbox AND the date that was just entered. Is there any nice way to, once the box has been checked, not have the checkbox show up any more? So, before it is checked the value is null, but I am not sure how I can use that in this situation.

Thanks,
Willie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top