Pass selected gridview value to detailsview template field in edit Mode - asp.net

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

Related

Find dynamically created control (TextBox) in div

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.

dropdownlist is always null in oneditcommand in datalist

I'm new to asp.net trying to use datalist control.
on edititemtemplate section i have put up a dropdownlist control. It has values in it. What I want is to select a value in edit mode but when I go to event (as set in oneditcommand ) my codes cannot find dropdownlist though its in edititemtemplate. Please check my codes below
DropDownList ddl = e.Item.FindControl("ddlType") as DropDownList;
ddl.Items.FindByValue((e.Item.FindControl("lblType") as Label).Text).Selected = true;
What I'm doing wrong?
thanks
-Navi
take a look at this link. Typically, you access controls like this at runtime by handling either the DataList's ItemCreated or ItemDataBound event.

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.

On postback lose value from dynamically created textbox gridview

I dynamically create gridview, and in this grid I have template field also
field.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
field.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName)
grdEmpty.Columns.Add(bfield)
but when enter some value in text box in this template field i lose value on postback. And also on postback I lose all template field and i must re-create this grid.
My goal is: I have button and i want to add new row in this grid, but i want to have old value also.
I struggle with this all day, and any help is welcome.
Tnx,
Just recreate the grid during each postback. If you have viewstate enabled in the page and the gridview then the property will be restored.
See: here for more explanation

Editing a row in a gridview

I have a two text boxes named region id and region name..and a button control
I enter some values into those text boxes and click the button to insert those values into the "gridview"and a "data table" associated with the gridview.
The gridview has the "enable editing" set to true..but when i click the "edit" button of a particular row in a gridview i get no response...i.e i do not get editable textboxes as it happens normally...
What is the solution for this?
You need to set the EditIndex of the row you are editting on the gdvMyGridView_RowEditing event:
gdvMyGridView.EditIndex = e.NewEditIndex
This will display your EditItemTemplate for the relevant row.
GridView Examples for ASP.NET 2.0: Editing the Underlying Data in a GridView
Editable GridView in ASP.NET 2.0
Edit control does not work out of blue. You have to write a backend SQL query for it to implement. The query will most likely look like this.
update regions SET name=#name where ID=#ID
where name and ID are fields of the table. Make sure the ID is the primary key, otherwise update will not work and you won't get any error.

Resources