<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
'Create a connection
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim ddlDataSet as DataSet = New DataSet()
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub
Sub BindData()
'2. Create the command object, passing in the SQL string
Const strSQL as String = "SELECT FAQID, F.Description FROM tblFAQ F WHERE FAQID <= 25"
'Set the datagrid's datasource to the datareader and databind
Dim resultsDataSet as New DataSet()
Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
myDataAdapter.Fill(resultsDataSet)
dgPopularFAQs.DataSource = resultsDataSet
dgPopularFAQs.DataBind()
End Sub
Sub dgPopularFAQs_Edit(sender as Object, e as DataGridCommandEventArgs)
dgPopularFAQs.EditItemIndex = e.Item.ItemIndex
BindData()
' Create a reference to the TextBox
Dim descTB as TextBox
descTB = dgPopularFAQs.Items(e.Item.ItemIndex).Cells(2).FindControl("txtDesc")
'Set the script to focus and select the TextBox
RegisterStartupScript("focus", "<script language=""JavaScript"">" & vbCrLf & _
vbTab & "frmEditFAQs." & descTB.ClientID & ".focus();" & _
vbCrLf & vbTab & "frmEditFAQs." & descTB.ClientID & ".select();" & _
vbCrLf & "<" & "/script>")
End Sub
Sub dgPopularFAQs_Cancel(sender as Object, e as DataGridCommandEventArgs)
dgPopularFAQs.EditItemIndex = -1
BindData()
End Sub
Sub dgPopularFAQs_Update(sender as Object, e as DataGridCommandEventArgs)
' Create a reference to the TextBox
Dim descTB as TextBox
descTB = dgPopularFAQs.Items(e.Item.ItemIndex).Cells(2).FindControl("txtDesc")
'Display what would have happened in the lblResults label
lblResults.Text = "You opted to update the Description field to: " & descTB.Text
dgPopularFAQs.EditItemIndex = -1
BindData()
End Sub
</script>
<form runat="server" id="frmEditFAQs">
<asp:label id="lblResults" Font-Bold="True" runat="server"
Font-Italic="True" ForeColor="#aa0000" />
<p>
<asp:datagrid id="dgPopularFAQs" runat="server"
AutoGenerateColumns="False"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="Red"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
HeaderStyle-Font-Name="Verdana"
AlternatingItemStyle-BackColor="#dddddd"
ItemStyle-Font-Name="Verdana"
DataKeyField="FAQID"
OnEditCommand="dgPopularFAQs_Edit"
OnUpdateCommand="dgPopularFAQs_Update"
OnCancelCommand="dgPopularFAQs_Cancel">
<Columns>
<asp:EditCommandColumn EditText="Edit" UpdateText="Update" CancelText="Cancel" />
<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID"
ItemStyle-HorizontalAlign="Center" ReadOnly="True" />
<asp:TemplateColumn HeaderText="Question">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtDesc" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>'
Width="95%" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</form>