AJAXControlToolkit TabContainer-How to add and remove tab dynamically - asp.net

I am using AjaxControlToolkit version 4.1, .NET 4.0, VS2010,language-c#
Using the TabContainer control I want to add/remove tabs dynamically. Suppose I have some .aspx page. In 1st tab I have some hyperlink . While I will click on the hyperlink the corresponding .aspx page will open in a tab dynamically and also I can remove it.How to do it? Can anyone help me?

Create
Create TabContainer
Dim tbContainer As New AjaxControlToolkit.TabContainer
tbContainer.ID = "TabContainer1"
Create TabContent
Dim tbContent As New Control
tbContent.ID = "tbContent_" & dt.Rows(i).Item("PRENR").ToString
Create TabPanel
Dim tbPanel As New AjaxControlToolkit.TabPanel
tbPanel.ID = "tbPanel_" & dt.Rows(i).Item("PRENR").ToString
tbPanel.HeaderText = "PREMIUM AMOUNT"
Adding the controls to Page
tbContent.Controls.Add(dvEarner1)
tbPanel.Controls.Add(tbContent)
tbContainer.Tabs.Add(tbPanel)
tbContainer.Tabs.Add(tbPanel1)
tbContainer.Tabs.Add(tbPanel2)
tbContainer.Tabs.Add(tbPanel3)
tbContainer.Tabs.Add(tbPanel4)
tbContainer.Tabs.Add(tbPanel5)
pane.ContentContainer.Controls.Add(tbContainer)
Me.MainAccordion1.Panes.Add(pane)
Remove
Me.TabContainer1.Tabs.Remove(tab)

Related

Handling dynamic controls from master page

I have a asp:placeholder in master page. From content page (in page_init) I am dynamically creating drop downs and adding to the asp: placeholder in master page. How do I access these dynamically created controls by id?
I tried this
Dim Outer_CP As ContentPlaceHolder Outer_CP = TryCast(Me.Master.Master.FindControl("MyContent"), ContentPlaceHolder)
Dim mydropdownsAs PlaceHolder = TryCast(Outer_CP.FindControl("mydropdownsAs "), PlaceHolder)
Dim ddlControl As DropDownList = CType(mydropdownsAs .FindControl(idName), DropDownList)

How to add controls to ReportViewer?

I want to create report with my own controls, But the controls don't appear in the report!
I tried this:
GridView1.DataSource=(Order.GetDailyReport());
GridView1.DataBind();
ReportViewer1.Controls.Add(GridView1);
But nothing appeared!
Read this documentation
Customize the Report Viewer Web Part
But why would you want to add a gridView anyway? it is more meant for controls like the buttons you see on the toolbar.
I think you are trying to build it dynamically because the reportviewer comes with its own table.
You just need to change the way you populate the datasource:
Here is an example of how you would achieve that:
DataTable dt = Order.GetDailyReport();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
No need for gridview

Dynamically adding Textbox in asp.net using C#

I have written a code in which a textbox is dynamically added to a gridview cell. There is some default texts in the textbox. I want that when users will click on the textbox the default text will disappear and the user can then write anything on it only in number, i.e. the user will not be able to use letters or special characters.
Kindly let me know how to achieve this.
Code example
Gridview gv=new Gridview();
gv.DataSource=dt;
gv.DataBind();
Textbox t1 = new Textbox();
t1.Text="Outages if any(in mins)";
gv.Rows[0].Cells[0].Controls.Add(t1);
Need help after this, something like when user puts his cursor in the textbox , the default text will disappear , and if the user removes the cursor without writing anything , the default text will reappear. Also the default text should be a bit blurred
Thanks.
Try something like this
Textbox t1 = new Textbox();
t1.Attributes.Add("onclick", "if(this.value == 'default text') this.value = '';"
t1.Attributes.Add("onblur", "if(this.value == '') this.value = 'default text';" />
You could also use onfocus in case users use tab key
With this, using this.value, approach you don't need to know the client ID of the control.
Here is a post describing exactly what you are looking to do:
HowTo: including default text in a Textbox while enforcing server-side validation
The pertinent points are:
Adding javascript attribute to onfocus & and onblur:
txtName.Attributes.Add("onfocus","clearText()");
txtName.Attributes.Add("onblur","resetText()");
Adding the javascript to clear and repopulate the textbox:
function clearText() {
document.form1.txtName.value = ""
}
function resetText() {
if(document.form1.txtName.value == "")
document.form1.txtName.value = "(enter something here)"
You can do this. The easiest way is to use the ajaxControlToolkit. You can create controls dynamically. For example:
Dim mt As new TextBox
Dim newTest As New AjaxControlToolkit.TextBoxWatermarkExtender
With mt
.ID = "textBox1"
.TextMode = TextBoxMode.SingleLine
End With
With newTest
.ID = "TextBoxWatermarkExtender1"
.TargetControlID = mt.ClientID
.WatermarkText = "test"
End With
Then just add both controls to the gridview as you are doing with the textbox. If you are not using Ajax, you can add javascript to the control through codebehind but this is more difficult. Let me know if that is what you want to do and Ill add some code to show you how.
This should create a textbox control with an associated ajax TextBoxWaterMarkExtender.

Displaying listbox control from another page ASP.Net VB.Net

I need to display the values from a listbox on a content page in a textbox on a masterpage in ASP.Net using VB.Net
Thanks in advance.
You can try this
Dim txt As Textbox = DirectCast(Master.FindControl("yourTextbox"), Textbox)
txt.text = "your Value here"
So you should run this code on your content page.
I assume your textbox on the masterpage is not in a updatepanel or panel etc. then you need to refer that first. but if not, this should work....
Dim tb As Textbox = DirectCast(Master.FindControl("theNameofYourTextBox"), Textbox)
tb.Text = ListBox.Item.Value (or the equivalent for getting the text value in the listbox)

Asp.net Dynamic link Button not raising event

I have a tabular data, in which at last column of every row a dynamic link button is added.
LinkButton link = new LinkButton();
link.Text = "Edit";
link.ID = dt.Rows[dt.Rows.IndexOf(dtRow)][0].ToString() + "|" + dt.Rows[dt.Rows.IndexOf(dtRow)][1].ToString();
link.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
cell.Controls.Add(link);
link.Click += new EventHandler(EditClicked);
The edit link is shown and on click it does the post back also But the event EditClicked is not fired at all.
Your problem is that you're dynamically creating your LinkButton and not recreating it again when your page is loaded.
If you dynamically create a control and then at postback, you don't create it again (in the Page_Load or preferably in the Page_Init) the event will not be fired.
One way to solve this is by using a hidden field:
When you dynamically create the linkbuttons, set a special value to a hidden field.
Then, in the Page_Load (in the if (IsPostback) ) check the hidden field, and if it has the special value - recreate all those controls again.

Resources