1

I would like to get the value and id of a checkbox in a repeater, and save the value in database. Here is what I have;

Repeater;

  • I have an id like this ; <%# DataBinder.Eval(Container.DataItem, "ID")%>">

the checkbox i want to use

<asp:CheckBox ID="chkRemind"  runat="server" OnCheckedChanged="Check_Changed" AutoPostBack="true" /> 

code behind

    protected void Check_Changed(Object sender, EventArgs e) {

        I need to know the Id and find out the current checkbox value
        so i can do 

        UpdateDB(ID, checkboxValue);


    }

4 Answers 4

1

You can databind the id to a data- attribute, then read that on postback.

cb.Attributes["data-id"] = DataItem.ID;

protected void Check_Changed(Object sender, EventArgs e) {

    var checkbox = sender as CheckBox;

    int ID = int.Parse(checkbox.Attributes["data-id"]);

    UpdateDB(ID, CheckBox.Checked);


}

Make sure you verify that the current user has permission to edit the requested item, since the data-id value can be changed on the client.

Sign up to request clarification or add additional context in comments.

Comments

0

Do this:

    protected void Check_Changed(Object sender, EventArgs e) {

    var item =  sender as Checkbox;

    UpdateDB(item.ID, item.Value);


}

Comments

0

Variable sender contains Control, that triggered event, so you need just cast it to CheckBox

protected void Check_Changed(Object sender, EventArgs e) 
{
   CheckBox chbSomeCheckbox = (CheckBox)sender;
   var ID = chbSomeCheckbox.ID;
   var checkboxValue = chbSomeCheckbox.Checked;

   UpdateDB(ID, checkboxValue);
}

Comments

0

All you have to do is get sender control's ID

protected void Check_Changed(Object sender, EventArgs e) 
{
    Checkbox item =  sender as Checkbox;
    UpdateDB(item.ID, item.Value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.