locked
I am stuck on this line - second line after int rowIndex=0; This line = int rowID = e.RowIndex; RRS feed

  • Question

  • User2052676935 posted

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="RepeaterControl.Test" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <title></title>
    <style type="text/css">
    .auto-style1 {
    width: 100%;
    }
    .auto-style2 {
    width: 164px;
    }
    </style>
    </head><body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
    <div class="row">
    <div class="col-md-2">
    <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
    </div>
    <div class="col-md-2">
    <asp:Label ID="Label2" runat="server" Text="Email"></asp:Label>
    </div>
    <div class="col-md-2">
    <asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label>
    </div>
    </div>
    </HeaderTemplate>
    <ItemTemplate>
    <div class="row">
    <div class="col-md-2">
    <asp:TextBox ID="txtname" runat="server" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="col-md-2">
    <asp:TextBox ID="txtemail" runat="server" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="col-md-2">
    <asp:TextBox ID="txtmobile" runat="server" CssClass="form-control"></asp:TextBox>
    </div>

    <div class="col-sm-1">
    <asp:LinkButton ID="btnadd" runat="server" Text="+" CssClass="btn btn-success" OnClick="btnadd_Click" ></asp:LinkButton>
    </div>
    <div class="col-sm-1" style="float:left">
    <%-- <asp:Button ID="rprdelete" runat="server" Text="-" CssClass="btn btn-danger left" />--%>
    <asp:LinkButton ID="btndel" runat="server" Text="-" CssClass="btn btn-danger left" OnClick="btndel_Click" ></asp:LinkButton>
    </div>

    </div>
    <br />
    </ItemTemplate>

    </asp:Repeater>
    </div> </form>
    </body>
    </html>

    ----------------------------------------------------------------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;


    namespace RepeaterControl
    {
    public partial class Test : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindRepeater();
    }
    }

    protected void BindRepeater()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("txtname", typeof(string));
    dt.Columns.Add("txtemail", typeof(string));
    dt.Columns.Add("txtmobile", typeof(string));
    DataRow dr = dt.NewRow();
    dr["txtname"] = string.Empty;
    dr["txtemail"] = string.Empty;
    dr["txtmobile"] = string.Empty;
    dt.Rows.Add(dr);
    ViewState["Curtbl"] = dt;
    Repeater1.DataSource = dt;
    Repeater1.DataBind();

    }

    private void SetOldData()
    {
    int rowIndex = 0;
    if (ViewState["Curtbl"] != null)
    {
    DataTable dt = (DataTable)ViewState["Curtbl"];
    if (dt.Rows.Count > 0)
    {
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    TextBox txtname = (TextBox)Repeater1.Items[rowIndex].FindControl("txtname");
    TextBox txtemail = (TextBox)Repeater1.Items[rowIndex].FindControl("txtemail");
    TextBox txtmobile = (TextBox)Repeater1.Items[rowIndex].FindControl("txtmobile");
    txtname.Text = dt.Rows[i]["txtname"].ToString();
    txtemail.Text = dt.Rows[i]["txtemail"].ToString();
    txtmobile.Text = dt.Rows[i]["txtmobile"].ToString();
    rowIndex++;
    }
    }
    }
    }

    protected void btnadd_Click(object sender, EventArgs e)

    {
    int rowIndex = 0;

    if (ViewState["Curtbl"] != null)
    {
    DataTable dt = (DataTable)ViewState["Curtbl"];
    DataRow drCurrentRow = null;
    if (dt.Rows.Count > 0)
    {
    for (int i = 1; i <= dt.Rows.Count; i++)
    {
    TextBox txtname = (TextBox)Repeater1.Items[rowIndex].FindControl("txtname");
    TextBox txtemail = (TextBox)Repeater1.Items[rowIndex].FindControl("txtemail");
    TextBox txtmobile = (TextBox)Repeater1.Items[rowIndex].FindControl("txtmobile");
    drCurrentRow = dt.NewRow();
    dt.Rows[i - 1]["txtname"] = txtname.Text;
    dt.Rows[i - 1]["txtemail"] = txtemail.Text;
    dt.Rows[i - 1]["txtmobile"] = txtmobile.Text;
    rowIndex++;
    }
    dt.Rows.Add(drCurrentRow);
    ViewState["Curtbl"] = dt;
    Repeater1.DataSource = dt;
    Repeater1.DataBind();
    }
    }
    else
    {
    Response.Write("ViewState Value is Null");
    }
    SetOldData();
    }


    protected void btndel_Click(object sender, EventArgs e)
    {
    int rowIndex = 0;
    int rowID = e.RowIndex;
    // lblTotal.Text = "0";
    if (ViewState["CurrTable"] != null)
    {
    DataTable dt = (DataTable)ViewState["Curtbl"];
    if (dt.Rows.Count > 0)
    {
    for (int i = 1; i <= Repeater1.Items.Count; i++)
    {
    TextBox txtname = (TextBox)Repeater1.Items[rowIndex].FindControl("txtname");
    TextBox txtemail = (TextBox)Repeater1.Items[rowIndex].FindControl("txtemail");
    TextBox txtmobile = (TextBox)Repeater1.Items[rowIndex].FindControl("txtmobile");
    dt.Rows[i - 1]["txtname"] = txtname.Text;
    dt.Rows[i - 1]["txtemail"] = txtemail.Text;
    dt.Rows[i - 1]["txtmobile"] = txtmobile.Text;
    rowIndex++;
    }

    if (e.RowIndex.ItemIndex < dt.Rows.Count)
    {
    dt.Rows.Remove(dt.Rows[rowID]);
    }
    }

    ViewState["Curtbl"] = dt;
    Repeater1.DataSource = dt;
    Repeater1.DataBind();
    }

    SetOldData();
    }
    }
    }

    -------------------------------------------

    I am stuck on this line - second line after int rowIndex=0;

    This line =    

     int rowID = e.RowIndex;

    Wednesday, February 26, 2020 5:45 AM

All replies

  • User-1716253493 posted
            LinkButton btndel = (LinkButton)sender;
            RepeaterItem item = (RepeaterItem)btndel.NamingContainer;
            int itemIdx = item.ItemIndex;

    Wednesday, February 26, 2020 7:40 AM
  • User-1330468790 posted

    Hi, careless,

    I have tried your code but felt a little confused. 

    Could you please tell me what the purpose you are trying to achieve? It would be helpful to modify the code and solve your problem precisely. 

    The problem of your deletion code is that "EventArgs e" does not contain "RowIndex" and any other value. 

    Generally, if you want to access the item(you can treat it as row) of the repeater, what you should do is through the event "OnItemCommand" in which you could find a very useful event argument "RepeaterCommandEventArgs".

    For example, here is an event handler, 

    "void Repeater_ItemCommand(Object Sender, RepeaterCommandEventArgs e)".

    In the argument "e", you could find four properties 

    1. e.CommandArgument: Arbitrary value which is bound to the Button/LinkButton inside each item of repeater control.
    2. e.CommandName: A string representing the command name which is bound to the Button/LinkButton inside each item
    3. e.CommandSource: The source of the command, i.e. the Button/LinkButton you use as the trigger of the command. 
    4. e.Item: The item/row of the repeater of which you put the Button/LinkButton in.

    Regarding your problem, I think you want to find the current row and delete it. I will focus on how to delete the current row.

    More details, you could find in below code in yellow background:

    .aspx Page:

    <head runat="server">
    
        <title></title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
        <style type="text/css">
            .auto-style1 {
                width: 100%;
            }
    
            .auto-style2 {
                width: 164px;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <HeaderTemplate>
                        <div class="row">
                            <div class="col-md-2">
                                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
                            </div>
                            <div class="col-md-2">
                                <asp:Label ID="Label2" runat="server" Text="Email"></asp:Label>
                            </div>
                            <div class="col-md-2">
                                <asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label>
                            </div>
                        </div>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <div class="row">
                            <div class="col-md-2">
                                <asp:TextBox ID="txtname" runat="server" CssClass="form-control"></asp:TextBox>
                            </div>
                            <div class="col-md-2">
                                <asp:TextBox ID="txtemail" runat="server" CssClass="form-control"></asp:TextBox>
                            </div>
                            <div class="col-md-2">
                                <asp:TextBox ID="txtmobile" runat="server" CssClass="form-control"></asp:TextBox>
                            </div>
    
                            <div class="col-sm-1">
                                <asp:LinkButton ID="btnadd" runat="server" Text="+" CssClass="btn btn-success" OnClick="btnadd_Click"></asp:LinkButton>
                            </div>
                            <div class="col-sm-1" style="float: left">
                               
                                <asp:LinkButton ID="btndel" CommandName="delete" runat="server" Text="-" CssClass="btn btn-danger left" ></asp:LinkButton>
                            </div>
    
                        </div>
                        <br />
                    </ItemTemplate>
    
                </asp:Repeater>
            </div>
        </form>
    </body>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
    
            {
                if (!IsPostBack)
                {
                    BindRepeater();
                }
            }
    
            protected void BindRepeater()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("txtname", typeof(string));
                dt.Columns.Add("txtemail", typeof(string));
                dt.Columns.Add("txtmobile", typeof(string));
                DataRow dr = dt.NewRow();
                dr["txtname"] = string.Empty;
                dr["txtemail"] = string.Empty;
                dr["txtmobile"] = string.Empty;
                dt.Rows.Add(dr);
                ViewState["Curtbl"] = dt;
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
    
            }
    
            private void SetOldData()
            {
                int rowIndex = 0;
                if (ViewState["Curtbl"] != null)
                {
                    DataTable dt = (DataTable)ViewState["Curtbl"];
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            TextBox txtname = (TextBox)Repeater1.Items[rowIndex].FindControl("txtname");
                            TextBox txtemail = (TextBox)Repeater1.Items[rowIndex].FindControl("txtemail");
                            TextBox txtmobile = (TextBox)Repeater1.Items[rowIndex].FindControl("txtmobile");
                            txtname.Text = dt.Rows[i]["txtname"].ToString();
                            txtemail.Text = dt.Rows[i]["txtemail"].ToString();
                            txtmobile.Text = dt.Rows[i]["txtmobile"].ToString();
                            rowIndex++;
                        }
                    }
                }
            }
    
            protected void btnadd_Click(object sender, EventArgs e)
    
            {
                int rowIndex = 0;
    
                if (ViewState["Curtbl"] != null)
                {
                    DataTable dt = (DataTable)ViewState["Curtbl"];
                    DataRow drCurrentRow = null;
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 1; i <= dt.Rows.Count; i++)
                        {
                            TextBox txtname = (TextBox)Repeater1.Items[rowIndex].FindControl("txtname");
                            TextBox txtemail = (TextBox)Repeater1.Items[rowIndex].FindControl("txtemail");
                            TextBox txtmobile = (TextBox)Repeater1.Items[rowIndex].FindControl("txtmobile");
                            drCurrentRow = dt.NewRow();
                            dt.Rows[i - 1]["txtname"] = txtname.Text;
                            dt.Rows[i - 1]["txtemail"] = txtemail.Text;
                            dt.Rows[i - 1]["txtmobile"] = txtmobile.Text;
                            rowIndex++;
                        }
                        dt.Rows.Add(drCurrentRow);
                        ViewState["Curtbl"] = dt;
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                }
                else
                {
                    Response.Write("ViewState Value is Null");
                }
                SetOldData();
            }
    
           
    
            protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
                if (e.CommandName == "delete")
                {
                    //if you bind the repeater with the dt, then the itemIdex should be corresponding to row index of the data table
                    int itemIndex = e.Item.ItemIndex;
                    if (ViewState["Curtbl"] != null){
                        DataTable dt = (DataTable)ViewState["Curtbl"];
                        dt.Rows.Remove(dt.Rows[itemIndex]);
                        ViewState["Curtbl"] = dt;
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                }
            }
    

    Demo:

    By the way, you can deal with the adding stuff in the same way. For example, assigning a command name like "add" to "btnadd" will make the click event triggering "Repeater1_ItemCommand" and you can check the command name of the argument "e"and do the following addition.

     

    Hope this can help you.

    Best regards,

    Sean

    Wednesday, February 26, 2020 9:33 AM