Datalist with Checkbox triggering serverside event

(ASP.NET)


Hi,
In this article i placed checkbox inside the datalist, on click of the checkbox it will trigger the server side event.

.aspx Page

<asp:DataList OnItemDataBound="dtlst_withcheckbox_ItemDataBound" OnItemCommand="dtlst_withcheckbox_ItemCommand"  ID="dtlst_withcheckbox" runat="server"  >
            <HeaderTemplate>
                <asp:Label ID="lbl_productid" runat="server" Text="Product ID" />
                <asp:Label ID="lbl_productname" runat="server" Text="Product Name" />
            </HeaderTemplate>

<ItemTemplate>
<asp:CheckBox ID="chck_dtlstproductid" AutoPostBack="true"  runat="server"
  Text='<%#Eval("ProductID") %>'   />
     <asp:Button ID="btn_hidden" runat="server" Text="HiddenBtn" style="display:none;" CommandName="CheckBoxEvent" CausesValidation="false"/>
    <asp:Label ID="lbl_productName" runat="server" Text='<%#Eval("ProductName") %>' />
</ItemTemplate>

           
        </asp:DataList>


Code behind:-

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    bindDataList();
                }
            }
            catch (Exception)
            { }
        }

        protected void bindDataList()
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

            string query = "Select * from Products";

            DataSet ds = new DataSet();

            SqlDataAdapter sqladp = new SqlDataAdapter(query, sqlcon);

            sqladp.Fill(ds);

            dtlst_withcheckbox.DataSource = ds;
            dtlst_withcheckbox.DataBind();

          
        }

        protected void dtlst_withcheckbox_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("CheckBoxEvent"))
                {
                    string str = "Hello ";
                    str += "World!";

                    //write your logic here using source and eventargs e
                }
            }
            catch (Exception)
            { }
        }

        protected void dtlst_withcheckbox_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                if (item != null)
                {
                    var chk = e.Item.FindControl("chck_dtlstproductid") as CheckBox;
                    var btn = e.Item.FindControl("btn_hidden") as Button;
                    if (chk != null)
                    {

                        chk.Attributes.Add("onclick", "javascript:document.getElementById('" + btn.ClientID + "').click()");
                    }
                }
            }
        }



Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/28/2013
6/28/2013
Download PDF

Comments