Displaying listbox control from another page ASP.Net VB.Net - asp.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)

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)

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.

How do I add a value to a TextBox that's inside an ASP:LoginView

I have some DropDownLists and some TextBoxes on a page. Normally I use this to clear and add value to the items:
Clear control:
txtEventNote.Text = String.Empty
ddlHours.SelectedIndex = 0
Add value:
ddlHours.SelectedValue = dtEvents.Rows(0)("EventDate")
txtEventTitle.Text = dtEvents.Rows(0)("EventHome").ToString()
But if I do this for items thats inside an ASP:LoginView I get an error because the ID's cant be found.
So my question is:
How do I clear the ddlSize DropDownList ID and how do i add String.Empty to txtEventName.Text that's inside the LoginView which has an ID of "loginview2"?
And how do I add a value to a TextBox and DropDownList inside an LoginView called "loginview2" where I have a TextBox with the ID textEventName.Text and the DropDownList have ID ddlSize.
If I delete the LoginView it's easy for me to do, but when I add the LoginView the ID's can't be found.
The LoginView control is a container control. It can hold other ASP.NET WebForm controls. To get a reference to the controls contained within the LoginView control use the FindContol method of the LoginView control.
TextBox tb = lcLoginView.FindControl("txtEventNote") as TextBox
if (tb != null)
{
tb.Text = "My New Value For This TextBox control";
}
Do the same for your DropDownList.
I found In Search Of ASP.Net Controls to be helpful for more about finding ASP.NET controls in other ASP.NET controls.

Losing <asp:Label> Text value from ViewState for dynamically added control

I am adding controls to a page programatically in the code behind. I add an asp:Label and set it's Text value. I add an asp:TextBox and set it's Text value. Both Text values are returned in the Response and displayed in the browser. All fine so far.
The user performs an action that causes a postback. I re-load the dynamically added asp:Label and asp:TextBox. When the Response is returned to the browser, only the asp:TextBox Text value is displayed. The asp:Label Text value is not.
If I inspect the HTML I can see the asp:Label control (rendered as an HTML span tag) but no value.
How can I get the code to automatically re-load the Text value of an asp:Label on each postback? Why is the behaviour different for an asp:Label and an asp:TextBox? I do not want to have to manually re-set the Text value on each postback.
Here is some code similar to what I am doing (placeHolderNameplates is an asp:PlaceHolder control on the aspx page):
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
Dim lbl As Label = New Label()
lbl.ID = "xxx1"
lbl.Text = "yo"
placeHolderNameplates.Controls.Add(lbl)
Dim tb As TextBox = New TextBox
tb.ID = "xxx2"
tb.Text = "yoyo"
placeHolderNameplates.Controls.Add(tb)
Else
Dim lbl As Label = New Label()
lbl.ID = "xxx1"
placeHolderNameplates.Controls.Add(lbl)
Dim tb As TextBox = New TextBox
tb.ID = "xxx2"
placeHolderNameplates.Controls.Add(tb)
End If
What you need to do is add the control to the placeholder before setting the values, so it should be
Dim lbl As Label = New Label()
placeHolderNameplates.Controls.Add(lbl)
lbl.ID = "xxx1"
lbl.Text = "yo"
See these posts for details:
http://www.yakkowarner.com/2008/01/aspnet-dynamic-controls-and-viewstate.html
http://codebetter.com/jefferypalermo/2004/11/25/key-to-ensuring-dynamic-asp-net-controls-save-viewstate-level-300/
Before they are added to the page, they have not initialized themselves. When a dynamic control is added to another control, the new control plays catch-up to get to the stage that the parent control is in. For instance, if in your Page_Load, you add a textbox, it will play catch-up and go through its Init and Load phases. This is important beceause it will start tracking its viewstate. Values added before it is tracking viewstate won’t make it to viewstate and will be lost on PostBack.
It seems like dynamically created controls won't be added to the ViewState automatically. The TextBox Control retains it's value however because of it's nature of being rendered to a <input type="text" value="xyz" /> html element.
Have a look at this article:
http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
Hey check thsi site MSDN
You have to add your control with following event( so viewstate maintain automaticly)
override protected void OnInit(EventArgs e)
example of Add Dynamic control
http://support.microsoft.com/kb/317794/en-us

Adding Checkbox in asp.net Listview control to allow multiple delete

I am trying to display checkboxes in front of every row in list view. So that after selecting the desired checkboxes user clicks on delete button and we should delete that records.
but how can it be done?
Add Checkbox in markup
<asp:CheckBox ID="ChkSelect" runat="server" />
code behind as follows:
Dim ChkSelect As CheckBox = Nothing
Dim ListItem As ListViewDataItem = Nothing
Dim ItemList As List(Of Person) = New List(Of Person)
Dim Item As Person= Nothing
For Each ListItem In MyDataList.Items
ChkSelect = ListItem.FindControl("ChkSelect")
If ChkSelect.Checked Then
Dim UIN As Integer = _
MyDataList.DataKeys(ListItem.DisplayIndex).Value.ToString()
Item = Persons.GetData(UIN)
Item.Deleted = True
ItemList.Add(Item)
End If
Next
Data = Persons.UpdateBulk(ItemList)
If Data = True Then
BindMyData()
End If
You need to create a template for the items in the ListView, put the checkbox in it, and then get all the items that were checked when you click the Delete button. You could either keep track of the selected items on the client or the server, but it would always require some work to persist them.
Here's an article on using templates with the ListView:
http://msdn.microsoft.com/en-us/library/bb398790.aspx#CreatingTemplatesForTheListViewControl
I use GridView Template if I want to do that in GridView...try to look if theres ListView Template if there is.

Resources