Find dynamically created control (TextBox) in div - asp.net

I'm working on a page in asp.net/vb.net where most divs are created dynamically. Furthermore I'd like the user to edit the content with dynamically inserted HtmlTextAreas, TextBoxes, etc.
I've already managed to create all of this stuff. I insert every div into a panel and after the user clicks on the edit button the divs are reloaded and filled with the items (e.g. TextBoxes). For saving, I'm looping through each div and try to find the item. The only problem that I have right now is, that somehow I'm unable to find the dynamically inserted items.
For Each control As HtmlGenericControl In panel.Controls
'do something like this:
'Dim textBox As New TextBox = control.FindControl("myTextBox")
Next
I've already tried some options, but unfortunately without success (TextBox is always Nothing)
I'm using Masterpages, so I tried this:
Dim MainContent As ContentPlaceHolder = TryCast(Page.Master.FindControl("ContentPlaceHolder"), ContentPlaceHolder)
Dim textBox As TextBox = DirectCast("myTextBox" & intCount.ToString(), TextBox)
Is there any way to do this?
Thanks for your time and effort! :)

You need to be careful with your casting. When you have a collection of control objects you can't cast all of them to a textbox (ie a Label can't be converted and will throw an exception).
You can find controls using:
Dim textbox as TextBox = CType(control.FindControl("myTextBox"), TextBox)
To loop over all your textboxes you could use:
For Each textbox As TextBox In panel.Controls.OfType(of TextBox)
... interact here
Next
In this line
Dim textBox As TextBox = DirectCast("myTextBox" & intCount.ToString(), TextBox)
you are attempting to convert a string to a TextBox which wont work. You need to use FindControl to retrieve it.

Related

asp.net vb.net textbox should not allow to change if disable

Am working on asp.net with using vb.net i have a one form where is some disable textbox am assign the value on page load.user some time change it using f12 or some another way. So i don't want to change it any way if textbox is disabled.
My textbox name is txtUsername which is disable on Page load
My Code is
txtUsername.Enabled=false
'here amn getting value from database.
Dim strName as string ="Xyz"
but some user change the name "abc" and click on save then it update my database.
So i don't want to change it.
Thanks.

Pass selected gridview value to detailsview template field in edit Mode

DetailsView1 shows master detail from selected record of GridView1.
In edit mode, I have included a functionality for search, where on user click, GridView2 visibility is enabled.
I would like to have data from a selected row value (eg "accountnumber") from GridView2 loaded into a textbox control in DetailsView1 when I want to update "accountnumber".
How can I achieve this using VB.NET?
I put together a few ideas to come up with this, which worked perfectly well.
Dim TextBox As TextBox = TryCast(DetailsView1.FindControl("TextBox1"), TextBox)
TextBox.Text = GridView2.SelectedRow.Cells(0).Text

Can't set focus on TextBox inside detailsview

The DetailsView.FindControl("TextBox1") returns me the correct textbox where I want to set focus but setting focus using any of the following ways does not work
(TextBox)DetailsView.FindControl("TextBox1").Focus()
Page.SetFocus("TextBox1");
Got the Id of this textbox by Viewing page source and SetFocus on that.
Made sure that DetailsView is in Edit/Insert mode before focus is set.
None of the above has worked, I am stuck.
Note - Its works if DetailsView is inside Panel but not working where it is inside tabpanel
Instead of
(TextBox)DetailsView.FindControl("TextBox1").Focus();
Try
ScriptManager.RegisterStartupScript(this, this.GetType(), "SetFocus", "document.getElementById('" + (TextBox)DetailsView.FindControl("TextBox1").ClientID + "').focus();", true);
UPDATE
Here's another approach that uses ScriptManager:
TextBox textBox = (TextBox)DetailsView.FindControl("TextBox1");
ScriptManager.GetCurrent(this).SetFocus(textBox);

Pass datatable to different page to be diaplyed in gridview

I have an aspx page with a gridview control. The code-behind for this page fills a datatable and then displays the datatable in the gridview. I then need to pass the datatable to a separate aspx page and display it again in a gridview on THAT page. I'm not sure how to go about this.
On the second page, in the code-behind, I'll have a simple function that receives a datatable and displays it in a gridview, but what is the best way to do what I'm trying to do? I need the gridview to display on the original page, but also display on the new page.
So... somehow display the gridview on page1, then pop open the new page and pass the datatable to my function? At a loss here.
You can try using Sessions, once you display the GridView for the first time, just save the DataTable it loaded into a Session, something like Session["griddata"] = myDataTable. Then in the other page just pull it out of the Session likeDataTable myDataTable = (DataTable)Session["griddata"]; then bind it to the grid.

How to Reference TextBox in One UserControl from Another UserControl

I have two UserControls on a MasterPage. DataEntryUC contains several TextBoxes and DropDownList. NavSaveUC contains navigation buttons. When the user clicks on a navigation button, I will be saving the data entered into DataEntryUC from the NavSaveUC UserControl.
I have a couple of tables in my DB that contain stored procedure names, control names, control types, SqlDbTypes, etc.... that correlate with DataEntryUC.
How do I reference a text box that is on DataEntryUC from NavSaveUC?
I have been working on the following code from NavSaveUC with no luck.
Dim MyControlName = "txtFirstName"
Dim MyControlType = "TextBox"
Dim MyStringValue as String
Dim tmpTxtControl as TextBox
Dim tmpDdlControl as DropDownList
Select Case MyControlType
Case "TextBox"
tmpTxtControl = CType(Page.FindControl(MyControlName, TextBox)
MyStringValue = tmpTxtControl.Text
Case "DropDownList"
tmpDdlControl = CType(Page.FindControl(MyControlName, DropDownList)
MyStringValue = tmpDdlControl.SelectedValue
End Select
Thanks!
Expose the textbox as a property of the user control and access it that way. This type of dependency is not a best practice though.
If the one user control is embedded inside the other user control, then simply expose it as a property of the control. If, however, both user controls separately exist on the same page, the controls should not interact directly with each other to avoid a dependency between the controls.
Instead, you should use the page as an intermediary between the two controls. For instance, NavSaveUC can raise an event with data in it's event argument when an action is performed. The page can handle the event from NavSaveUC. If DataEntryUC exposes the text box or text box's text property as a public property, the page's event handler can get data from NavSaveUC's event argument and place it in DataEntryUC's text box.

Resources