OS: WinXP Pro, VB/ASP/ADO.NET
I'm building a web-based supplier management application. For each supplier we are required by the FDA to track certain documents. I've managed to pull them from Access into a Gridview (Gridview1), in which I have AutoGenerateEditButton set to True.
What I want is this: when a user clicks "Edit" on a line and it goes to edit mode, I want to grab the supplier's category (4th column of the grid, named SupplierCategory), hit an Access DB called Requirements and get the requirements for each each document field. The first field I'm working with is called SplAudit. So if Requirements.SplAudit comes back with a value of "Y" then the SplAudit column of the grid should be rendered with a textbox, or else it should be rendered with a label.
Here's what I have so far: I come into GridView1_RowEditing, set the current category (currently hard-coded), set up the SQL Select (depends on session keys acting as filtering variables), load GridView1, then call TurnOnLabelOrTextBox.
TurnOnLabelOrTextBox hits the Requirements table to get the fields required (any that come back with a value of 'Y' - currently retrieving only SplAudit), and then it calls CreateAttributeString to build the attribute string that is referenced in the ASP code.
I've managed to make SplAudit's value disappear in edit mode, so I'm guessing the dereferencing in my attribute string for the edit template is not working. It's also displaying neither a label nor a textbox.
On the web page:
<asp:GridView ID="GridView1" runat="server" Width="941px"
BorderColor="Black" BorderStyle="Solid"
CaptionAlign="Top" ForeColor="Black"
autogeneratecolumns="False" AutoGenerateEditButton="True">
<Columns>
.
.
.
<asp:TemplateField HeaderText="Specialized Audit">
<ItemTemplate>
<asp:label ID="txtSplAudit" Text='<%# DataBinder.Eval
(Container.DataItem, "SplAudit") %>' Runat="server" />
</ItemTemplate>
<EditItemTemplate >
<%#SplAuditAttributes %>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
In the code-behind I have:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Partial Class frmFilter
Inherits System.Web.UI.Page
Public SplAuditAttributes as string
Protected Sub GridView1_RowEditing(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
Handles GridView1.RowEditing
Dim CurCat As String
CurCat = "SPROC" 'sender.SelectedRow.Cells(3).Text()
' value of SPROC turns fldswitch to 'Y' in TurnOnLabelOrTextBox
' How do I get the current value of SupplierCategory out of the grid?!
'CurCat = sender.Rows(index of selected row).Cells(3).Text - doesn't work
ConstructSQLString() ' Builds the SQL Select statement based on session
' keys
LoadTheGrid() ' Binds the data to the grid
TurnOnLabelOrTextBox(CurCat) ' Turns editable fields to textbox or label
End Sub
Protected Sub TurnOnLabelOrTextBox(ByVal CurCat As String)
' This procedures hits the Requirements table and returns the fields'
' values of whether they're required or not. DB columns have same names
' as grid columns.
' Verified to be working correctly.
Dim dsReqs As DataSet = New DataSet
Dim fldswitch As String
Dim strSQL As String
strSQL = "SELECT SplAudit FROM Requirements" & _
" WHERE Category = '" & CurCat & "'"
Dim cn As New OleDbConnection
cn.ConnectionString = strConn
Dim cmd As New OleDbCommand(strSQL, cn)
Dim adpt As New OleDbDataAdapter
adpt.SelectCommand = cmd
Dim dtReqs As DataTable = New DataTable("Reqs")
' Add the columns to dtSumm
dtReqs.Columns.Add("SplAudit", System.Type.GetType("System.String"))
dsReqs.Tables.Add(dtReqs)
' Execute the query and fill the data adapter.
adpt.Fill(dtReqs)
' Create a datatable reader
Dim dtRdrName As DataTableReader = dtReqs.CreateDataReader
Do While dtRdrName.Read()
fldswitch = dtRdrName.Item("SplAudit").ToString
Loop
cn.Close()
CreateAttributeString(fldswitch, "SplAudit", SplAuditAttributes)
' Trash pickup - not sure how much of this I really need.
cn = Nothing
dtRdrName = Nothing
adpt = Nothing
dtReqs = Nothing
dsReqs = Nothing
End Sub
Protected Sub CreateAttributeString(ByVal fldsw As String, _
ByVal fldname As String, _
ByRef fldattrib As String)
Dim quot As String = Chr(34) ' Put the double quote char in a variable.
If fldsw = "Y" Then
' If the field is required, return a text box
' Not sure if dereferenced text values will work here.
' If not, how do I get the value coded into the attribute string?
fldattrib = "<asp:TextBox ID=" & quot & "txt" & fldname & _
quot & "runat=" & quot & "server" & quot & " Columns=" & _
quot & "20" & quot & " Text='<#bind(" & quot & fldname & quot & _
") %>' />"
Else
' If the field is turned off, return a label box.
fldattrib = "<asp:label ID=" & quot & "txt" & fldname & quot & _
" Text='<%# DataBinder.Eval(Container.DataItem, " & quot & fldname & quot & _
") %>' Runat=" & quot & "server" & quot & " />"
End If
End Sub