0
**ASPX CODE** this is the aspx code
<% for (int i = 0; i < pricePageObj.cartItemsgetset; i++)
                { %>
            <div class="row">
                <div class="col-md-1 ">
                    <% Response.Write(i + 1); %>
                </div>
                <div class="col-md-2 ">
                    <% Response.Write(svcID[i]); %>
                </div>
                <div class="col-md-2 ">
                    <% Response.Write(svcName[i]); %>
                </div>
                <div class="col-md-3 ">
                    <% Response.Write(svcDesc[i]); %>
                </div>
                <div class="col-md-2 ">
                    <% Response.Write(svcCharges[i]); %>
                </div>
                <div class="col-md-1 ">
                    <asp:Button CssClass="btn btn-danger" runat="server" Text="Remove"  CommandArgument="dont know how to pass i here" OnCommand="onRemove" />
                </div>
            </div>
            <%} %>

Let's say i=0 ; i<5; i++. so for loop will go for 5 times. so i want to send i as a parameter in the code behind to read the value of i. C# CODE code behind

public void onRemove(object sender, CommandEventArgs e)
    {
        try
        {
            string indexx = Convert.ToString(e.CommandArgument);
            System.Diagnostics.Debug.WriteLine("String index = " + indexx);
        }
        catch (Exception ex)
        {

        }


    }

I want to send that 'i' index in the code behind. Any help?
When i clicked on the button, i want to read that i in the onRemove event. any help?

19
  • you mean send it from code behind? Commented Jul 19, 2017 at 16:10
  • No i want to send that in the code behind. i want to get that index in the c# function onRemove. Commented Jul 19, 2017 at 16:12
  • do you want to pass svcCharges[i] or just the 'i' to the code behind? Commented Jul 19, 2017 at 16:14
  • @AmrElgarhy Just the 'i'. Commented Jul 19, 2017 at 16:17
  • 2
    Normally, you could assign value to CommandArgument. How do you assign that value? Please tell us what you are trying to achieve over all instead of what is not working. Commented Jul 19, 2017 at 16:27

2 Answers 2

1

Here is how we normally display a collection inside Bootstrap divs in ASP.NET Web Form. If we want to manipulate individual row, we use ItemDataBound event.

It seems a lot of code compare to your original question, but this is a recommended approach in ASP.NET Web Form.

enter image description here

<asp:ListView runat="server" ID="ListView1" 
    ItemPlaceholderID="ItemPlaceholder"
    OnItemDataBound="ListView_ItemDataBound">
    <LayoutTemplate>
        <asp:Panel runat="server" ID="ItemPlaceholder"></asp:Panel>
        <asp:DataPager runat="server" ID="DataPager" PageSize="3">
            <Fields>
                <asp:NumericPagerField ButtonCount="5" PreviousPageText="<" NextPageText=">" />
            </Fields>
        </asp:DataPager>
    </LayoutTemplate>        
    <ItemTemplate>
        <div class="row">
            <div class="col-md-1">
                <%# Eval("Id") %>
            </div>
            <div class="col-md-2">
                <%# Eval("Name") %>
            </div>
            <div class="col-md-1">
                <asp:Button CssClass="btn btn-danger" runat="server" Text="Remove" 
                    CommandArgument='<%# Eval("Id") %>' ID="RemoveButton" 
                    OnCommand="RemoveButton_Command" />
            </div>
        </div>
    </ItemTemplate>
</asp:ListView>

Code Behind

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public partial class About : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListView1.DataSource = new List<User>
            {
                new User {Id = 1, Name = "John"},
                new User {Id = 2, Name = "Marry"},
                new User {Id = 3, Name = "Nancy"},
                new User {Id = 4, Name = "Eric"},
            };
            ListView1.DataBind();
        }
    }

    protected void RemoveButton_Command(object sender, CommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);

    }

    protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            User user = e.Item.DataItem as User;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help.
0
<% Response.Write(i + 1); %>

By doing this, you are passing i to the Response.Write method.

You should be able to just write a method in your code behind.

Code behind sample method:

protected int Foo(int bar){
    //do something with bar

    return bar;
}

You should be able to do this on the .aspx page now:

<% Response.Write(Foo(1) + 1); %>

You can access all of the methods in your code behind by putting the below line at the top of your page, replacing MyCoolPage with your page name. This line should be added for you automagically, though, when you create a web form page:

<%@ Page CodeBehind="MyCoolPage.aspx.cs" Inherits="MyCoolPage" Language="C#" %>

Instead of doing what you are doing with your button, just add an OnClick event instead of what you are doing now, as follows:

<asp:Button CssClass="btn btn-danger" runat="server" Text="Remove" OnClick="DoSomethingCool(i) />

DoSomethingCool:

protected void DoSomethingCool(int index){
    try
    {
        System.Diagnostics.Debug.WriteLine("index = " + 
         index);
    }
    catch (Exception ex)
    {

    }
}

1 Comment

Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.