locked
Repeater control code to work on... RRS feed

  • Question

  • User1182587605 posted

    Hi All,

    I am migrating a project and in the new UI, The repeater is not working. Please help me by looking at the code how to make it work.

    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
    
            <HeaderTemplate>
                <asp:Label ID="ItemCode" Text="ItemCode" runat="server" Width="85" Font-Bold="true" BackColor='#002649' ForeColor='#bfd1e5' ></asp:Label>
                <asp:Label ID="Description" Text="Description" runat="server" Width="255" Font-Bold="true" BackColor='#002649' ForeColor='#bfd1e5'></asp:Label>
                <asp:Label ID="Qty" Text="Qty" runat="server" Width="55" Font-Bold="true" BackColor='#002649' ForeColor='#bfd1e5'></asp:Label>
                &nbsp;<asp:Label ID="Cost" Text="Cost" runat="server" Width="55" Font-Bold="true" BackColor='#002649' ForeColor='#bfd1e5'></asp:Label>
                <asp:Label ID="Value" Text="Value" runat="server" Width="55" Font-Bold="true" BackColor='#002649' ForeColor='#bfd1e5'></asp:Label>
                <br>
            </HeaderTemplate>
       
             <ItemTemplate>
                  <asp:DropDownList ID="ddItemId" runat="server" Width="84" DataTextField='<%# Eval("ItemId")%>' Visible='<%# Eval("ItemId") == "" %>'>
                        </asp:DropDownList>
                        <ajaxToolkit:CascadingDropDown 
                        ID="CascadingDropDown2"
                        runat="server" 
                        Category="ItemId"
                        TargetControlID="ddItemId"
                        PromptText="[Item ID]"
                        ServicePath="ServiceGL.asmx"
                        ServiceMethod="GetItemId"/>
                 <asp:TextBox ID="ItemId" runat="server" Text='<%# Eval("ItemId")%>' Width="79" BackColor='#75aadb' Visible='<%# Eval("ItemId") != "" %>'></asp:TextBox>
    
               	<asp:DropDownList ID="ddItemName" runat="server"  Width="257" BackColor='#75aadb' ReadOnly="True"  DataTextField='<%# Eval("ItemName")%>' Visible='<%# Eval("ItemId") == "" %>'></asp:DropDownList>
                <ajaxToolkit:CascadingDropDown 
                        ID="CascadingDropDown3"
                        runat="server" 
                        Category="ItemName"
                        TargetControlID="ddItemName" 
                        ParentControlID="ddItemId"
                        ServicePath="ServiceGL.asmx"
                        ServiceMethod="GetItemName"/>
                <asp:TextBox ID="ItemName" runat="server" Text='<%# Eval("ItemName")%>' Width="250" BackColor='#75aadb' Visible='<%# Eval("ItemId") != "" %>'></asp:TextBox>
    
                <asp:TextBox ID="Qnty" runat="server" Text='<%# Eval("Qnty")%>' Width="50" BackColor='#75aadb'></asp:TextBox>
    
    
                <asp:DropDownList ID="ddCost" runat="server"  Width="54" BackColor='#75aadb' ReadOnly="True"  DataTextField='<%# Eval("ItemName")%>' Visible='<%# Eval("ItemId") == "" %>'></asp:DropDownList>
                <ajaxToolkit:CascadingDropDown 
                        ID="CascadingDropDown4"
                        runat="server" 
                        Category="Cost"
                        TargetControlID="ddCost" 
                        ParentControlID="ddItemId"
                        ServicePath="ServiceGL.asmx"
                        ServiceMethod="GetItemCost"/>
                <asp:TextBox ID="Cost" runat="server" Text='<%# Eval("Cost")%>' Width="50" BackColor='#75aadb' ReadOnly="True" Visible='<%# Eval("ItemId") != "" %>'></asp:TextBox>
    
    
                <asp:TextBox ID="Value" runat="server" Text='<%# Eval("CalVal")%>' Width="50" BackColor='#75aadb' ReadOnly="True"></asp:TextBox>
    
                <asp:LinkButton ID="UpdateRow" runat="server" CommandName ="UpdateRow" Visible='<%# Eval("ItemId") != "" %>'>Update</asp:LinkButton>
                <asp:LinkButton ID="DeleteRow" runat="server" CommandName ="DeleteRow" Visible='<%# Eval("ItemId") != "" %>'>Delete</asp:LinkButton>
                <asp:LinkButton ID="lnkUpdataDatabase" runat="server" CommandName="UpdateDatabase" Visible='<%# Eval("ItemId") == "" %>'>Add</asp:LinkButton>
                    
    
                <br>
            </ItemTemplate>
    
    
    
            </asp:Repeater>

    My server side code is:

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
                
                
                if (e.CommandName == "AddRow")
                {
                    GetData();
                    dt.Rows.Add("", "", "", "", "");
                    Repeater1.DataSource = dt;
                    Repeater1.DataBind();
                    return;
                }
    
    
                
    
                if (e.CommandName == "UpdateDatabase")
                {
                    try
                    {
    
                        string SQLIns = "";
                        string newInvyNum = TxtHInvyNum.Text;
                        string newInvyDate = InvyDate.Text;
                        string newSite = Site.Text;
                        
                        string newItemId = ((TextBox)e.Item.FindControl("ItemId")).Text;
                        if (newItemId == "")
                        {
                            newItemId = ((DropDownList)e.Item.FindControl("ddItemId")).SelectedValue;
                        }
                        
                        string newItemName = ((TextBox)e.Item.FindControl("ItemName")).Text;
                        if (newItemName == "")
                        {
                            newItemName = ((DropDownList)e.Item.FindControl("ddItemName")).SelectedValue;
                        }
    
                        string newQty = ((TextBox)e.Item.FindControl("Qnty")).Text;
                        //if (newQty == "")
                        //{
                        //    newQty = ((DropDownList)e.Item.FindControl("Qnty")).SelectedValue;
                        //}
                        
                        Int32 newNumQty = Convert.ToInt32(newQty);
                        
                        string newCost = ((TextBox)e.Item.FindControl("Cost")).Text;
                        if (newCost == "")
                        {
                            newCost = ((DropDownList)e.Item.FindControl("ddCost")).SelectedValue;
                        }
                        
                        Int32 Orgnl_Qty;
    
                        //--- Check for the same item already exists...
                        string SQL_verify = "";
                        SQL_verify = "select ItemId,Qty from IC_Inventory_T where Invy_Num = '" + newInvyNum + "' and ItemId = '" + newItemId + "'";
    
                        SqlDataReader ResData;
                        SqlConnection conn = new SqlConnection(ConString);
    
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                        }
    
                        SqlCommand SelCmd = new SqlCommand(SQL_verify, conn);
                        ResData = SelCmd.ExecuteReader();
    
                        if (ResData.Read())
                        {
                            Orgnl_Qty = Convert.ToInt32(ResData["Qty"]);
                            newNumQty = newNumQty + Orgnl_Qty;
                            SQLIns = "Update IC_Inventory_T set Qty = '" + newNumQty + "' where Invy_Num = '" + newInvyNum + "' and ItemID = '" + newItemId + "'";
                        }
                        else
                        {
                            SQLIns = "Insert into IC_Inventory_T (Invy_Num,Invy_Date,SiteID,ItemId,ItemName,Qty,Cost) values ('" + newInvyNum + "','" + newInvyDate + "','" + newSite + "','" + newItemId + "','" + newItemName + "','" + newNumQty + "','" + newCost + "')";
                        }
    
                        ResData.Close();
    
                        //-- End of Check for the same item already exists...
    
    
                        conn = new SqlConnection(ConString);
                        SqlCommand cmd = new SqlCommand(SQLIns, conn);
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                        }
    
                        string Str1 = newQty;
                        string Str2 = newCost;
                        double Num1, Num2;
                        bool isNum1 = double.TryParse(Str1, out Num1);
                        bool isNum2 = double.TryParse(Str2, out Num2);
                        if ((isNum1) && (isNum2))
                        {
                        int temp = cmd.ExecuteNonQuery();
                        }
                        else
                        {
                            MessageBox.Text = "Qty should be numeric";
                        }
    
                        MessageBox.Text = "";
                        conn.Close();
                    }
    
                    catch (Exception ex)
                    {
                        MessageBox.Text = "Qty should be numeric";
                    }
    
                }
    
                if (e.CommandName == "UpdateRow")
                {
                    string SQLUpdt = "";
                    string newInvyNum = TxtHInvyNum.Text;
                    string newItemId = ((TextBox)e.Item.FindControl("ItemId")).Text;
                    string newItemName = ((TextBox)e.Item.FindControl("ItemName")).Text;
                    string newQty = ((TextBox)e.Item.FindControl("Qnty")).Text;
                    string newCost = ((TextBox)e.Item.FindControl("Cost")).Text;
                    // Update Line item
                    SQLUpdt = "Update IC_Inventory_T set Qty = '" + newQty + "' where Invy_Num = '" + newInvyNum + "' and ItemID = '" + newItemId + "'";
    
                    SqlConnection conn = new SqlConnection(ConString);
                    SqlCommand cmd = new SqlCommand(SQLUpdt, conn);
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
    
                    try
                    {
                        int temp = cmd.ExecuteNonQuery();
                        MessageBox.Text = "";
                    }
    
                    catch (Exception ex)
                    {
                        MessageBox.Text = "Qty should be numeric";
                    }
    
                    conn.Close();
                }
    
    
    
    
                if (e.CommandName == "DeleteRow")
                {
                    string SQLDel = "";
                    string newInvyNum = TxtHInvyNum.Text;
                    string newItemId = ((TextBox)e.Item.FindControl("ItemId")).Text;
                    // Delete row
                    SQLDel = "Delete IC_Inventory_T where Invy_Num = '" + newInvyNum + "' and ItemID = '" + newItemId + "'";
    
                    SqlConnection conn = new SqlConnection(ConString);
                    SqlCommand cmd = new SqlCommand(SQLDel, conn);
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    int temp = cmd.ExecuteNonQuery();
                    conn.Close();
                }
    
                GetData();
                dt.Rows.Add("", "", "", "", "");
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
    
            }

    When I populate the same in my code, The repeater does not create any text boxes, When I debug the code, the break point does not hit at the event. Please help me in populating the Repeater in the below UI code:

    <div class="white-table-view-block "><!-- white-table-view-block -->
                        <div class="container-fluid"><!-- container-fluid -->
                            <div class="row"><!-- row -->
                                <div class="col-md-12">
                                    <h2>cost calculation</h2>
                                    <div class="table-responsive">
                                       <%--<table id="Table-id-Name" class="table custom-table hide">
                                            <thead>
                                                <tr>
                                                    <th class="blanck-space">&nbsp;</th>
                                                    <th class="item-number">Item Number <i class="icon"><img src="images/item-number-icon.png" alt="" /></i></th>
                                                    <th class="item-description">Description</th>
                                                    <th class="item-quantity">Quantity</th>
                                                    <th class="item-cost">Cost</th>
                                                    <th class="item-value">Value</th>
                                                    <th class="item-action">&nbsp;</th>
                                                </tr>
                                            </thead>
                                            <tbody class="tbody-content">
                                               
                                            </tbody>
                                       </table>--%>
    
                                        <asp:Panel runat="server" ID="pnlTextBoxes" CssClass="col-md-12">
                                            <table id="Table-id-Name" class="table custom-table hide">
                                            <thead>
                                                <tr>
                                                    <th class="blanck-space">&nbsp;</th>
                                                    <th class="item-number">Item Number <i class="icon"><img src="images/item-number-icon.png" alt="" /></i></th>
                                                    <th class="item-description">Description</th>
                                                    <th class="item-quantity">Quantity</th>
                                                    <th class="item-cost">Cost</th>
                                                    <th class="item-value">Value</th>
                                                    <th class="item-action">&nbsp;</th>
                                                </tr>
                                            </thead>
                                            </table>
                                        </asp:Panel>
    
                                       <table class="table table-total table-total-2" align="right">
                                            <tbody class="tbody-total">
                                                <tr>
                                                    <td class="item-label-qty">Total Qty</td>
                                                    <td class="item-value-field-qty"><asp:Textbox runat="server" class="form-control" placeholder="" ID="txtTotalQuantity" /></td>
                                                    <td class="item-label">Total Value</td>
                                                    <td class="item-value-field"><asp:TextBox runat="server" class="form-control" placeholder="$" ID="txtTotalValue" /></td>
                                                    <td class="item-blanck"></td>
                                                </tr>
    
                                            </tbody>
                                       </table>
    
                                    </div>
                                </div>
                            </div>
                            <!-- /row -->
                        </div>
                        <!-- /container-fluid -->
                  </div>

    I know there is a lot of code but I am unable to find the exact error reason. The break point at the same code in presentation(.cs code) does not work. Please help me.

    Regards,

    Deepak 



    Sunday, November 6, 2016 7:40 AM

Answers

  • User1182587605 posted

    Yes, I have checked that. But now, I have placed two repeater controls one to fetch data and the other to insert values. This is working fine for me.

    Thanks,

    Deepak

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 29, 2016 7:17 AM

All replies

  • User-1716253493 posted

    What do you mean not working? Where addrow button?

    Sunday, November 6, 2016 10:42 AM
  • User1724605321 posted

    Hi acmedeepak,

    When I populate the same in my code, The repeater does not create any text boxes, When I debug the code, the break point does not hit at the event. Please help me in populating the Repeater in the below UI code:

    I have not found your Repeater control in that UI , Do you want to dynamically create the textbox table like Repeater ? 

    Best Regards,

    Nan Yu

    Tuesday, November 8, 2016 5:11 AM
  • User1182587605 posted

    Hi Nan Yu,

    The repeater is to be placed in my code and the textboxes need to appear same as the old aspx code, I placed it and tried but the textboxes does not appear I thought I did some mistake so I have removed it before posting it here. That is why I have just posted headings in my code. You can place a new repeater. Please look into it.

    Regards,

    Deepak

    Tuesday, November 8, 2016 6:21 AM
  • User-1716253493 posted

    Place the textboxes inside ItemTemplate and set repeater datasource then call databind() to show the textboxes. Ensure you have data in the datasource

    Tuesday, November 8, 2016 6:43 AM
  • User1182587605 posted

    Hi oned_gk,

    can you please give me some code for your explanation. That would help me a lot.

    Regards,

    Deepak

    Tuesday, November 8, 2016 8:08 AM
  • User-1716253493 posted

    You can see in your first post, the textboxes is inside itemtemplate.

    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
            <HeaderTemplate>
                     .......
            </HeaderTemplate>
       
            <ItemTemplate>
                    textboxes here
            </ItemTemplate>
     </asp:Repeater>

    Find another c# code there that call Repeater1.DataBind() usualy in page_load event.

    Something like this

    //get the datatable from database here
    Repeater1.DataSource = dt; Repeater1.DataBind();

    If any rows in the datatable dt, you will see the textboxes

    Tuesday, November 8, 2016 8:43 AM
  • User1182587605 posted

    Hi oned_gk,

    Sorry for late reply. I am sorry to say that even this sis not work. Please help me in this.

    Wednesday, November 16, 2016 10:31 AM
  • User1724605321 posted

    Hi acmedeepak,

    After migrating  to new UI , you keep the same markup and code behind event of Repeater ? Then what is the error , have you debug your application to check whether the DataSource is not null (has record)?

    Best Regards,

    Nan Yu

    Tuesday, November 29, 2016 6:51 AM
  • User1182587605 posted

    Yes, I have checked that. But now, I have placed two repeater controls one to fetch data and the other to insert values. This is working fine for me.

    Thanks,

    Deepak

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 29, 2016 7:17 AM