I have an EmptyDataTemplate in my asp:ListView which I want to use to insert a new record.
I have Inserting working in the InsertItemTemplate... I thought I could copy the InsertItemTemplate into the EmptyDataTemplate, on clicking Insert this gives the error
Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert.
How can I use the EmptyDataTemplate to Insert a row? Do I need to use the OnClick of the button to access the values within the EmptyDataTemplate and do an Insert by myself?
I'm using LinqDataSource
You might have figured it by now
but if you set the InsertItemPosition to anything other than None the EmptyData Template will not be rendered i.e it will always show the insert template
you can read more here
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.emptydatatemplate.aspx
No way if want to insert data in empty data template.
It is possible to do an insert from the EmptyDataTemplate by handcrafting the insert. I am using a listview to display a static number of rows based on a unique filtered item. I am basically listing all the static attributes of an object. In the case where a new object is filtered on that does not have any attributes associated with it, i use the EmptyDataTemplate of the listview to display a HTMLTable that contains asp.net controls to capture data. I have a command button within the table that i evaluate using the ListView_ItemCommand. If the CommandName matches that of the "Insert" button within the EmptyDataItem, I use the ListView.Controls(0).FindControl method to locate my table. I then loop through my table and do inserts on the data found within each row. I included the how to find a control within the htmltable. In my code I am actually grabbing a bunch of controls then crafting the sql and using a SQLConnection to perform the insert.
Protected Sub ListView_ItemCommand(sender As Object, e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView.ItemCommand
Select Case e.CommandName
Case "Submit"
Dim edt As HtmlTable = ListView.Controls(0).FindControl("myhtmltable")
Dim ddl As DropDownList = CType(edt.FindControl("mydropdownlist"), DropDownList)
'Perform Insert
Case "Some other commandname"
End Select
End Sub
You will need to still do error checking and databind() and refresh your listview.
Is this the best way. Maybe not... But it is possible.
~Ian
Related
I have a big DetailsView form for an insert to a database and I have a lot (10+) checkbox fields. Many of them aren't visible because we don't want those fields activated yet to have data submitted (but may be activated in the future).
How I can set the default value of all checkboxes at first to "false" (unchecked)? As of right now, they are being inserted as nulls and that doesn't play too well with everything else.
So far I have this code behind running as an onDataBound event for the DetailsView:
Dim row As DetailsViewRow
For Each row In DetailsView1A.Rows
If row.Cells.GetType() = GetType(CheckBox) Then
Dim tempCheckbox As CheckBox = CType(row.Cells(1).Controls(0), CheckBox)
If tempCheckbox Is DBNull.Value Then
tempCheckbox.Checked = False
End If
End If
Next
However, all the checkboxes are still being submitted as nulls. What am I doing wrong?
Try handling the ItemInserting event, and updating the "Values" collection of the DetailsViewInsertEventArgs object. These are the values that will be passed to your datasource's insert statement.
Go through and update the "Values" entry for each of these hidden CheckBox columns to False. Here's is an example:
Sub CustomerDetail_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
e.Values("yourFieldName") = False
End Sub
Note that "yourFieldName" will be the name of the column in the datasource to which the DetailsView is connected.
Also, False might need to be a string, I'm not sure how this actually gets passed:
e.Values("yourFieldName") = "False"
One other thought: if the above doesn't work (for some reason those hidden CheckBoxes don't have entries in the Values collection), I would just not hide them on the server side - hide them using CSS or JavaScript. In other words, instead of having Visible="False" on your CheckBox, use the CSS property display:none.
I have tried everything to no avail. I need to cast the value from a user selected dropdownlist to another dropdownlist on another page. The data in the textboxes are a series of numbers.
Also is there a way to display the value in the second dropdown list as well as other values? For example, my dropdown has the values 1-6, user selects 4, which displays first in the dropdown on page 2 but the other values also display in case they want to change there selection??
If Not Page.PreviousPage Is Nothing Then
Dim table As Control = PreviousPage.Controls(0).FindControl("table1")
Dim ddl As DropDownList = CType(table.FindControl("ddlB_Codes"),c DropDownList)
If Not ddl Is Nothing Then
ddl_OC.DataSource = ddl.SelectedValue.Substring(0, 6)
End If
End If
Quick update I FINALLY got my casting to work through a session, only now I need to know how to display the session values and the other additional values for my drop box in case the user wants to change something? Thanks for the help
ddl_OC.DataSource = CType(Session.Item("valCodes"), String)
ddl_OC.DataBind()
ddl_SO.DataSource = CType(Session.Item("valAccts"), String)
ddl_SO.DataBind()
Use event selectedvaluechanged to set datasource for another DropDownList, when first one is changed.
Don't forget to populate it vin DataBind function usage.
What I am trying to do is dynamically create a bunch of dropdown lists and then I want to loop through them and update the database with those values, but the problem I am facing is that I create the dropdown lists, but then when I go back to loop through them they are no longer in the panel. I don't know why but when I am debugging I have a count of 50 controls in pnlTeacherSelect right up until I press the button that calls prcChoose.
Called on Page Load
Sub prcSetTeachers()
For Each Subject In ds.Tables("Subject").Rows
Dim Temp As New DropDownList
pnlTeacherSelect.Controls.Add(Temp)
Temp.ID = "drp" & Subject.Item(0) & "s" & Child.Item(0)
Next
End Sub
Called on Clicking a button
Sub prcChoose()
For Each DropDownList In pnlTeacherSelect.Controls.OfType(Of DropDownList)
'This is never executed
Next
End Sub
Any ideas what's causing it? Thanks in advance!
You have to recreate all dynamically created controls on every postback(in load event at the latest).
You also have to ensure that they get the same ID as before to trigger events and maintain ViewState.
If you know the number of controls to create(which could be stored in ViewState) you can derive the ID from the counter variable by appending it to the control-id. Then you can recreate them with the correct ID in page's init event.
Recommandable readings:
TRULY Understanding Dynamic Controls
Page-Lifecycle
Or you use one of the builtin Data-Bound Control like Repeater that do this automatically. You only have to set their DataSource and call DataBind().
Here are answers of me on similar questions with implementations:
C#
VB.NET (+ C#)
I have a rad grid, i have bounded the columns in the grid using GripBoundColumns which shows me dropdown cloumns when i edit the record, Insert/update/delete are working fine for me.
My question is it possible to insert new data into the column(Not the whole record just only to one column) when i edit the record.
For example...
suppose i have 5 columns (Client Name, Account No, Account name, account status, Custodian Dealer)
CustodianDealer is my drop down coloumn and data for it comes from different table
when i edit or insert a new record in the grid, i can select the existing Custodiandealers in the table.
now if i want to insert a new record to the custodianDealer table not to the grid, how can i acheive it
Try something like this within the ItemDataBound event:
If TypeOf (e.Item) Is GridDataItem Then
Dim GDI As GridDataItem = CType(e.Item, GridDataItem)
GDI([ColumnNameAsString]).Text = "Some custom text"
End If
The GridDataItem is essentially a TableCell, so you can add controls as needed.
Oh, and if it wasn't clear from my notation, "[ColumnNameAsString]" is your own column name.
Wire the grid insert command and execute an insert query which updates merely the Custodiandealers table with a new entry. To display it in the dropdown editor, make sure the new record has a foreign relation with the grid main table source as the rest of the items in the Custodiandealers table.
So, if I'm reading your problem correctly, you are trying to insert a record into the custodianDealer table based on some changes made during edit/insert.
If that's the case, you can handle the ItemCommand event and update your datatable based on those changes.
Can you provide the exact details of the use case?
If I have an ASP.Net RadioButtonList within a TemplateField in a GridView, which is tied to a Data Control, I can get databinding on page loading like so:
<asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>'>
So when the gridview renders, the user can select an option for each row in the grid.
What I'm wondering is, when the save button is pressed and a postback occurs, is there a way to automatically push these selected values back through to the datacontrol so they can be saved, or do I have to manually loop through all the grid rows and exract the chosen value, like so:
For iRow As Integer = 0 To Me.myGrid.Rows.Count - 1
Dim selectedValue As String = CType(Me.myGrid.Rows(iRow).FindControl("RequirementOption"), RadioButtonList).SelectedValue
'...Load up the appropriate class and save the selected value
Next
Can it be done automatically via databinding?
NOTE: I am talking about about updating all the rows and then accessing all of them opn postback, not one row at a time via inline editing.
Yes, the selectedValue will be available as long as you specify Bind instead of Eval.
(assuming you're updating a record in the usual way)