需要在不实时使用数据库的情况下在按钮单击上选择 2 个下拉菜单后,在下面的 GridView 中显示数据

问题描述

请帮助显示我们在 DropDownList 中选择的数据将立即填充 GridView 行。

<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <!-- General meta information -->
    <title>Menu</title>
    <%--    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="robots" content="index,follow" />
    <meta charset="utf-8" />--%>
    <!-- // General meta information -->


    <!-- Load Javascript -->
    <script type="text/javascript" src="./js/jquery.js"></script>
    <script type="text/javascript" src="./js/jquery.query-2.1.7.js"></script>
    <script type="text/javascript" src="./js/rainbows.js"></script>
    <!-- // Load Javascipt -->

    <!-- Load stylesheets -->
    <link type="text/css" rel="stylesheet" href="css/style.css" media="screen" />
    <%--  <style>
    input[type='text']::placeholder
    {   
        text-align: center;      /* for Chrome,Firefox,Opera */
    }
    :-ms-input-placeholder
    { 
        text-align: center;      /* for IE 10-11 */
    }
    ::-webkit-input-placeholder
    { 
        text-align: center;      /* for IE Edge */
    }
</style>--%>
    <!-- // Load stylesheets -->
    <style>
        .mydropdownlist {
            color: #fff;
            font-size: 20px;
            padding: 5px 10px;
            border-radius: 5px;
            background-color: #cc2a41;
            font-weight: bold;
        }

        .mydropdownlist1 {
            color: #fff;
            font-size: 20px;
            padding: 5px 10px;
            border-radius: 5px 12px;
            background-color: #292929;
            font-weight: bold;
        }
    </style>
   
</head>

<body>

    <div id="wrapper">
        <form id="Form1" runat="server">
            <div>
                <div style="float:left;">
                    <asp:DropDownList ID="DropDownList1" runat="server" WatermarkText="Select a car" Width="300%" Height="30px" AutoPostBack="false" OnSelectedIndexChanged="ddl_onSelectIndexChanged">
                        <Items>
                            <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                            <asp:ListItem Text="Haldi" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Jeera" Value="2"></asp:ListItem>
                            <asp:ListItem Text="Amchoor" Value="3"></asp:ListItem>
                            <asp:ListItem Text="Ajwain" Value="4"></asp:ListItem>
                            <asp:ListItem Text="Dhaniya" Value="5"></asp:ListItem>
                            <asp:ListItem Text="Kasturi Methi" Value="6"></asp:ListItem>
                            <asp:ListItem Text="Mirch Pisi" Value="7"></asp:ListItem>
                            <asp:ListItem Text="Mirch Kuti" Value="8"></asp:ListItem>
                            <asp:ListItem Text="Kashmiri Mirch" Value="9"></asp:ListItem>
                            <asp:ListItem Text="Mirch Pili" Value="10"></asp:ListItem>
                            <asp:ListItem Text="Garam Masala" Value="11"></asp:ListItem>
                            <asp:ListItem Text="Chana Masala" Value="12"></asp:ListItem>
                            <asp:ListItem Text="Kitchen Kind" Value="13"></asp:ListItem>
                            <asp:ListItem Text="Dal Masala" Value="14"></asp:ListItem>
                            <asp:ListItem Text="Chat Masala" Value="15"></asp:ListItem>
                            <asp:ListItem Text="Sambhar Masala" Value="16"></asp:ListItem>
                            <asp:ListItem Text="Chhole Masala" Value="17"></asp:ListItem>
                            <asp:ListItem Text="Shahi Paneer Masala" Value="18"></asp:ListItem>
                            <asp:ListItem Text="Black Salt" Value="19"></asp:ListItem>
                            <asp:ListItem Text="Sendha Salt" Value="20"></asp:ListItem>
                            <asp:ListItem Text="Sonth Pisi" Value="21"></asp:ListItem>
                            <asp:ListItem Text="Tej Patta" Value="22"></asp:ListItem>
                            <asp:ListItem Text="Loung" Value="23"></asp:ListItem>
                            <asp:ListItem Text="Kali Mirch Sabut" Value="24"></asp:ListItem>
                            <asp:ListItem Text="Kali Mirch Pisi" Value="25"></asp:ListItem>
                            <asp:ListItem Text="Kalonji" Value="26"></asp:ListItem>
                            <asp:ListItem Text="Elaichi Badi" Value="27"></asp:ListItem>
                            <asp:ListItem Text="Elaichi Chhoti" Value="28"></asp:ListItem>
                            <asp:ListItem Text="Hing" Value="29"></asp:ListItem>
                            <asp:ListItem Text="Dalchini" Value="30"></asp:ListItem>
                            <asp:ListItem Text="Javitri" Value="31"></asp:ListItem>
                            <asp:ListItem Text="Jaifal" Value="32"></asp:ListItem>
                            <asp:ListItem Text="Postdana" Value="33"></asp:ListItem>
                            <asp:ListItem Text="Ajinomoto" Value="34"></asp:ListItem>
                            <asp:ListItem Text="Safed Mirch" Value="35"></asp:ListItem>
                            <asp:ListItem Text="Imli" Value="36"></asp:ListItem>
                            <asp:ListItem Text="Methi Dana" Value="37"></asp:ListItem>
                            <asp:ListItem Text="Raee" Value="38"></asp:ListItem>
                            <asp:ListItem Text="Saunf" Value="39"></asp:ListItem>
                            <asp:ListItem Text="Besan" Value="40"></asp:ListItem>
                            <asp:ListItem Text="Jeera Pisa" Value="41"></asp:ListItem>
                            <asp:ListItem Text="Pasta" Value="42"></asp:ListItem>
                            <asp:ListItem Text="Tarbuj ke Beej" Value="43"></asp:ListItem>
                            <asp:ListItem Text="Sabut Lal Mirch" Value="44"></asp:ListItem>
                            <asp:ListItem Text="Sabut Dhaniya" Value="45"></asp:ListItem>
                            <asp:ListItem Text="Moong Dal Mangodi" Value="46"></asp:ListItem>
                            <asp:ListItem Text="Sabudana" Value="47"></asp:ListItem>
                            <asp:ListItem Text="Supari" Value="48"></asp:ListItem>
                            <asp:ListItem Text="Star Flower" Value="49"></asp:ListItem>
                            <asp:ListItem Text="Til" Value="50"></asp:ListItem>
                            <asp:ListItem Text="Shahi Jeera" Value="51"></asp:ListItem>
                            <asp:ListItem Text="Anardana" Value="52"></asp:ListItem>
                        </Items>
                    </asp:DropDownList>
                </div>
                <div style="float:left; margin-left:350px;">
                    <asp:DropDownList ID="DropDownList2" runat="server" WatermarkText="Select a car" Width="300%" Height="30px" EnableViewState="true">
                        <Items>
                            <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                            <asp:ListItem Text="10g" Value="1"></asp:ListItem>
                            <asp:ListItem Text="20g" Value="2"></asp:ListItem>
                            <asp:ListItem Text="50g" Value="3"></asp:ListItem>
                            <asp:ListItem Text="100g" Value="4"></asp:ListItem>
                            <asp:ListItem Text="200g" Value="5"></asp:ListItem>
                            <asp:ListItem Text="300g" Value="6"></asp:ListItem>
                            <asp:ListItem Text="400g" Value="7"></asp:ListItem>
                            <asp:ListItem Text="500g" Value="8"></asp:ListItem>
                            <asp:ListItem Text="600g" Value="9"></asp:ListItem>
                            <asp:ListItem Text="700g" Value="10"></asp:ListItem>
                            <asp:ListItem Text="800g" Value="11"></asp:ListItem>
                            <asp:ListItem Text="900g" Value="12"></asp:ListItem>
                            <asp:ListItem Text="1kg" Value="13"></asp:ListItem>
                            <asp:ListItem Text="2kg" Value="14"></asp:ListItem>
                            <asp:ListItem Text="3kg" Value="15"></asp:ListItem>
                            <asp:ListItem Text="4kg" Value="16"></asp:ListItem>
                            <asp:ListItem Text="5kg" Value="17"></asp:ListItem>
                            <asp:ListItem Text="10kg" Value="18"></asp:ListItem>
                        </Items>
                    </asp:DropDownList>
                </div>
                <div style="float:left; margin-left:300px;">
                    <asp:Button ID="Button" runat="server" Width="200%" Height="40px" Text="Submit" BackColor="BlanchedAlmond" OnClick="SubmitButton_Click">
                    </asp:Button>
                </div>
            </div>
            <div>
                 <asp:Panel ID="pnlGrid" CssClass="panel" runat="server" CellPadding="20" CellSpacing="25">
            <div style="text-align: center">

                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true" AllowPaging="false" AllowSorting="True" CssClass="gridview" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
                    OnPageIndexChanging="OnPageIndexChanging" PageSize="10" CellPadding="4" ForeColor="#333333" OnRowDataBound="GridView1_RowDataBound" GridLines="Vertical">
                    <Columns>
                        <%--<asp:BoundField ItemStyle-Width="150px" ItemStyle-Height="40px" HeaderText="Masala Name" />
                        <asp:BoundField ItemStyle-Width="100px" HeaderText="Quantity" />
                        <asp:BoundField ItemStyle-Width="100px" HeaderText="Prize" />--%>
                        <asp:TemplateField HeaderText="S.NO."><ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate></asp:TemplateField>
                    </Columns>
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <HeaderStyle CssClass="gridViewHeader" />
                    <RowStyle CssClass="gridViewRow" />
                    <AlternatingRowStyle CssClass="gridViewAltRow" />
                    <SelectedRowStyle CssClass="gridViewSelectedRow" />
                    <PagerStyle CssClass="gridViewPager" />
                </asp:GridView>

            </div>
        </asp:Panel>
            </div>
            <asp:Label ID="lblRowNumber" runat="server"></asp:Label>
        </form>
    </div>
</body>
</html>

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

隐藏代码:在此我尝试了一个简单的代码来绑定下拉数据,因此在提交下拉选择后第一行出现了,但之后如果我想要从下拉列表中进行另一个选择并提交,那么它不会占用另一行。

using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MasalaStore
{
    public partial class Menu2 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
        protected void Page_Load(object sender,EventArgs e)
        {

        }

        protected void SubmitButton_Click(object sender,EventArgs e)
        {
            BindGrid();
        }
        private void BindGrid()
        {
            //if (DropDownList1.Text != null && DropDownList2.Text != null)
            //{

            DataTable dt = new DataTable();
            //dt.Columns.Add("S.No.",typeof(Int32));
            dt.Columns.Add("ITEM",typeof(string));
            dt.Columns.Add("QUANTITY",typeof(string));
            dt.Columns.Add("PRIZE",typeof(string));

            DataRow dtrow = dt.NewRow();    // Create New Row and Bind Data to Columns
            dtrow["Item"] = DropDownList1.SelectedItem;
            dtrow["Quantity"] = DropDownList2.SelectedItem;
            dtrow["Prize"] = "#";
            dt.Rows.Add(dtrow);

            //if (dt.Rows.Count > 0)
            //{
                GridView2.DataSource = dt;
                GridView2.DataBind();
                DropDownList1.ClearSelection();
                DropDownList2.ClearSelection();
            //}
        }

        protected void OnPageIndexChanging(object sender,GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }

        protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.Cells[1].Text).ToUpper() == "OUT OF STOCK")
                {
                    // CHANGE THE COLOR OF THE CELL.
                    e.Row.Cells[1].BackColor = System.Drawing.Color.Crimson;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.WhiteSmoke;
                }
            }
        }

        protected void ddl_onSelectIndexChanged(object sender,System.EventArgs e)
        {

        }

    }
}

Image for a rough Idea

解决方法

好的,那么我们就可以使用这段代码了:

点击提交将添加该行,清除两个下拉菜单。

代码如下:

注意非常接近我们在表单类级别声明 MyTable 的方式。

private DataTable MyTable = new DataTable();

public void Page_Load(object sender,System.EventArgs e)
{
    if (IsPostBack == false)
    {
        SetUpGrid();
        ViewState["MyTable"] = MyTable;
    }
    else
        MyTable = ViewState["MyTable"];
}


public void SetUpGrid()
{
    MyTable.Columns.Add("ITEM",typeof(string));
    MyTable.Columns.Add("QUANTITY",typeof(Int32));
    MyTable.Columns.Add("PRIZE",typeof(Int32));
}

protected void Button_Click(object sender,EventArgs e)
{

    // add row to table

    DataRow OneRow = MyTable.Rows.Add;

    OneRow("ITEM") = DropDownList1.SelectedItem.Text;
    OneRow("QUANTITY") = 1;
    OneRow("PRIZE") = DropDownList2.SelectedItem.Value;

    GridView2.DataSource = MyTable;
    GridView2.DataBind();

    DropDownList1.ClearSelection();
    DropDownList2.ClearSelection();
}
}

所以这里的技巧是在第一页加载时:

设置表格。

但是,我们必须保存这个表的状态。这使得添加一行变得容易。每次添加新行时,我们都会重新绑定网格,清除下拉列表。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...