I have a webpage which retrieves data from a table and display it in a datagrid. This datagrid has edit, cancel, update and add new links. When i click on the edit or add new link, there appears a textbox in the fields. If i enter a value and update it works fine... but I dont want textbox there , instead I want a dropdown menu which should fetch data from another table. so that the user dont have to type but just select it...BUt these dropdown should only appear when I click on edit or new . Can it be done????
How can I do that...
My code is like this...
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sql
Client" %>
<script runat="server">
' TODO: update the ConnectionString and Command values for your application
Dim ConnectionString As String = "server=ser;uid=sa;pwd=sa;
database=d
ata;"
Dim SelectCommand As String = "select FullName, GroupName from tblusers inner join tblwebaccess on tblwebaccess.id_user=tblus
ers.id_use
r inner join tblReports_AutoGen_GroupTy
pes on tblwebaccess.ID_reportgrou
p =tblReports_AutoGen_GroupT
ypes.ID_re
portgroup order by fullname"
Dim isEditing As Boolean = False
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
' Databind the data grid on the first request only
' (on postback, bind only in editing, paging and sorting commands)
BindGrid()
End If
End Sub
' --------------------------
----------
----------
----------
-------
'
' DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
'
Sub DataGrid_ItemCommand(Sende
r As Object, E As DataGridCommandEventArgs)
' this event fires prior to all of the other commands
' use it to provide a more graceful transition out of edit mode
CheckIsEditing(e.CommandNa
me)
End Sub
Sub CheckIsEditing(commandName
As String)
If DataGrid1.EditItemIndex <> -1 Then
' we are currently editing a row
If commandName <> "Cancel" And commandName <> "Update" Then
' user's edit changes (If any) will not be committed
Message.Text = "Your changes have not been saved yet. Please press update to save your changes, or cancel to discard your changes, before selecting another item."
isEditing = True
End If
End If
End Sub
Sub DataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
' turn on editing for the selected row
If Not isEditing Then
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid()
End If
End Sub
Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
' update the database with the new values
' get the edit text boxes
'Dim id As String = CType(e.Item.Cells(2).Cont
rols(0), TextBox).Text
Dim fullname As String = CType(e.Item.Cells(0).Cont
rols(0), TextBox).Text
Dim groupname As String = CType(e.Item.Cells(1).Cont
rols(0), TextBox).Text
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim UpdateCommand As SqlCommand = new SqlCommand()
UpdateCommand.Connection = myConnection
If AddingNew = True Then
UpdateCommand.CommandText = "insert into tblwebaccess (id_user,id_reportgroup) select id_user,ID_reportgroup from tblusers,tblReports_AutoGe
n_GroupTyp
es where tblusers.fullname like '" & fullname &"%' and tblReports_AutoGen_GroupTy
pes.groupn
ame like '" & groupname & "%'"
Else
'UpdateCommand.CommandText
= "UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE au_id = @au_id"
End If
'UpdateCommand.Parameters.
Add("@au_i
d", SqlDbType.VarChar, 11).Value = id
'UpdateCommand.Parameters.
Add("@au_l
name", SqlDbType.VarChar, 40).Value = lname
' UpdateCommand.Parameters.A
dd("@au_fn
ame", SqlDbType.VarChar, 20).Value = fname
' execute the command
Try
myConnection.Open()
UpdateCommand.ExecuteNonQu
ery()
Catch ex1 as SQLException
Message.Text="Record already present"
Catch ex as Exception
Message.Text = ex.ToString()
Finally
myConnection.Close()
End Try
' Resort the grid for new records
If AddingNew = True Then
DataGrid1.CurrentPageIndex
= 0
AddingNew = false
End If
' rebind the grid
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
Sub DataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)
' cancel editing
DataGrid1.EditItemIndex = -1
BindGrid()
AddingNew = False
End Sub
Sub DataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)
' delete the selected row
If Not isEditing Then
' the key value for this row is in the DataKeys collection
'Dim keyValue As String = CStr(DataGrid1.DataKeys(e.
Item.ItemI
ndex))
'response.write(e.Item.Ite
mIndex)
'response.write(e.item.cel
ls(3).text
)
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionSt
ring)
'Dim DeleteCommand As New SqlCommand("DELETE from tblwebaccess where id=" & keyValue , myConnection)
Dim DeleteCommand As New SqlCommand("delete from tblwebaccess where id_user=(select id_user from tblusers where fullname like '" & e.item.cells(0).text &"') and id_ReportGroup=(select id_reportgroup from tblReports_AutoGen_GroupTy
pes where groupname like '" & e.item.cells(1).text & "')" , myConnection)
' execute the command
myConnection.Open()
DeleteCommand.ExecuteNonQu
ery()
myConnection.Close()
' rebind the grid
DataGrid1.CurrentPageIndex
= 0
DataGrid1.EditItemIndex = -1
BindGrid()
End If
End Sub
Sub DataGrid_Page(Sender As Object, E As DataGridPageChangedEventAr
gs)
' display a new page of data
If Not isEditing Then
DataGrid1.EditItemIndex = -1
DataGrid1.CurrentPageIndex
= e.NewPageIndex
BindGrid()
End If
End Sub
Sub AddNew_Click(Sender As Object, E As EventArgs)
' add a new row to the end of the data, and set editing mode 'on'
CheckIsEditing("")
If Not isEditing = True Then
' set the flag so we know to do an insert at Update time
AddingNew = True
' add new row to the end of the dataset after binding
' first get the data
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim myCommand As New SqlDataAdapter(SelectComma
nd, myConnection)
Dim ds As New DataSet()
myCommand.Fill(ds)
' add a new blank row to the end of the data
Dim rowValues As Object() = {"", ""}
ds.Tables(0).Rows.Add(rowV
alues)
' figure out the EditItemIndex, last record on last page
Dim recordCount As Integer = ds.Tables(0).Rows.Count
If recordCount > 1 Then
recordCount -= 1
DataGrid1.CurrentPageIndex
= recordCount \ DataGrid1.PageSize
DataGrid1.EditItemIndex = recordCount Mod DataGrid1.PageSize
End If
' databind
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End If
End Sub
' --------------------------
----------
----------
----------
-------
'
' Helpers Methods:
'
' property to keep track of whether we are adding a new record,
' and save it in viewstate between postbacks
Property AddingNew() As Boolean
Get
Dim o As Object = ViewState("AddingNew")
If o Is Nothing Then
Return False
End If
Return CBool(o)
End Get
Set(ByVal Value As Boolean)
ViewState("AddingNew") = Value
End Set
End Property
Sub BindGrid()
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim myCommand As New SqlDataAdapter(SelectComma
nd, myConnection)
Dim ds As New DataSet()
myCommand.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
'DataGrid1.columns(0).visi
ble=false
End Sub
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Web Access Control
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGr
id_Page" PageSize="6" AllowPaging="true" OnDeleteCommand="DataGrid_
Delete" OnCancelCommand="DataGrid_
Cancel" OnUpdateCommand="DataGrid_
Update" OnEditCommand="DataGrid_Ed
it" OnItemCommand="DataGrid_It
emCommand"
DataKeyField="" AutoGenerateColumns="False
" AllowSorting="True">
<FooterStyle backcolor="#C6C3C6"></Foot
erStyle>
<PagerStyle mode="NumericPages" backcolor="#C6C3C6" font-size="Smaller" horizontalalign="Right"></
PagerStyle
>
<AlternatingItemStyle backcolor="PaleGoldenrod">
</Alternat
ingItemSty
le>
<ItemStyle backcolor="Beige"></ItemSt
yle>
<HeaderStyle backcolor="#4A3C8C" forecolor="White" font-bold="True"></HeaderS
tyle>
<Columns>
<asp:BoundColumn DataField="FullName" HeaderText="Full Name"></asp:BoundColumn>
<asp:BoundColumn DataField="GroupName" HeaderText="Group Name"></asp:BoundColumn>
<asp:EditCommandColumn CancelText="Cancel" UpdateText="Update" EditText="Edit">
<ItemStyle width="10%" font-size="Smaller"></Item
Style>
</asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" Text="Delete">
<ItemStyle width="10%" font-size="Smaller"></Item
Style>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>
<br />
<asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Font-Size="smaller" Text="Add new item"></asp:LinkButton>
<br />
<br />
<asp:Label id="Message" runat="server" width="80%" enableviewstate="false" forecolor="red"></asp:Labe
l>
</form>
</body>
</html>