Keep in mind I am using a different database than you
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="newsgroup.aspx.vb" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="TaskID" DataSourceID="SqlTempTbl">
<Columns>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="TaskID" HeaderText="TaskID"
InsertVisible="False"
ReadOnly="True" SortExpression="TaskID" />
<asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task"
/>
<asp:BoundField DataField="TaskGroup" HeaderText="TaskGroup"
SortExpression="TaskGroup" />
<asp:BoundField DataField="FrKey" HeaderText="FrKey"
SortExpression="FrKey" />
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Bind("TaskGroup") %>' />
<asp
ropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp
ropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlTempTbl"
runat="server" ConnectionString="<%$ ConnectionStrings:ItemConnStr %>"
SelectCommand="SELECT [Description], [TaskID], [Task], [TaskGroup],
[FrKey] FROM [tblTask]"
DeleteCommand="DELETE FROM [tblTask] WHERE [TaskID] = @TaskID"
InsertCommand="INSERT INTO [tblTask] ([Description], [Task], [TaskGroup],
[FrKey]) VALUES (@Description, @Task, @TaskGroup, @FrKey)"
UpdateCommand="UPDATE [tblTask] SET [Description] = @Description, [Task] =
@Task, [TaskGroup] = @TaskGroup, [FrKey] = @FrKey WHERE [TaskID] =
@TaskID">
<DeleteParameters>
<asp
arameter Name="TaskID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp
arameter Name="Description" Type="String" />
<asp
arameter Name="Task" Type="String" />
<asp
arameter Name="TaskGroup" Type="String" />
<asp
arameter Name="FrKey" Type="Int32" />
<asp
arameter Name="TaskID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp
arameter Name="Description" Type="String" />
<asp
arameter Name="Task" Type="String" />
<asp
arameter Name="TaskGroup" Type="String" />
<asp
arameter Name="FrKey" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Partial Class Default4
Inherits System.Web.UI.Page
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
dd = e.Row.FindControl("DropDownList1")
If Not dd Is Nothing Then
Dim z As ListItem
z = New ListItem(HF.Value, HF.Value)
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <> CInt(HF.Value) Then
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next
End If
End If
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim HF As HiddenField = sender.FindControl("HiddenField1")
Dim dd As DropDownList = sender.FindControl("DropDownList1")
If Not HF Is Nothing Then
HF.Value = dd.SelectedValue
End If
End Sub
End Class
Ben said:
when i remove the If Not HF Is Nothing Then
i get the error: Object reference not set to an instance of an object.
at line: HF.Value = dd.SelectedValue
it seems that the hiddenfield is not recognized.
John said:
check to see if DD autopostback is set to true, if it still does not
fire, try puting that code in the event updating.
it is firing in my test though and saving changes
John
Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be
fired, because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.
Tricky problem, isn't it?
"John" <
[email protected]> schreef in bericht
if I get what your saying, because the DD is now unbound you will need
add an event on the DD that assigns the new value to the HF.
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs)
Dim HF As HiddenField = sender.FindControl("HiddenField1")
Dim dd As DropDownList = sender.FindControl("DropDownList1")
If Not HF Is Nothing Then
HF.Value = dd.SelectedValue
End If
End Sub
Hi John,
i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null
value in the table)
with Bind, the value displayed before clicking the Update button
remains unchanged
The update works for the other (normal) field ("operator").
UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] =
@operator WHERE [id] = @id">
<UpdateParameters>
<asp
arameter Name="wa" Type="String" />
<asp
arameter Name="operator" Type="String" />
</UpdateParameters>
<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Bind("wa") %>' />
<asp
ropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp
ropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("wa") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem
z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <> CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next
Thanks for your time.
Ben