General Function

74 Views Asked by At

in a project I am working on I have TabContainer (AJAX.NET) have many tabPanels all of them are doing the same function BUT each on on a different Table

let me give a sample :

    <asp:TabContainer ID="TabContainer3" runat="server" ActiveTabIndex="0" BorderStyle="None"
    BorderWidth="0" CssClass="MyTabStyle" Width="625px">
    <asp:TabPanel ID="TabPanel1" runat="server">
        <HeaderTemplate>
            Tab_x
        </HeaderTemplate>
        <ContentTemplate>
            <asp:TextBox ID="txt_x" runat="server"></asp:TextBox>
            <asp:Button ID="btnx" runat="server" Text="Button" />                
        </ContentTemplate>
    </asp:TabPanel>
    <asp:TabPanel ID="TabPanel2" runat="server">
        <HeaderTemplate>
            Tab_y
        </HeaderTemplate>
        <ContentTemplate>
            <asp:TextBox ID="txt_y" runat="server"></asp:TextBox>
            <asp:Button ID="btny" runat="server" Text="Button" />                
        </ContentTemplate>
    </asp:TabPanel>
</asp:TabContainer>

Code behind (VB.NET)

Protected Sub btnx_Click(sender As Object, e As System.EventArgs) Handles btnx.Click
    SaveText_x(txt_x.Text)
End Sub

Protected Sub btny_Click(sender As Object, e As System.EventArgs) Handles btny.Click
    SaveText_y(txt_y.Text)
End Sub

is there a way to create general Sub or Function so if I clicked btnx function Save_x(txt_x.Text) be called

and when I click btny function Save_y(txt_y.Text) be called ?

2

There are 2 best solutions below

0
Jignesh Suvariya On

1)Made user control with public string property. use view state to store value of that property.

2)Add that user control into your tabs set that property with values like "X" or "Y" or any.

3)On button click check that property with if .. else if .. else or by switch statement and call your SaveText functions variants.

2
Nunners On

You can assign multiple buttons to have the same click handler with the following code :

Protected Sub btn_Click(sender As Object, e As System.EventArgs) Handles btnx.Click, btny.Click
    Dim btn As Button = CType(sender, Button)

    If btn.ID = "btnx" Then
        SaveText_x(txt_x.Text)
    ElseIf btn.ID = "btny" Then
        SaveText_y(txt_y.Text)
    End If
End Sub

Both btnx and btny will both fire this Sub and it will check the button that sent it to see which method to call.