J
jason
Hi.
The below code works, except, when I edit a record and update, the two
drop downs always take the first entry in the dropdownlist if not
selected. I'd also like the dropdown to show the currently set value in
edit mode. I'm sure this is a common question. I've reviewed several
related post and tried them out to no avial. Some of the threads in the
listgroup look right on, but are not clear to me.
Thank you for any help or information.
<%@ Page Language="VB" Debug=true%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.String" %>
<%@ import Namespace="System.Web.Mail" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
BindData()
End If
End Sub
Public Sub BindData()
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
objConn.Open()
Dim oaUser As OleDbDataAdapter
Dim UserDS as DataSet = New DataSet()
oaUser = New OleDbDataAdapter("Select * FROM appworx", objConn)
oaUser.Fill(UserDS,"appworx")
objConn.Close
UserGrid.DataSource = UserDS.Tables("appworx")
UserGrid.DataBind()
End Sub
Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = E.Item.ItemIndex
BindData()
End Sub
Public Sub UserGrid_Cancel (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = -1
BindData()
End Sub
Public Sub UserGrid_Update (Source As Object, E As
DataGridCommandEventArgs)
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand ("UPDATE appworx SET
responsible=@responsible,status=@sstatus,waitingon=@swaitingon,comments=@comments
WHERE chain=@chain", objConn)
Dim schain As String = e.Item.Cells(2).text
Dim sresponsible As String =
CType(e.Item.findcontrol("responsible"),
Dropdownlist).SelectedItem.value
Dim sstatus As String = CType(e.Item.findcontrol("status"),
Dropdownlist).SelectedItem.value
Dim swaitingon As String = CType(e.Item.Cells(5).Controls (0),
TextBox).Text
Dim scomments As String = CType(e.Item.Cells(6).Controls (0),
TextBox).Text
cmd.Parameters.Add(new OleDbParameter("@responsible",
sresponsible))
cmd.Parameters.Add(new OleDbParameter("@status", sstatus))
cmd.Parameters.Add(new OleDbParameter("@waitingon",swaitingon))
cmd.Parameters.Add(new OleDbParameter ("@comments",scomments))
cmd.Parameters.Add(new OleDbParameter("@chain", schain))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
UserGrid.EditItemIndex = -1
BindData()
End Sub
Public Sub UserGrid_Command(sender As Object, e As
DataGridCommandEventArgs)
Select (CType(e.CommandSource, LinkButton)).CommandName
Case "Delete"
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("DELETE FROM
appworx WHERE chain = @chain", objConn)
cmd.Parameters.Add(new OleDbParameter("@chain",
e.Item.Cells(2).Text))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
Case Else
' Do Nothing
End Select
BindData()
End Sub
Public Sub UserGrid_ItemCreated(sender As Object, e As
DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem
' Add confirmation to Delete button
Dim tblCell As TableCell
Dim btnDelete As LinkButton
tblCell = e.Item.Cells(1)
btnDelete = tblCell.Controls(0)
btnDelete.Attributes.Add("onclick", "return confirm('Are you
sure you want to delete?');")
End Select
End Sub
Public Sub newrec_Click(sender As Object, e As EventArgs)
addrecord.visible = false
addinsert.visible = true
AddCancel.Visible = true
addchain.Visible = true
addresponsible.Visible = true
addstatus.Visible = true
addwaitingon.Visible = true
addcomments.Visible = true
End Sub
Public Sub Insert_Click(sender As Object, e As EventArgs)
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("INSERT INTO appworx
(chain, responsible,status,waitingon,comments)
values(@chain,@responsible,@status,@waitingon,@comments)", objConn)
cmd.Parameters.Add(new OleDbParameter("@chain", Addchain.Text))
cmd.Parameters.Add(new OleDbParameter("@responsible",
Addresponsible.Text))
cmd.Parameters.Add(new OleDbParameter("@status", Addstatus.Text))
cmd.Parameters.Add(new OleDbParameter("@waitingon",
Addwaitingon.Text))
cmd.Parameters.Add(new OleDbParameter("@comments",
Addcomments.Text))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
addrecord.visible = true
addinsert.visible = false
AddCancel.Visible = false
addchain.Visible = false
addresponsible.Visible = false
addstatus.Visible = false
addwaitingon.Visible = false
addcomments.Visible = false
BindData()
End Sub
Public Sub AddCancel_Click(sender As Object, e As EventArgs)
addrecord.visible = true
addinsert.visible = false
addcancel.Visible = false
addchain.Visible = false
addresponsible.Visible = false
addstatus.Visible = false
addwaitingon.Visible = false
addcomments.Visible = false
' Reset text fields (for next time)
addchain.text = ""
addresponsible.text = ""
addstatus.text = ""
addwaitingon.text = ""
addcomments.text = ""
End Sub
</script>
<html>
<head>
</head>
<body>
<form method="post" runat="server">
<table bgcolor=lightgrey border=0>
<tr>
<td span=2 width=160px><asp:linkbutton id ="AddRecord"
width=40px Text="New" runat="server" visible="true"
onClick="NewRec_Click" /> </td>
<td width=70px>Chain</td>
<td width=100px>Responsible</td>
<td width=100px>Status</td>
<td width=100px>Waiting On</td>
<td width=300px>Comments</td>
</tr>
<tr>
<td span=2 width=160px>
<asp:linkbutton id ="AddInsert" Text="Update" runat="server"
visible="false" onClick="Insert_Click" />
<asp:linkbutton id="AddCancel" Text="Cancel" runat="server"
visible="false" onClick="AddCancel_Click"/></td>
<td ><asp:textbox id="Addchain" width=70px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addresponsible" width=100px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addstatus" width=100px runat="server"
visible="false"/></td>
<td><asp:textbox id="Addwaitingon" width=100px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addcomments" width=300px runat="server"
visible="false" /></td>
</table>
<asp:datagrid id="UserGrid" runat=server AutoGenerateColumns=false
BorderStyle="Dotted" BorderWidth="2"
BackgroundColor="red"
CellPadding="5"
Font-Name="Arial" Font-Size="10pt"
OnEditCommand="UserGrid_Edit"
OnCancelCommand="UserGrid_Cancel"
OnUpdateCommand="UserGrid_Update"
OnItemCommand="UserGrid_Command"
OnItemCreated="UserGrid_ItemCreated">
<EditItemStyle BackColor="yellow">
</EditItemStyle>
<ItemStyle Wrap="false">
</ItemStyle>
<Columns>
<asp:EditCommandColumn
ButtonType ="LinkButton"
CancelText = "Cancel"
EditText = "Edit"
UpdateText = "Update"<itemstyle width=100px />
</asp:EditCommandColumn>
<asp:ButtonColumn
ButtonType="LinkButton"
Text="Delete"
CommandName="Delete"/>
<asp:BoundColumn DataField = "chain" ReadOnly = true >
<itemstyle width=70px />
</asp:BoundColumn>
<asp:TemplateColumn SortExpression="responsible" >
<itemstyle width=100px />
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("responsible") %>'
runat="server" ID="Llbresp"/>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList runat="server" id="responsible">
<asp:ListItem value="x">x</asp:ListItem>
<asp:ListItem value="y">y</asp:ListItem>
<asp:ListItem value="z">z</asp:ListItem>
<asp:ListItem value="g">g</asp:ListItem>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<itemstyle width=100px />
<ItemTemplate >
<asp:Label runat="server" Text='<%# Container.DataItem("status")
%>' ID="Llbstat"/>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList runat="server" id="status">
<asp:ListItem value="Pending">Pending</asp:ListItem>
<asp:ListItem value="Developing">Developing</asp:ListItem>
<asp:ListItem value="Testing">Testing</asp:ListItem>
<asp:ListItem value="Hold">Hold</asp:ListItem>
<asp:ListItem value="Passed">Passed</asp:ListItem>
<asp:ListItem value="Delete">Delete</asp:ListItem>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField = "waitingon">
<itemstyle width=100px />
</asp:BoundColumn>
<asp:BoundColumn DataField = "comments">
<itemstyle width=300px />
</asp:BoundColumn>
</Columns>
</asp:datagrid>
</form>
The below code works, except, when I edit a record and update, the two
drop downs always take the first entry in the dropdownlist if not
selected. I'd also like the dropdown to show the currently set value in
edit mode. I'm sure this is a common question. I've reviewed several
related post and tried them out to no avial. Some of the threads in the
listgroup look right on, but are not clear to me.
Thank you for any help or information.
<%@ Page Language="VB" Debug=true%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.String" %>
<%@ import Namespace="System.Web.Mail" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
BindData()
End If
End Sub
Public Sub BindData()
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
objConn.Open()
Dim oaUser As OleDbDataAdapter
Dim UserDS as DataSet = New DataSet()
oaUser = New OleDbDataAdapter("Select * FROM appworx", objConn)
oaUser.Fill(UserDS,"appworx")
objConn.Close
UserGrid.DataSource = UserDS.Tables("appworx")
UserGrid.DataBind()
End Sub
Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = E.Item.ItemIndex
BindData()
End Sub
Public Sub UserGrid_Cancel (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = -1
BindData()
End Sub
Public Sub UserGrid_Update (Source As Object, E As
DataGridCommandEventArgs)
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand ("UPDATE appworx SET
responsible=@responsible,status=@sstatus,waitingon=@swaitingon,comments=@comments
WHERE chain=@chain", objConn)
Dim schain As String = e.Item.Cells(2).text
Dim sresponsible As String =
CType(e.Item.findcontrol("responsible"),
Dropdownlist).SelectedItem.value
Dim sstatus As String = CType(e.Item.findcontrol("status"),
Dropdownlist).SelectedItem.value
Dim swaitingon As String = CType(e.Item.Cells(5).Controls (0),
TextBox).Text
Dim scomments As String = CType(e.Item.Cells(6).Controls (0),
TextBox).Text
cmd.Parameters.Add(new OleDbParameter("@responsible",
sresponsible))
cmd.Parameters.Add(new OleDbParameter("@status", sstatus))
cmd.Parameters.Add(new OleDbParameter("@waitingon",swaitingon))
cmd.Parameters.Add(new OleDbParameter ("@comments",scomments))
cmd.Parameters.Add(new OleDbParameter("@chain", schain))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
UserGrid.EditItemIndex = -1
BindData()
End Sub
Public Sub UserGrid_Command(sender As Object, e As
DataGridCommandEventArgs)
Select (CType(e.CommandSource, LinkButton)).CommandName
Case "Delete"
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("DELETE FROM
appworx WHERE chain = @chain", objConn)
cmd.Parameters.Add(new OleDbParameter("@chain",
e.Item.Cells(2).Text))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
Case Else
' Do Nothing
End Select
BindData()
End Sub
Public Sub UserGrid_ItemCreated(sender As Object, e As
DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem
' Add confirmation to Delete button
Dim tblCell As TableCell
Dim btnDelete As LinkButton
tblCell = e.Item.Cells(1)
btnDelete = tblCell.Controls(0)
btnDelete.Attributes.Add("onclick", "return confirm('Are you
sure you want to delete?');")
End Select
End Sub
Public Sub newrec_Click(sender As Object, e As EventArgs)
addrecord.visible = false
addinsert.visible = true
AddCancel.Visible = true
addchain.Visible = true
addresponsible.Visible = true
addstatus.Visible = true
addwaitingon.Visible = true
addcomments.Visible = true
End Sub
Public Sub Insert_Click(sender As Object, e As EventArgs)
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=db.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("INSERT INTO appworx
(chain, responsible,status,waitingon,comments)
values(@chain,@responsible,@status,@waitingon,@comments)", objConn)
cmd.Parameters.Add(new OleDbParameter("@chain", Addchain.Text))
cmd.Parameters.Add(new OleDbParameter("@responsible",
Addresponsible.Text))
cmd.Parameters.Add(new OleDbParameter("@status", Addstatus.Text))
cmd.Parameters.Add(new OleDbParameter("@waitingon",
Addwaitingon.Text))
cmd.Parameters.Add(new OleDbParameter("@comments",
Addcomments.Text))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
addrecord.visible = true
addinsert.visible = false
AddCancel.Visible = false
addchain.Visible = false
addresponsible.Visible = false
addstatus.Visible = false
addwaitingon.Visible = false
addcomments.Visible = false
BindData()
End Sub
Public Sub AddCancel_Click(sender As Object, e As EventArgs)
addrecord.visible = true
addinsert.visible = false
addcancel.Visible = false
addchain.Visible = false
addresponsible.Visible = false
addstatus.Visible = false
addwaitingon.Visible = false
addcomments.Visible = false
' Reset text fields (for next time)
addchain.text = ""
addresponsible.text = ""
addstatus.text = ""
addwaitingon.text = ""
addcomments.text = ""
End Sub
</script>
<html>
<head>
</head>
<body>
<form method="post" runat="server">
<table bgcolor=lightgrey border=0>
<tr>
<td span=2 width=160px><asp:linkbutton id ="AddRecord"
width=40px Text="New" runat="server" visible="true"
onClick="NewRec_Click" /> </td>
<td width=70px>Chain</td>
<td width=100px>Responsible</td>
<td width=100px>Status</td>
<td width=100px>Waiting On</td>
<td width=300px>Comments</td>
</tr>
<tr>
<td span=2 width=160px>
<asp:linkbutton id ="AddInsert" Text="Update" runat="server"
visible="false" onClick="Insert_Click" />
<asp:linkbutton id="AddCancel" Text="Cancel" runat="server"
visible="false" onClick="AddCancel_Click"/></td>
<td ><asp:textbox id="Addchain" width=70px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addresponsible" width=100px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addstatus" width=100px runat="server"
visible="false"/></td>
<td><asp:textbox id="Addwaitingon" width=100px runat="server"
visible="false" /></td>
<td><asp:textbox id="Addcomments" width=300px runat="server"
visible="false" /></td>
</table>
<asp:datagrid id="UserGrid" runat=server AutoGenerateColumns=false
BorderStyle="Dotted" BorderWidth="2"
BackgroundColor="red"
CellPadding="5"
Font-Name="Arial" Font-Size="10pt"
OnEditCommand="UserGrid_Edit"
OnCancelCommand="UserGrid_Cancel"
OnUpdateCommand="UserGrid_Update"
OnItemCommand="UserGrid_Command"
OnItemCreated="UserGrid_ItemCreated">
<EditItemStyle BackColor="yellow">
</EditItemStyle>
<ItemStyle Wrap="false">
</ItemStyle>
<Columns>
<asp:EditCommandColumn
ButtonType ="LinkButton"
CancelText = "Cancel"
EditText = "Edit"
UpdateText = "Update"<itemstyle width=100px />
</asp:EditCommandColumn>
<asp:ButtonColumn
ButtonType="LinkButton"
Text="Delete"
CommandName="Delete"/>
<asp:BoundColumn DataField = "chain" ReadOnly = true >
<itemstyle width=70px />
</asp:BoundColumn>
<asp:TemplateColumn SortExpression="responsible" >
<itemstyle width=100px />
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("responsible") %>'
runat="server" ID="Llbresp"/>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList runat="server" id="responsible">
<asp:ListItem value="x">x</asp:ListItem>
<asp:ListItem value="y">y</asp:ListItem>
<asp:ListItem value="z">z</asp:ListItem>
<asp:ListItem value="g">g</asp:ListItem>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<itemstyle width=100px />
<ItemTemplate >
<asp:Label runat="server" Text='<%# Container.DataItem("status")
%>' ID="Llbstat"/>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList runat="server" id="status">
<asp:ListItem value="Pending">Pending</asp:ListItem>
<asp:ListItem value="Developing">Developing</asp:ListItem>
<asp:ListItem value="Testing">Testing</asp:ListItem>
<asp:ListItem value="Hold">Hold</asp:ListItem>
<asp:ListItem value="Passed">Passed</asp:ListItem>
<asp:ListItem value="Delete">Delete</asp:ListItem>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField = "waitingon">
<itemstyle width=100px />
</asp:BoundColumn>
<asp:BoundColumn DataField = "comments">
<itemstyle width=300px />
</asp:BoundColumn>
</Columns>
</asp:datagrid>
</form>