cancel
Showing results for 
Search instead for 
Did you mean: 

Creating TabStrip and Tabs via Code

Former Member
0 Kudos

Hello Experts,

I am trying to add Tabstip Control dynamically.Let me tell you I am using this

URL:-https://www.sdn.sap.com/irj/sdn/dotnet?rid=/lw/uuid/95628888-0d01-0010-7d97-f0d771999d0f

The Tabs and Tab strip controls get added that was fine.

But my problem is I am unable to handle Tab strip SelectedTab event.And unable to set default Tab when control is created.

I also tried T1strip.Select += new TabStrip.SelectEventHandler(T1strip_Select); in page load.

void T1strip_Select(object sender, TabStrip.SelectEventArgs e)

{

this.write(e.Tab);

}

Any help appreciated?

Sunil Pawar

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Sunil,

how are you doing ?

a dynamic tab strip is like any other dynamic control

please remember to add it to the viewstate, to be able to retain it's values, my guess is that the handler is created but lost due to a non saving activity on postback,as u're code seems fine

please review the following link for dynamic control programming :

http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

with respect,

amit

Former Member
0 Kudos

Hi Amit,

Thanks for your reply.

The link you provided is good to understand page life cycle.

But I am thinking there is problem with only Tab Strip control.Because I tried another control like "button"(Create it dynamically and handle the click event).It was fine.But Tab Strip Control is not behaving as I suppose to.

I am posting code below.

-


override protected void OnInit(EventArgs e)

{

//Tab1 constructor

Tab T1tab = new Tab();

Caption C1caption = new Caption("Tab1");

T1tab.Header = C1caption;

//Create TabContent1 Template

TabContent T1content = new TabContent();

SAP.Web.UI.Controls.Label Label1 = new SAP.Web.UI.Controls.Label();

InputField Input1 = new InputField();

//Add controls to TabContent1 Template

Input1.ID = "I1";

Label1.Text = "Text1";

T1content.Controls.Add(Label1);

T1content.Controls.Add(Input1);

T1tab.TabContent = T1content;

TabStrip1.Tabs.Add(T1tab);

//Tab2 constructor

Tab T2tab = new Tab();

Caption C2caption = new Caption("Tab2");

T2tab.Header = C2caption;

//Create TabContent1 Template

TabContent T2content = new TabContent();

SAP.Web.UI.Controls.Label Label2 = new SAP.Web.UI.Controls.Label();

//Add controls to TabContent1 Template

Label2.Text = "Text2";

T2content.Controls.Add(Label2);

T2tab.TabContent = T2content;

TabStrip1.Tabs.Add(T2tab);

//Add TabStrip control to the page

TabStrip1.ID = "T1";

TabStrip1.Width = "350";

TabStrip1.Height = "150";

TabStrip1.SelectedTab = ("Tab2"); <b>/* please also check this line*/</b>

this.Controls.Add(TabStrip1);

TabStrip1.Select+=new TabStrip.SelectEventHandler(TabStrip1_Select);

}

-


void TabStrip1_Select(object sender, TabStrip.SelectEventArgs e)

{

this.Write(e.Tab);

}

*********************************************************

The problem is that above code did not set Tab I wish also not give any value in Select event(e.Tab is blank).

Please help me if any one knows why this happens.

Thanks in advance

Sunil Pawar

rima-sirich
Advisor
Advisor
0 Kudos

Hi Sunil,

I believe that this problem caused by the fact that you didn't set T1tab.ID and T2tab.ID , therefore e.Tab is empty.

Following code works for me:

protected void Page_Load(object sender, EventArgs e)

{

//Create object for tabstrip control

TabStrip T1strip = new TabStrip();

//Tab1 constructor

Tab T1tab = new Tab();

T1tab.ID = "Tab1"; // ***** add this line *****

Caption C1caption = new Caption("Tab1");

T1tab.Header = C1caption;

//Create TabContent1 Template

TabContent T1content = new TabContent();

SAP.Web.UI.Controls.Label Label1 = new SAP.Web.UI.Controls.Label();

InputField Input1 = new InputField();

//Add controls to TabContent1 Template

Input1.ID = "I1";

Label1.Text = "Text1";

T1content.Controls.Add(Label1);

T1content.Controls.Add(Input1);

T1tab.TabContent = T1content;

T1strip.Tabs.Add(T1tab);

//Tab2 constructor

Tab T2tab = new Tab();

T2tab.ID = "Tab2"; // ***** add this line *****

Caption C2caption = new Caption("Tab2");

T2tab.Header = C2caption;

//Create TabContent1 Template

TabContent T2content = new TabContent();

SAP.Web.UI.Controls.Label Label2 = new SAP.Web.UI.Controls.Label();

//Add controls to TabContent1 Template

Label2.Text = "Text2";

T2content.Controls.Add(Label2);

T2tab.TabContent = T2content;

T1strip.Tabs.Add(T2tab);

//Add TabStrip control to the page

T1strip.ID = "T1";

T1strip.Width = "350";

T1strip.Height = "150";

T1strip.Select += new TabStrip.SelectEventHandler(T1strip_Select);

this.Controls.Add(T1strip);

}

public void T1strip_Select(object sender, TabStrip.SelectEventArgs e)

{

Write(e.Tab);

}

Regards,

Rima.

Former Member
0 Kudos

Hi Sunil,

how are you doing today ?

a few points about u're code (which are consistent with Rima's code)

1> without assigining the ID, it will not be possible for you to refer to that particular Tab after rendering [you could estimate it, but that is not a healthy programming practice )

you can dynamically create the Tab without assigining it the Id, it will get a randomly generated Id, but your code will not be able to access it unless it knows the Id]

2> you need to add the even handler before you add the control to the page

[or at least it is maintainable and a good programming practice]

3>The SelectedTab property would need either an index (0 based) or an Id

TabStrip1.SelectedTab = "myTab02";

or

TabStrip1.SelectedTab = 1;

if the index / id is not valid, it would throw an exception, so add an exception handler for dynamic controls always**

4> to have a blank tab

simply create a new one and add it to the tab strip and say

TabStrip1.SelectedTab = 0;

by default it should be zero anyways for dynamic controls

but adding the code is a better idea

with respect,

amit

Former Member
0 Kudos

Hi Rima,

Your solution is <b>perfect</b>.

I miss those IDs of Tabs.

But let me tell you those are also not present in sdn help.

Finally your code sample helps me a lot.

Thank You Very Much

Regards

Sunil Pawar

Former Member
0 Kudos

Hello Amit.

Thanks for your contribution too.

Regards

Sunil Pawar

Message was edited by:

Sunil Pawar

Answers (0)