在页面加载上显示网格视图

时间:2013-03-22 10:00:16

标签: c# gridview webforms

我有一个网格视图,其中存储过程在SQL中设置。我有三个控件2 dropdown list's和一个texbox与jQuery日期选择器。我的问题是gridview没有显示在页面加载。但是,当我开始添加控件时,会突然显示gridview。从调试开始,我推测带有jQuery datepicker的文本框没有将NULL值传递给指定的存储过程,尽管我仍然想知道它是否是原因。这是代码..

Aspx代码

<div class="datarange">
        <asp:DropDownList ID="categoryDDL" AutoPostBack="true" AppendDataBoundItems="true" runat="server">
            <asp:ListItem Text="Select Category" Value=" " />
        </asp:DropDownList>
        <asp:DropDownList ID="brokerDDL" AutoPostBack="true" runat="server"></asp:DropDownList>
        <asp:TextBox ID="openDate" AutoPostBack="true" runat="server"></asp:TextBox>

    </div>
    <br />
    <%-- SQL DATA SOURCE PARAMETERS --%>
    <asp:SqlDataSource ID="SqlRAListings" runat="server" ConnectionString="<%$ ConnectionStrings:kmc_SalesPipelineConnectionString %>" SelectCommand="RecentlyAddedListings" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="categoryDDL" Name="Category" PropertyName="SelectedValue" Type="String" DefaultValue=" " />
            <asp:ControlParameter ControlID="brokerDDL" Name="Broker" PropertyName="SelectedValue" Type="String" DefaultValue=" Select Employee" />
            <asp:ControlParameter ControlID="openDate" Name="OpenDate" PropertyName="Text" Type="DateTime" DefaultValue="" />
        </SelectParameters>
    </asp:SqlDataSource>
    <%-- END OF SQL DATA SOURCE PARAMETERS --%>


    <%-- GRIDVIEW FOR DISPLAYING RECENTLY ADDED LISTINGS --%>
    <asp:GridView ID="ralGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" OnSelectedIndexChanged="ralGridView_SelectedIndexChanged">
        <Columns>
            <asp:TemplateField HeaderText="Opportunity">
                <ItemTemplate>
                    <a href="/management/opportunity.aspx?id=<%# Eval("ID") %>" target="_blank">
                        <%# Eval("Opportunity").ToString() != "" ? Eval("Opportunity") : "Opportunity" %>
                    </a>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ID" HeaderText="ID"
                SortExpression="ID" InsertVisible="False" ReadOnly="True" />
            <asp:BoundField DataField="Category" HeaderText="Category"
                SortExpression="Category" />
            <asp:BoundField DataField="Contact Name" HeaderText="Contact Name"
                SortExpression="Contact Name" />
            <asp:BoundField DataField="Employee" HeaderText="Employee" SortExpression="Employee" />
            <asp:BoundField DataField="Open Date" HeaderText="Open Date" SortExpression="Open Date" />
            <asp:TemplateField HeaderText="Opportunity from">
                <ItemTemplate>
                    <%# Eval("LeadID").ToString().Length > 0 == true ? "Lead System" : "Personal Lead" %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDbRAListings" runat="server" ConnectionString="<%$ ConnectionStrings:kmc_SalesPipelineConnectionString %>" SelectCommand="RecentlyAddedListings" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="categoryDDL" Name="Category" PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="brokerDDL" Name="Broker" PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="openDate" Name="OpenDate" PropertyName="Text" Type="DateTime" />
        </SelectParameters>
    </asp:SqlDataSource>
    <%-- END OF GRID VIEW --%>
</form>

<script type="text/javascript">
    $(function () {
        var dates = $("#openDate").datepicker({
            defaultDate: "+1w",
            changeMonth: false,
            numberOfMonths: 1
        });
    });

</script>

C#代码

    namespace KMCWebLMS
{
    public partial class RecentlyAssignedLead : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fillCategoryDropDown();
                fillBrokerDropDown();   
            }
            else
            {
                ralGridView.DataSource = SqlDbRAListings;
                ralGridView.DataBind();
            }
        }

        public void fillCategoryDropDown()
        {
            DataTable categories = new DataTable();
            string command = @"SELECT LeadCategory FROM LeadCategory ORDER BY LeadCategory";
            ConnectSQL cmd = new ConnectSQL();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd.configureSQL(command));
            adapter.Fill(categories);

            try
            {
                categoryDDL.DataSource = categories;
                categoryDDL.DataTextField = "LeadCategory";
                categoryDDL.DataValueField = "LeadCategory";
                categoryDDL.DataBind();
            }
            catch
            {

            }
        }

        public void fillBrokerDropDown()
        {
            DataTable employees = new DataTable();
            string command = @"SELECT TOP 100 [Emp_Name] FROM [kmc_SalesPipeline].[dbo].[vwEmployees] ORDER BY Emp_Name";
            ConnectSQL cmd = new ConnectSQL();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd.configureSQL(command));
            adapter.Fill(employees);
            try
            {
                brokerDDL.DataSource = employees;
                brokerDDL.DataTextField = "Emp_Name";
                brokerDDL.DataValueField = "Emp_Name";
                brokerDDL.DataBind();
            }
            catch
            {

            }
        }

        protected void ralGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

2 个答案:

答案 0 :(得分:0)

但是从您的代码背后,似乎您只在回发中填充gridview 对于PageLoad,您只填充下拉列表。

这段代码不应该来

ralGridView.DataSource = SqlDbRAListings;
                ralGridView.DataBind();

 if (!Page.IsPostBack)

答案 1 :(得分:0)

我找到了问题的解决方案。为此,我在Code Behind中声明了存储过程所需的参数,并相应地将其设置在那里。

//STORED PROCEDURE PARAMETERS
cmd.Parameters.AddWithValue("@OpenDate", OpenDate);
cmd.Parameters.AddWithValue("@Broker", broker);
cmd.Parameters.AddWithValue("@Category", category);

程序将在page_load上显示默认值,显示gridview中的所有数据。在回发时,指定的参数将相应地设置为当前存储在特定控制参数中的值。

broker = brokerDDL.SelectedValue;
category = categoryDDL.SelectedValue;
ralGridView.DataSource = CreateRecentlyAddedTable();
ralGridView.DataBind();