I'm working on an application where the user selects a record by choosing a item in a ListBox. When the ListBox item is selected the details for the record appera in a ListView grid. By default the ListView does not allow the user to add new reocrds.
On the page ther is a button (cmdAdd) that when clicked adds a new row to the top of the ListView. Data for a new record can be added in this row and saved to the database.
There is a datasource (dsProd) for the ListBox. This datasource has ProdNum (Data value Field) and ProdDescription (Data Text Field).
When the cmdAdd button is clicked and the "insert row" appearsin the ListView, I need to set the value of the txtProdNum column to the value of the PrddNum of the ListBox datasource (dsProd).
Can this be done? Would it be done in the html source code or in the code behind file? I'm at a complete loss. Any suggestions would be greatly appreciated.
Yes, txtProdNum is a column in the insert row of the ListView. I'd like the ProdNum to appear when the insert row appears. There is a save button on the row that will insert the row into the new database.
Yes it can be done. Here is some code for you. Only difference is the footer row is always visible. that you can change, Study the code, try to do it and respond. It is taken from a woking example page I use.
'CODE BEHIND in VB.net (If needed convert it)
Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs)
Dim _Description As String = DirectCast(GridView1.FooterRow.FindControl("txtDescription"), TextBox).Text
Dim _Mother As String = DirectCast(GridView1.FooterRow.FindControl("txtMother"), TextBox).Text
Dim _Child As String = DirectCast(GridView1.FooterRow.FindControl("txtChild"), TextBox).Text
If e.CommandName.Equals("Insert") Then
SD.InsertParameters("Father").DefaultValue = _Description
SD.InsertParameters("Mother").DefaultValue = _Mother
SD.InsertParameters("Child").DefaultValue = _Child
SD.Insert()
End If
End Sub
HTML FOR GRID VIEW
Are you using InsertItemTemplate for the Insert, or do you do it all in data?
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
In that case, handle ItemDataBound of the ListView. In the event arguments will be the Item. Check if it is the InsertItem. If it is, use FindControl to find the Controls that need their default value set. Then set these values.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Marked as answer by Tony Anthony on Mar 08, 2012 08:32 PM
Without testing, I was thinking something like this for ItemDataBound:
Protected Sub ListView1_ItemDataBound(sender As Object, e As EventArgs)
If e.Item.ItemType = ListViewItemType.InsertItem Then
Dim valueTextBox As TextBox = e.Item.FindControl("ValueTextBox")
valueTextBox.Text = "Initial value"
End If
End Sub
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Tony Anthony
Member
10 Points
8 Posts
Set value for a ListView coloumn
Mar 06, 2012 09:30 PM|LINK
I'm working on an application where the user selects a record by choosing a item in a ListBox. When the ListBox item is selected the details for the record appera in a ListView grid. By default the ListView does not allow the user to add new reocrds. On the page ther is a button (cmdAdd) that when clicked adds a new row to the top of the ListView. Data for a new record can be added in this row and saved to the database.
There is a datasource (dsProd) for the ListBox. This datasource has ProdNum (Data value Field) and ProdDescription (Data Text Field).
When the cmdAdd button is clicked and the "insert row" appearsin the ListView, I need to set the value of the txtProdNum column to the value of the PrddNum of the ListBox datasource (dsProd).
Can this be done? Would it be done in the html source code or in the code behind file? I'm at a complete loss. Any suggestions would be greatly appreciated.
Thanks....
viddhi
Participant
1458 Points
378 Posts
Re: Set value for a ListView coloumn
Mar 06, 2012 09:38 PM|LINK
You can do it code behind. So what is txtProdNum? is it a text box column in the insert row?
When do an add, where does it get stored? For me it looks like after the insert, you need to rebind your list box also.
Tony Anthony
Member
10 Points
8 Posts
Re: Set value for a ListView coloumn
Mar 06, 2012 11:13 PM|LINK
Yes, txtProdNum is a column in the insert row of the ListView. I'd like the ProdNum to appear when the insert row appears. There is a save button on the row that will insert the row into the new database.
basheerkal
Star
10672 Points
2426 Posts
Re: Set value for a ListView coloumn
Mar 07, 2012 01:39 AM|LINK
Yes it can be done. Here is some code for you. Only difference is the footer row is always visible. that you can change, Study the code, try to do it and respond. It is taken from a woking example page I use.
'CODE BEHIND in VB.net (If needed convert it)
Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Dim _Description As String = DirectCast(GridView1.FooterRow.FindControl("txtDescription"), TextBox).Text Dim _Mother As String = DirectCast(GridView1.FooterRow.FindControl("txtMother"), TextBox).Text Dim _Child As String = DirectCast(GridView1.FooterRow.FindControl("txtChild"), TextBox).Text If e.CommandName.Equals("Insert") Then SD.InsertParameters("Father").DefaultValue = _Description SD.InsertParameters("Mother").DefaultValue = _Mother SD.InsertParameters("Child").DefaultValue = _Child SD.Insert() End If End Sub HTML FOR GRID VIEW<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SD" ShowFooter="True" AllowPaging="True" onrowcommand="GridView1_RowCommand" EnableTheming="False" > <Columns> <asp:TemplateField ShowHeader="False"> <FooterTemplate> <asp:LinkButton ID="lbInsert" runat="server" CommandName="Insert">Insert</asp:LinkButton> </FooterTemplate> </asp:TemplateField> <asp:BoundField DataField="Id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="father" SortExpression="Father"> <EditItemTemplate> <asp:TextBox ID="txtDescripton" runat="server" Text='<%# Bind("Father") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Father") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtDescription" runat="server" /></FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mother" SortExpression="Mother"> <EditItemTemplate> <asp:TextBox ID="txtMother" runat="server" Text='<%# Bind("Mother") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblMother" runat="server" Text='<%# Bind("Mother") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtMother" runat="server" /></FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Child" SortExpression="Child"> <EditItemTemplate> <asp:TextBox ID="txtChild" runat="server" Text='<%# Bind("Child") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblChild" runat="server" Text='<%# Bind("Child") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtChild" runat="server" /></FooterTemplate> </asp:TemplateField> </Columns> <HeaderStyle BorderStyle="None" /> </asp:GridView>HTML FRO DATASOUCE <asp:SqlDataSource ID="SD" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnection %>" SelectCommand="SELECT * FROM [Table2] ORDER BY ID DESC" InsertCommand="INSERT INTO [table2] ([Father], [Mother], [Child]) VALUES (@Father, @Mother, @Child)" UpdateCommand="UPDATE [Table2] SET [Father] = @father, [Mother] = @Mother, [Child] = @Child WHERE [Id] = @Id" oninserting="onInserting"> <InsertParameters> <asp:Parameter Name="father" Type="String" /> <asp:Parameter Name="Mother" Type="String" /> <asp:Parameter Name="Child" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="father" Type="String" /> <asp:Parameter Name="Mother" Type="String" /> <asp:Parameter Name="Child" Type="String" /> <asp:Parameter Name="Id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource>(Talk less..Work more)
superguppie
All-Star
48225 Points
8679 Posts
Re: Set value for a ListView coloumn
Mar 07, 2012 08:57 AM|LINK
Are you using InsertItemTemplate for the Insert, or do you do it all in data?
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
basheerkal
Star
10672 Points
2426 Posts
Re: Set value for a ListView coloumn
Mar 07, 2012 11:21 AM|LINK
Sorry Tony
You asked fro ListView and I gave you code for Gridview so srry. I will ask moderator to delete that post
You may refer this Link
http://www.codedigest.com/Articles/ASPNET/105_EditUpdateDelete_and_Insert_in_ListView_Control.aspx
B
(Talk less..Work more)
Tony Anthony
Member
10 Points
8 Posts
Re: Set value for a ListView coloumn
Mar 07, 2012 04:27 PM|LINK
I am using the InsertItemTemplate.
superguppie
All-Star
48225 Points
8679 Posts
Re: Set value for a ListView coloumn
Mar 08, 2012 07:28 AM|LINK
In that case, handle ItemDataBound of the ListView. In the event arguments will be the Item. Check if it is the InsertItem. If it is, use FindControl to find the Controls that need their default value set. Then set these values.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Tony Anthony
Member
10 Points
8 Posts
Re: Set value for a ListView coloumn
Mar 21, 2012 10:06 PM|LINK
I am still at a loss. Can you give me an example of how to code this using VB, or do you know of a link where I can fird examples of code. Thank you.
superguppie
All-Star
48225 Points
8679 Posts
Re: Set value for a ListView coloumn
Mar 22, 2012 12:22 PM|LINK
Without testing, I was thinking something like this for ItemDataBound:
Protected Sub ListView1_ItemDataBound(sender As Object, e As EventArgs) If e.Item.ItemType = ListViewItemType.InsertItem Then Dim valueTextBox As TextBox = e.Item.FindControl("ValueTextBox") valueTextBox.Text = "Initial value" End If End SubPlease remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.