G
Guest
I have a Datagrid that I can't seem to get to work. I can populate the grid
from a database but I can't figure out how to update the DB when the user
edits a templated column. I have done it before and copied the code over but
it isn't working. Any help is greatly appreciated.
Code behind:
Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As
DataGridCommandEventArgs)
'get a reference to the title and publication date text boxes
Dim objType As DropDownList
objType = CType(objArgs.Item.FindControl("UsrStatus"), DropDownList)
'OBJTYPE.SELECTEDVALUE IS 0 NO MATTER WHAT SELECTION IS MADE
Response.Write(objType.SelectedValue & " - " &
UnitLists.DataKeys(objArgs.Item.ItemIndex))
'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "UPDATE Unit_General SET Status=" &
CInt(objType.SelectedValue) & " WHERE ID=" &
UnitLists.DataKeys(objArgs.Item.ItemIndex)
Response.Write(strSQL)
ExecuteSQLStatement(strSQL)
If objType.SelectedValue = 0 Then
strSQL = "DELETE * FROM Users_Units WHERE PID=" &
objType.SelectedValue
ExecuteSQLStatement(strSQL)
End If
'set EditItemIndex property of grid to -1 to switch out of Edit mode
UnitLists.EditItemIndex = -1
BindDataGrid() 'bind the data and display it
End Sub
HTML PAGE:
<form id="Form1" method="post" runat="server">
<asp:datagrid id="UnitLists" runat="server" AllowSorting="True"
PageSize="50" EditItemStyle-BackColor="yellow"
AutoGenerateColumns="False" DataKeyField="ID" Width="696px"
BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4"
GridLines="Horizontal" ForeColor="Black"
OnCancelCommand="DoItemCancel" OnUpdateCommand="DoItemUpdate"
OnEditCommand="DoItemEdit">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CC3333"></SelectedItemStyle>
<EditItemStyle BackColor="Yellow"></EditItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#333333"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="UnitNumber" ReadOnly="True"
HeaderText="Unit #"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label id="txtType" Runat="server" text='<%#
databinder.eval(Container.dataitem,"StatusText") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList id="UsrStatus" runat="server">
<asp:ListItem Value="0">Available</asp:ListItem>
<asp:ListItem Value="1">Pending</asp:ListItem>
<asp:ListItem Value="2">Unavailable</asp:ListItem>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="FNAME" ReadOnly=True
HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LNAME" ReadOnly=True
HeaderText="Last Name"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="White"></PagerStyle>
</asp:datagrid></form>
from a database but I can't figure out how to update the DB when the user
edits a templated column. I have done it before and copied the code over but
it isn't working. Any help is greatly appreciated.
Code behind:
Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As
DataGridCommandEventArgs)
'get a reference to the title and publication date text boxes
Dim objType As DropDownList
objType = CType(objArgs.Item.FindControl("UsrStatus"), DropDownList)
'OBJTYPE.SELECTEDVALUE IS 0 NO MATTER WHAT SELECTION IS MADE
Response.Write(objType.SelectedValue & " - " &
UnitLists.DataKeys(objArgs.Item.ItemIndex))
'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "UPDATE Unit_General SET Status=" &
CInt(objType.SelectedValue) & " WHERE ID=" &
UnitLists.DataKeys(objArgs.Item.ItemIndex)
Response.Write(strSQL)
ExecuteSQLStatement(strSQL)
If objType.SelectedValue = 0 Then
strSQL = "DELETE * FROM Users_Units WHERE PID=" &
objType.SelectedValue
ExecuteSQLStatement(strSQL)
End If
'set EditItemIndex property of grid to -1 to switch out of Edit mode
UnitLists.EditItemIndex = -1
BindDataGrid() 'bind the data and display it
End Sub
HTML PAGE:
<form id="Form1" method="post" runat="server">
<asp:datagrid id="UnitLists" runat="server" AllowSorting="True"
PageSize="50" EditItemStyle-BackColor="yellow"
AutoGenerateColumns="False" DataKeyField="ID" Width="696px"
BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4"
GridLines="Horizontal" ForeColor="Black"
OnCancelCommand="DoItemCancel" OnUpdateCommand="DoItemUpdate"
OnEditCommand="DoItemEdit">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CC3333"></SelectedItemStyle>
<EditItemStyle BackColor="Yellow"></EditItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#333333"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="UnitNumber" ReadOnly="True"
HeaderText="Unit #"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label id="txtType" Runat="server" text='<%#
databinder.eval(Container.dataitem,"StatusText") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList id="UsrStatus" runat="server">
<asp:ListItem Value="0">Available</asp:ListItem>
<asp:ListItem Value="1">Pending</asp:ListItem>
<asp:ListItem Value="2">Unavailable</asp:ListItem>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="FNAME" ReadOnly=True
HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LNAME" ReadOnly=True
HeaderText="Last Name"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="White"></PagerStyle>
</asp:datagrid></form>