Latest Entries »

Monday, August 23, 2010

# .netTiers. make data:repeater behave like asp:Repeater with OnCommand event

Hello. Today I would like to share with you with my expirience around strongly typed controls in nettiers.
  and 

Normally if developer want put control causing PostBack in Repeater should give CommandName , CommandArgument in PostBack Control and provide Repeater_OnCommand (or OnClick,OnCommand in child control) method. Badly data:Repeaters doesn't have onCommand Event and even postback in child control OnClick and OnCommand is not firing. Code below is nice but doesn't work:

<data:GblLanguageRepeater ID="nTiersGblRepeater" runat="server" DataSourceID="GblLanguageDataSource1">
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="LangFlagLb2" CommandName="GblLanguageId" CommandArgument='<%# Container.GblLanguageId %>'
            OnCommand="LangFlagLb_Command" OnClick="LangFlagLb_Click" >
            <asp:Image runat="server" ID="LangFlagImg2" ImageUrl='<%# Container.LanguageFlag %>'
                ToolTip='<%# Container.LanguageDescription %>' />
        </asp:LinkButton>
    </ItemTemplate>
</data:GblLanguageRepeater>
<data:GblLanguageDataSource ID="GblLanguageDataSource1" runat="server" SelectMethod="GetAll">
</data:GblLanguageDataSource>
Any method listed below won't fire.

//LinkButton OnCommand Event
    protected void LangFlagLb_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.ToString() == "GblLanguageId")
        {
            int GblLanguageId = int.Parse(e.CommandArgument.ToString());
            this.Profile.GblGroup.GblLanguageId = GblLanguageId;
            CurrentLangLit.Text = mgr.Data.DataRepository.GblLanguageProvider.GetByGblLanguageId(this.Profile.GblGroup.GblLanguageId).LanguageDescription;
        }
    }
//Repeater OnCommand Event
    protected void GblLangRep_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "GblLanguageId")
        {
            int GblLanguageId = int.Parse(e.CommandArgument.ToString());
            this.Profile.GblGroup.GblLanguageId = GblLanguageId;
            CurrentLangLit.Text = mgr.Data.DataRepository.GblLanguageProvider.GetByGblLanguageId(this.Profile.GblGroup.GblLanguageId).LanguageDescription;
        }
    }
//LinkButton Click Event
    protected void LangGlagLb_Click(object sender, EventArgs e)
    {
        LinkButton r = sender as LinkButton;
        if (r.CommandName.ToString() == "GblLanguageId")
        {
            int GblLanguageId = int.Parse(r.CommandArgument.ToString());
            this.Profile.GblGroup.GblLanguageId = GblLanguageId;
            CurrentLangLit.Text = mgr.Data.DataRepository.GblLanguageProvider.GetByGblLanguageId(this.Profile.GblGroup.GblLanguageId).LanguageDescription;
        }
    }

Code that works.

First thing about is change data:GblRepeater into normal asp:Repeater control and secondly we will put at the top of control directive to import Container Item namespace.

<%@ Import Namespace="mgr.Entities" %>

And change CommandArgumend,CommandName,Tooltip:

<asp:Repeater ID="GblLangRep" runat="server" DataSourceID="GblLangDs" OnCommand="GblLangRep_ItemCommand">
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="LangFlagLb" CommandName="GblLanguageId" CommandArgument='<%# (Container.DataItem as GblLanguage).GblLanguageId %>'
            OnCommand="LangFlagLb_Command">
            <asp:Image runat="server" ID="LangFlagImg" ImageUrl='<%# (Container.DataItem as GblLanguage).LanguageFlag %>'
                ToolTip='<%# (Container.DataItem as GblLanguage).LanguageDescription %>' />
        </asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>
<data:GblLanguageDataSource runat="server" ID="GblLangDs" SelectMethod="GetAll" />


Now you can do whatever you want in OnCommand event in Repeater or OnClick(OnCommand) Event in LinkButton. After some reaserch in google I found that next version of nettiers should fix the problem with PostBack Events.