Wednesday, January 8, 2014

Get data from repeater using CommandName and CommandArgument

Hi everyone,
previously I gave an example of how to get data from a repeater using javascript. Today I am going to describe how to retreive data from repeater using CommandArgument and CommandName.

CommandName and CommandArgumet are two different properties of a ASP server button and using those two we are doing this example.

<asp:Button ID="btn_details" runat="server" Text="Show Id"   CommandName="edit" CommandArgument='<%# Eval("ContactID") %>'/>

Here in the button, which is within the repeater, we are binding the CommandArgumet with the ID of the data table. And when ever the button is clicking the value is passing and with that ID you can do your necessary work(edit, delete etc.).

<asp:Repeater ID="Repeater1" runat="server" 
            onitemcommand="Repeater1_ItemCommand">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>&nbsp;
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactID") %>'></asp:Label>
                <asp:Button ID="btn_details" runat="server" Text="Show Id"   CommandName="edit" CommandArgument='<%# Eval("ContactID") %>'/>   <%-- OnClientClick="Get_Product_Details(this);"--%>
                <asp:HiddenField ID="pid" runat="server" Value='<%# Eval("ContactID") %>'/>
                <br />
            </ItemTemplate>
        </asp:Repeater>

In repeater we have to add a new event named onItemCommand. And within that method we have to code to retrieve the ID or whatever value you want to pass.

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "edit")
                Response.Write("<script>alert('"+e.CommandArgument.ToString()+"');</script>");
        }  

Here I am only showing the ID in a alertbox, but its upto you how you want to use the data.

Full Code is here....

ASP Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get data from Repeater</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server" 
            onitemcommand="Repeater1_ItemCommand">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>&nbsp;
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactID") %>'></asp:Label>
                <asp:Button ID="btn_details" runat="server" Text="Show Id"   CommandName="edit" CommandArgument='<%# Eval("ContactID") %>'/>   
                <asp:HiddenField ID="pid" runat="server" Value='<%# Eval("ContactID") %>'/>
                <br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>


CS Code

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("ContactID", typeof(int));
                table.Columns.Add("ContactName", typeof(string));

                table.Rows.Add(20, "Abc");

                table.Rows.Add(30, "Xyz");
                table.Rows.Add(10, "Pqr");
                table.Rows.Add(25, "Mnop");
                table.Rows.Add(150, "Qrst");

                Repeater1.DataSource = table;

                Repeater1.DataBind();
            }

        }


        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)

        {
            if (e.CommandName == "edit")
                Response.Write("<script>alert('"+e.CommandArgument.ToString()+"');</script>");
        }    
    }

Download the full code here.

Download Now

0 comments:

Post a Comment

Popular Posts

Pageviews