Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code I select a row from datalist using 'add to cart' button event.
I want to pass certain columns to a datatable as a new row and later bind into a gridview.
It works without error or exception but the values are not getting assigned to respective TextBox's and with respective columns of the datatable. Can any one help me out. I am struck here.

XML
<asp:DataList ID="DataList1" runat="server"  RepeatColumns="2" OnItemCommand="dl_item_command"   RepeatDirection="Horizontal"  >
    <ItemTemplate>
    <table id="Table1" cellpadding="1" cellspacing="1" visible ="true">
    <tr>
    <td width="140px">
    <p align="left">
    <b>Product ID: </b>
    <asp:Label ID="lblEmpCode" runat ="server"
    Text= '<%#DataBinder.Eval(Container.DataItem, "product_id")%>'>
    </asp:Label>
    </p>
    </td>
    </tr>
    </table>
    <table id="Table2" style=" margin-top:-33px" cellpadding="1" cellspacing="1" visible ="true">
    <tr>
    <td>
    <td  style="left:0px"width="200px">
    <p align="left">
    <b>Product Name: </b>
    <asp:Label ID="lblEmpName" runat = "server"
    Text=' <%# DataBinder.Eval(Container.DataItem, "product_name")%>'>
    </asp:Label>
    </p>
    </td>
    </tr>
    </table>
    <table id="Table6" cellpadding="1" style=" margin-top:-39px" cellspacing="1"  width="360px" visible ="true">
    <tr>
    <td >
    <p align="left">
    <asp:TextBox ID="AddToCartTB"  runat = "server" Text='1' Width="44px"> </asp:TextBox>

    <asp:Button ID="AddToCart"  runat = "server" Text='Add to Cart' Width="100px" commandname="myevent">
    </asp:Button>
    </p>
    </td>
    </tr>
    </table>
    </ItemTemplate>
</asp:DataList>




C#
private void AddNewRow()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox TB_Productid =
                      (TextBox)grvcart.Rows[rowIndex].Cells[2].FindControl("TBProductId");
                    TextBox TB_ProductName =
                      (TextBox)grvcart.Rows[rowIndex].Cells[3].FindControl("TBProductName");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Col1"] = TB_Productid.Text;
                    dtCurrentTable.Rows[i - 1]["Col2"] = TB_ProductName.Text;
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;
                grvcart.DataSource = dtCurrentTable;
                grvcart.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }
Posted
Updated 17-Nov-13 20:33pm
v2
Comments
Er Daljeet Singh 19-Nov-13 1:40am    
i think you should try your code by selecting two record from the datalist.
try to add 2item in your cart and see what happen,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900