473,508 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Gridview - conditional textbox/label in edit templates

3 New Member
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
Nov 22 '06 #1
1 12206
cyningeston
3 New Member
Got it. I finally figured out how to grab the cell value. With that I was able to drive the query to get the requirements. From there I figured out that I had to feed the ASP the post-rendered HTML code. For labels I set up the <span id="field name">field value </span> tags.

I still need to find out if I can jimmy the code to feed data back to the database for fields I want to save, but I can do that tomorrow. This is good enough for today.
Nov 27 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6530
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
4
4458
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2...
0
2374
by: Mike P | last post by:
Where exactly are the updateparameters of a gridview picked up from? I have created 2 very similar gridviews and given the updateparameters the same names as in my edititemtemplates. Yet this...
2
3793
by: VB Programmer | last post by:
I have a gridview control with several data fields. When the user clicks 'edit' for the row I want the user to be able to edit a particular text field. Under that 'edit' textbox is a button. ...
0
2465
by: Mike P | last post by:
I am trying to edit a gridview while using paging, but whenever I try to edit a row on a page other than page 1, I get an error. Here is my gridview and my code : <asp:GridView ID="GridView1"...
2
1475
by: Danielle | last post by:
All - Thanks in advance for any help you can provide. I've been working with a GridView in Visual Basic for a long time trying to get a list of contacts and to be able to edit and delete the...
4
10988
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
0
1574
by: Andy B | last post by:
I have a GridView with a LINQDataSource attached to it. My code in the aspx markup is below. I need to know why only the checkbox field for the activated column is getting updated (the...
6
4255
by: Ahmedhussain | last post by:
Hi there, I m doing work on a gridview and Im getting an error: A potentially dangerous Request.Form value was detected from the client (ctl00$Content$GridView1$ctl03$TextBox1="<span...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.