0

I have a checkbox in repeater control. on changing it's bit value, it should prompt the alert message like do u wish to delete? and if yes is selected, the onselectedchange server event should be fired.

Right now. on clicking the checkbox i am using onclick event to fire the prompt but it is not able to fire the server event.

here is my code

<script language="javascript" type="text/javascript">

function CheckedChange() {
         if (confirm('Are you sure you want to delete?'))
             return true;
         else
             return false;
     }
</script>

<asp:Repeater ID="repQuestion" runat="server"  onitemcommand="repQuestion_ItemCommand"                                                    onitemdatabound="repQuestion_ItemDataBound">

<ItemTemplate>
<table  width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
<asp:CheckBox onclick="return CheckedChange();" ID="delete" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "IsDeleted")%>' OnCheckedChanged="chkdelete_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Pankaj
  • 9,749
  • 32
  • 139
  • 283

3 Answers3

3

Work around this snippet.

The Repeater.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <table>
            <tr>
                <td><%# Eval("Index") %></td>
                <td>
                    <asp:CheckBox ID="delete" runat="server" 
                            AutoPostBack="true"
                            Checked='<%# Convert.ToBoolean(Eval("IsDeleted")) %>'
                            Enabled='<%# !Convert.ToBoolean(Eval("IsDeleted")) %>'
                            OnCheckedChanged="chkdelete_Click" 
                            onclick="javascript:return CheckedChange(this)"/>              
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

JavaScript

function CheckedChange(objCheckBox) {
    if (confirm('Are you sure you want to delete?')) {
        __doPostBack("'" + objCheckBox.id + "'", '');
        return true;
    }
    else {
        objCheckBox.checked = false;
        return false;
    }
}

Page_Load Event

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            //bind gridview here
            DataTable dataTable = new DataTable
            {
                Columns = { "Index", { "IsDeleted", typeof(bool) } }
            };
            bool temp = true;
            for (var i = 0; i < 6; i++)
            {
                dataTable.Rows.Add((i + 1).ToString(), temp);
                temp = !temp;
            }

            Repeater1.DataSource = dataTable;
            Repeater1.DataBind();
        }
    }
    catch (Exception exception)
    {
        //Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
    }
}

Well, I don't personally like exposing the __doPostBack but this seems to be the only way in this case.

On an unrelated side note, if you are showing deleted items, its better to show them disabled.

naveen
  • 53,448
  • 46
  • 161
  • 251
  • It is giving javascript error "Microsoft JScript runtime error: Object expected" on executing of html code. – Pankaj Aug 30 '11 at 05:12
  • which version of asp.net are you running. i am running 4.0 – naveen Aug 30 '11 at 05:18
  • i have pasted my working snippet. what are you doing differently? perhaps not setting `AutoPostBack=true`? – naveen Aug 30 '11 at 05:34
  • My last question in this thread - Why it is not recommended to exposing __doPostBack ?? – Pankaj Aug 30 '11 at 08:46
  • @Pankaj: because thats framework version related. say at a future iteration say asp.net 10.0, the architects tend to postback using `__doit` then our code will be obslete. `__dopostback` is a name they give and its not guaranteed to stay for ever. – naveen Aug 30 '11 at 10:08
1

Try by passing the UniqueID (that is the name of the Checkbox).

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <table>
            <tr>
                <td><%# Eval("Index") %></td>
                <td>
                    <asp:CheckBox ID="delete" runat="server" 
                            AutoPostBack="true"
                            Checked='<%# Convert.ToBoolean(Eval("IsDeleted")) %>'
                            Enabled='<%# !Convert.ToBoolean(Eval("IsDeleted")) %>'
                            OnCheckedChanged="chkdelete_Click" 
                            onclick="javascript:return CheckedChange(this)"/>              
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

JavaScript

function CheckedChange(objCheckBox) {
    if (confirm('Are you sure you want to delete?')) {
        __doPostBack("'" + objCheckBox.name + "'", '');
        return true;
    }
    else {
        objCheckBox.checked = false;
        return false;
    }
}
Sreekumar P
  • 5,900
  • 11
  • 57
  • 82
0

Try this

    function confirmUser(checkBox)
    {
        if(checkBox.childNodes[0].checked === false){
            alert('Your alert message');
            }
    }
<asp:Repeater ID="rptSelecteduserRoles" runat="server" OnItemDataBound="rptSelecteduserRoles_OnItemDataBound">
<ItemTemplate>
                                                        
<asp:CheckBox ID="chkBox" CssClass="fieldLabel" AutoPostBack="False" onchange="confirmUser(this)"runat="server" Text='<%# Eval("Name") %>' />
                                                        
 </ItemTemplate>
 </asp:Repeater>
Muheeb
  • 216
  • 2
  • 6