Mike
Mike

Reputation: 435

asp.net checkbox never calls method

VS2010

Hello, I have a webpage that contains a gridview populated from a database. One of the fields is a checkbox. What I am trying to do is update a value in the database when the checkbox is clicked. However I have a breakpoint in the gvSiteInfo_CheckBoxUpdate() method and it is never reached.

Can you point me in the right direction to get this to work properly? The few examples I have found on the web appear to be set up like mine.

Below is an abbreviated version of my markup.

<asp:GridView ID="gvSiteInfo" runat="server" 
    AutoGenerateColumns="False" OnSorting="gvSiteInfo_Sorting" 
    AllowSorting="True">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:BoundField DataField="prodHostHeader" HeaderText="Production Host Header" 
            SortExpression="prodHostHeader" />
        <asp:BoundField DataField="prodDirectory" HeaderText="Production Directory" 
            SortExpression="prodDirectory" />
        <asp:BoundField HeaderText="Active Issues" DataField="issueCount"
            SortExpression="issueCount" />
        <asp:TemplateField HeaderText="Testing Complete" SortExpression="true">
            <ItemTemplate>
                <asp:CheckBox ID="cbTestComplete" runat="server" CausesValidation="true" AutoPostBack="true" OnCheckedChanged="gvSiteInfo_CheckBoxUpdate"
                 Checked='<%# DataBinder.Eval(Container, "DataItem.testComplete").ToString().Equals("true") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="cbTestComplete" runat="server" CausesValidation="true" AutoPostBack="true" OnCheckedChanged="gvSiteInfo_CheckBoxUpdate"
                     Checked='<%# DataBinder.Eval(Container, "DataItem.testComplete").ToString().Equals("true") %>' />             
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Here is the code that should be called

public void gvSiteInfo_CheckBoxUpdate(object sender, EventArgs e)
{
        SiteDB dbAccess = new SiteDB();
        dbAccess.updateSiteInfo();
}

---------------EDIT----------------------------- Here is my page_load code

    protected void Page_Load(object sender, EventArgs e){
        SiteDB dataAccess = new SiteDB();
        dataAccess.SelectedConnectionString = "WISQL01";

        DataTable dt = dataAccess.getSiteInfo();

        gvSiteInfo.DataSource = dt;
        gvSiteInfo.DataBind();
    }

-------------UPDATE----------------------------

added CausesValidation="true" to <asp:checkbox>

Upvotes: 0

Views: 250

Answers (2)

Yuriy Galanter
Yuriy Galanter

Reputation: 39807

You need to recreate grid content (rebind the grid to the data source) on every postback. Since checkbox cell is generated dynamically - it has to be recreated on every postback for event handler to take effect.

Upvotes: 2

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You have problem about binding of your GridView, when you bind for every time, you erase your event.

you must adopt this behavior of binding

if(! IsPostBack)
{
  Bind();
}

And use EnableViewState=true in order to persist your gridview

Upvotes: 1

Related Questions