Archive for the ‘Event’ Category

Hello All

Today I will show you how to fire  Link Button’s event with argument and initialize a label inside of a repeater. To do that first we have to take LinkButton and a Label control inside the ItemTemplate of the repeater. Then we have to initialize the the Label from OnItemDataBound event of the repeater. In the following code blocks its shown how to perform the job.

Following is my Default.aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestProject._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Selected ID:<asp:Label ID="lblSelectedID" runat="server" Text=""></asp:Label>
        <br />
        <asp:Repeater ID="rptContent" runat="server" OnItemDataBound="rptContent_ItemDataBound">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server">
                </asp:Label>
                <asp:LinkButton ID="lnkShowData" runat="server" OnCommand="LinkButton_Command" CommandArgument='<%# Eval("key") %>'
                    Text="[Show]" CommandName="Show"></asp:LinkButton>
                <br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

In the above code I have bind the "key" as CommandArgument in LinkButton which will be initialize form the code behind file.

My code behind is as following:

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

namespace TestProject
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            rptContent.DataSource = this.userInformation();
            rptContent.DataBind();
        }

       // Fire the link button event from inside repeater.
       protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
           lblSelectedID.Text = e.CommandArgument.ToString();
        }

        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // find the label control by id
            Label lblDynaLabel = (Label)(e.Item.FindControl("lblName"));

            // bind the data from repeater data source (value from the dictionary)with the label control.
            lblDynaLabel.Text = DataBinder.Eval(e.Item.DataItem,"value").ToString();
        }

        #region Private method

        private Dictionary<int, string> userInformation()
        {
            Dictionary<int, string> userData = new Dictionary<int, string>();

            userData.Add(1, "Max");
            userData.Add(2, "Top");
            userData.Add(3, "Avg");

            return userData;
        }
        #endregion
    }
}

The output will be as bellow:

Selected ID:1
Max [Show]
Top [Show]
Avg [Show]

In the private method section I have created a simple mathod named userInformation, which contains dictionary data collection to bind it with repeater.Hope that above code blocks will help.

Thanks

That’s all for the day.

BYE

User ScrumPad for your Agile based projects.

Hello All,

Today I will try to deliberate my experience on custom event creation in .NET , Actually I was trying to find something like hands on for event creation in .NET application, suddenly I got a quick help from one of my brother Sadique, who helped me out to resolve the thirst.

We are going to create a custom control where we will define our event class and event handler.To do that first take a  user control named InputControl.ascx  . The ascx page is as bellow.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InputControl.ascx.cs" Inherits="InputControl" %>
<asp:Label ID="lblLabel" runat="server" Text=""></asp:Label>
    <br />
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox>
    <br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
    onclick="btnSubmit_Click" />

In that page I put a textbox control to take input , a button control to fire our custom event handler and a label to show our input result through our custom event.

The InputControl.ascx.cs code behind file is as bellow :

using System;

public partial class InputControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string EventInputBox
    {
        get {
                return this.txtTextBox.Text;
            }
        set {
                this.txtTextBox.Text = value;
            }
    }

    public string EventLabel {
        get {
                 return this.lblLabel.Text;
            }
        set {
                 this.lblLabel.Text = value;
            }
    }

    public event EventHandler<CustomEventArgs> CustomEventSubmit;
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this.CustomEventSubmit != null)
            this.CustomEventSubmit(this, new CustomEventArgs("your text is:" + EventInputBox));
    }

}

public class CustomEventArgs : EventArgs
{
    public string EventText { set; get; }

    public CustomEventArgs(string eventTextData)
    {
        EventText = eventTextData;

        /*
         Perform other event tasks.

         */
    }
}

Now I register the control in my Default.aspx page as bellow .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="InputControl.ascx" tagname="InputControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:InputControl ID="icEventBox" runat="server" OnCustomEventSubmit="icEventBox_CustomEventSubmit"  />
    </div>
    </form>
</body>
</html>

In the file above we have to focus on OnCustomEventSubmit="icEventBox_CustomEventSubmit" which is pointing our custom event.

The code behind file Default.aspx.cs is as bellow.

using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void icEventBox_CustomEventSubmit(object sender, CustomEventArgs e)
    {
        icEventBox.EventLabel = e.EventText;
    }
}

In the above article I didn’t go for much brief discussion , I think the code is sufficient to describe a custom event itself.

That’s all for the day.

BYE

User ScrumPad for your Agile based projects.