根据DropDownList.SelectedValue和Button Click填充GridView

时间:2016-03-18 06:15:39

标签: asp.net vb.net

从早上起就挣扎着。方案如下: 我有一个ASP.NET网页

  • 两个下拉列表
  • 两个按钮
  • 一个GridView 想要根据下拉列表选择的值填充gridview 这是.aspx代码



<%@ Page Title="" Language="VB" MasterPageFile="~/Admin/ADMIN.master" AutoEventWireup="false" CodeFile="EditSTUdetail.aspx.vb" Inherits="Admin_EditSTUdetail" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <style type="text/css">
        .auto-style1
        {
            height: 42px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div align="center">
        <fieldset>
            <legend style="font-size: 16px;">SEARCH / EDIT / DELETE STUDENT RECORDS
            </legend>
            <div style="overflow: auto">
                <div>
                    <asp:Label ID="lblMsg" runat="server" CssClass="lblresponse" />
                    <table style="margin: 3px auto 1px auto; height: 72px;">
                        <tr>
                            <td style="text-align: right;" class="auto-style2">Select Session/सत्र का चयन करें :
                            </td>
                            <td class="auto-style3">
                                <asp:DropDownList ID="ddlSession" runat="server" AppendDataBoundItems="True" Width="236px" Height="28px">
                                    <asp:ListItem Text="--Select Session--" Value=""></asp:ListItem>
                                    <asp:ListItem Text="" Value=""></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="ddlSession" ForeColor="#990000"></asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td style="text-align: right;" class="auto-style1">Select Course /कक्षा का चयन करें :
                            </td>
                            <td>
                                <asp:DropDownList ID="ddlCourse" runat="server" AppendDataBoundItems="true" Width="236px" Height="28px">
                                    <asp:ListItem Text="--Select Course--" Value=""></asp:ListItem>
                                    <asp:ListItem Text="" Value=""></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="ddlCourse" ForeColor="#990000"></asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td class="auto-style1"></td>
                            <td class="auto-style1">
                                <asp:Button ID="btnSearch" runat="server" Text="SEARCH" CssClass="button" OnClick="btnSearch_Click" Height="34px" Width="77px" /></td>
                            <td colspan="2" class="auto-style1">&nbsp;&nbsp;
                           <asp:Button ID="btnRefresh" runat="server" Text="REFRESH" CssClass="button" OnClick="btnRefresh_Click" Height="34px" Width="77px" /></td>
                        </tr>
                    </table>
                </div>
            </div>
            <asp:GridView ID="GVdata" runat="server" Width="674px" CaptionAlign="Top"
                AutoGenerateColumns="False" Height="100px" BackColor="White"
                BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
                CellSpacing="2" GridLines="Vertical"
                EmptyDataText="There Are No Record Found" OnRowCancelingEdit="gvManageOrders_RowCancelingEdit"
                OnRowDeleting="gvManageOrders_RowDeleting" OnRowEditing="gvManageOrders_RowEditing"
                OnRowUpdating="gvManageOrders_RowUpdating">
                <AlternatingRowStyle BackColor="#DCDCDC" />
                <Columns>

                    <asp:TemplateField HeaderText="Sr. No." ItemStyle-Width="20">
                        <ItemTemplate>
                            <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField HeaderText="Edit/Remove" ShowDeleteButton="True"
                        ShowEditButton="True" DeleteText="Remove" />
                    <asp:TemplateField HeaderText="Student ID" Visible="True">
                        <ItemTemplate>
                            <asp:Label ID="studentID" runat="server" Text='<%# Bind("studentID")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Student Name" Visible="True">
                        <ItemTemplate>
                            <asp:Label ID="Sname" runat="server" Text='<%# Bind("Sname")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Father's Name" Visible="True">
                        <ItemTemplate>
                            <asp:Label ID="Fname" runat="server" Text='<%# Bind("Fname")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataRowStyle BackColor="#eeeeee" BorderColor="Black"
                    BorderStyle="Solid" BorderWidth="1px" Font-Size="Large" ForeColor="#851010"
                    HorizontalAlign="Center" />
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <HeaderStyle BackColor="#851010" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#851010" ForeColor="Black" HorizontalAlign="Center" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#0000A9" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#000065" />
            </asp:GridView>
        </fieldset>

    </div>
</asp:Content>
&#13;
&#13;
&#13;

这是我的vb代码:

    Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Net.Mail
Imports System.Data
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Partial Public Class Admin_EditSTUdetail
    Inherits System.Web.UI.Page
    Private myds As DataSet
    Protected Sub LoadSession()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("cmsDB").ConnectionString)
        con.Open()
        Dim com As New SqlCommand("select sessionID, session from tblcategories ORDER by session DESC", con)
        Dim da As New SqlDataAdapter(com)
        Dim ds As New DataSet()
        da.Fill(ds)
        ddlSession.DataTextField = ds.Tables(0).Columns("session").ToString()
        ' text field name of table dispalyed in dropdown
        ddlSession.DataValueField = ds.Tables(0).Columns("sessionID").ToString()
        ' to retrive specific  textfield name 
        ddlSession.DataSource = ds.Tables(0)
        'assigning datasource to the dropdownlist
        ddlSession.DataBind()
        ddlSession.SelectedIndex = -1
        'binding dropdownlist
    End Sub
    Protected Sub LoadCourse()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("cmsDB").ConnectionString)
        con.Open()
        Dim com As New SqlCommand("SELECT DISTINCT course AS mycolumn FROM " _
                                     & " tblsubjects where course is not null and " _
                                     & "not course = '' order by mycolumn", con)
        Dim da As New SqlDataAdapter(com)
        Dim ds As New DataSet()
        da.Fill(ds)
        ddlCourse.DataTextField = ds.Tables(0).Columns("mycolumn").ToString()
        ' text field name of table dispalyed in dropdown
        ddlCourse.DataValueField = ds.Tables(0).Columns("mycolumn").ToString()
        ' to retrive specific  textfield name 
        ddlCourse.DataSource = ds.Tables(0)
        'assigning datasource to the dropdownlist
        ddlCourse.DataBind()
        ddlCourse.SelectedIndex = -1
        'binding dropdownlist
    End Sub


    Protected Sub fillgrid()
        Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("cmsDB").ConnectionString)
        Dim cmd As New SqlCommand("SELECT StudentID,session,course,Sname,Fname FROM [tblstudetail]  WHERE " _
                              & " [session] = '" + ddlSession.SelectedValue.ToString() + "' AND " _
                              & " [course] = '" + ddlCourse.SelectedValue.ToString() + "'", cn)

        cn.Open()
        Dim da As New SqlDataAdapter(cmd)
        myds = New DataSet()
        da.Fill(myds)
        GVdata.DataSource = myds
        GVdata.DataBind()

    End Sub

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            LoadSession()
            LoadCourse()
            fillgrid()
        End If

    End Sub


    Protected Sub gvManageOrders_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
        Throw New NotImplementedException
    End Sub

    Protected Sub gvManageOrders_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
        Throw New NotImplementedException
    End Sub

    Protected Sub gvManageOrders_RowEditing(sender As Object, e As GridViewEditEventArgs)
        Throw New NotImplementedException
    End Sub

    Protected Sub gvManageOrders_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
        Throw New NotImplementedException
    End Sub

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        If Not Page.IsPostBack Then
           fillgrid()
        End If
    End Sub

    Protected Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GVdata.DataSource = Nothing
    End Sub
End Class

但它没有归还我的要求。 请帮助我....

2 个答案:

答案 0 :(得分:0)

请删除这些行,

ddlSession.SelectedIndex = -1 ddlCourse.SelectedIndex = -1LoadCourseLoadSession方法的末尾开始。这些正在重置下拉值并导致问题。

流程在您的代码中搞砸了。您正在从页面加载方法中串行调用loadsession,loadcourses和fillgrid。想一想。当fillgrid中的代码执行时,下拉列表中没有选定的值。显然没有找到数据,所以gridview不可见。

更好的处理方法是引入两种新方法,

 Protected Sub ddlCourses_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlSession.SelectedIndexChanged


Protected Sub ddlAC3_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCourse.SelectedIndexChanged

在这些方法中发出对fillgrid的调用。 同样在fillgrid方法内部首先验证ddlCourse和ddlSession下拉列表中是否有一些值,然后发出数据库命令。

答案 1 :(得分:0)

首先

ddlSession.SelectedValue.ToString() 

成为

ddlSession.SelectedValue

然后在Protected Sub中btnSearch_Click删除

 If Not Page.IsPostBack Then

然后

ddlCourse.SelectedIndex = -1

成为

ddlCourse.ClearSelection();

最后,你确定数据库中没有NULL值吗?