zoukankan      html  css  js  c++  java
  • Adding a DataDependent DropDownList to the DataGrid's Footer

    <% @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.FAQCategoryID, F.Description, FC.Name AS CategoryName FROM tblFAQ F INNER JOIN tblFAQCategory FC ON F.FAQCategoryID = FC.FAQCategoryID 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
    Function GetCategories() as DataSet
    'Populate the ddlDataSet
    Const strSQLDDL as String = _
    "SELECT FAQCategoryID, Name FROM tblFAQCategory ORDER BY Name"
    Dim myDataAdapter as SqlDataAdapter = New _
    SqlDataAdapter(strSQLDDL, myConnection)
    myDataAdapter.Fill(ddlDataSet, "Categories")
    Return ddlDataSet
    End Function
    Function GetSelectedIndex(CID as String) as Integer
    Dim iLoop as Integer
    Dim dt as DataTable = ddlDataSet.Tables("Categories")
    For iLoop = 0 to dt.Rows.Count - 1
    If Int32.Parse(CID) = Int32.Parse(dt.Rows(iLoop)("FAQCategoryID")) then
    Return iLoop
    End If
    Next iLoop
    End Function
    Sub dgPopFAQs_Edit(sender As Object, e As DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = e.Item.ItemIndex
    BindData()
    dgPopularFAQs.ShowFooter = False
    End Sub
    Sub dgPopFAQs_Cancel(sender As Object, e As DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = -1
    BindData()
    dgPopularFAQs.ShowFooter = True
    End Sub
    Sub dgPopFAQs_Update(sender As Object, e As DataGridCommandEventArgs)
    Response.Write("<b>The Update Command has no effect on this live demo...</b>")
    dgPopularFAQs.EditItemIndex = -1
    BindData()
    End Sub
    Sub dgPopFAQs_Insert(sender as Object, e As DataGridCommandEventArgs)
    If e.CommandName = "Insert" then
    Dim txtNewDescription as TextBox = e.Item.FindControl("txtNewDescription")
    Dim lstCategoriesInsert as DropDownList = e.Item.FindControl("lstCategoriesInsert")
    Response.Write("<b>The Insert Command has no effect on this live demo...</b><br />")
    Response.Write("<b>The data you would have inserted, though, was:<ul>")
    Response.Write("<li>" & lstCategoriesInsert.SelectedItem.Text & "</li>")
    Response.Write("<li>" & txtNewDescription.Text & "</li></ul></b>")
    End If
    End Sub
    </script>
    <form runat="server">
    <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"
    OnEditCommand="dgPopFAQs_Edit"
    OnCancelCommand="dgPopFAQs_Cancel"
    OnUpdateCommand="dgPopFAQs_Update"
    OnItemCommand="dgPopFAQs_Insert"
    ShowFooter="True">
    <Columns>
    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="OK" />
    <asp:TemplateColumn ItemStyle-Width="10%"
    ItemStyle-HorizontalAlign="Center" HeaderText="FAQ ID"
    FooterStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <%# Container.DataItem("FAQID") %>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button Text="Add" CommandName="Insert" runat="server" />
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Category">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList runat="server" id="lstCategories"
    DataValueField="FAQCategoryID"  DataTextField="Name"
    DataSource='<%# GetCategories() %>'
    SelectedIndex='<%# GetSelectedIndex(Container.DataItem("FAQCategoryID")) %>' />
    </EditItemTemplate>
    <FooterTemplate>
    <asp:DropDownList runat="server" id="lstCategoriesInsert"
    DataValueField="FAQCategoryID"  DataTextField="Name"
    DataSource='<%# GetCategories() %>' />
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="FAQ Question">
    <ItemTemplate>
    <%# Container.DataItem("Description") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" Text='<%# Container.DataItem("Description") %>'
    Columns="80" />
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox id="txtNewDescription" runat="server" Columns="80" />
    </FooterTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
    </form>
    
  • 相关阅读:
    多任务并行
    不同方法来移动数据库(更改磁盘时用)(转)
    ASP.NET执行SQL超时的解决方案
    SQL Server 简单模式下,误删除堆表记录如何恢复(绕过页眉校验) (转)
    关于跟踪标记
    .NET分布式事务处理
    安装SQL Server 2005时出现COM+的警告/错误,下面是微软给出的解决方法。
    PHP中如何获取多个checkbox的值
    C# 模拟post数据提交时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
    winform出现"LC.exe"已退出,代码为1
  • 原文地址:https://www.cnblogs.com/hhq80/p/662199.html
Copyright © 2011-2022 走看看