[英]Need to display data in GridView below after Selection of 2 DropDowns on their Button Click without using database in real time
Please help to Display Data which we are selecting in DropDownList will Fill in the GridView Row Instantly.请帮助显示我们在 DropDownList 中选择的数据将立即填写 GridView 行。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Menu2.aspx.cs" Inherits="MasalaStore.Menu2" %> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Menu2.aspx.cs" Inherits="MasalaStore.Menu2" %>
<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>
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
CODE BEHIND: In this i tried a simple code to bind Dropdown data so then First Row has come after submitting the selection of Dropdowns but after that if i want another selection from dropdowns and submitting then it is not taking another Row.后面的代码:在此我尝试了一个简单的代码来绑定下拉数据,因此在提交下拉列表的选择之后,第一行已经到来,但在那之后,如果我想从下拉列表中选择另一个选择并提交,那么它不会占用另一行。
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)
{
}
}
}
Ok, then we can use this code:好的,那么我们可以使用这段代码:
Hitting submit will add the row, clear the two drop downs.点击提交将添加行,清除两个下拉菜单。
The code will look like this:代码将如下所示:
NOTE VERY close how we declared the MyTable at the forms class level.注意非常接近我们在 forms class 级别声明 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();
}
}
so the trick here is on first page load:所以这里的诀窍是在第一页加载:
Setup the table.设置表。
But, we have to SAVE the state of this table.但是,我们必须保存这张表的 state。 This makes it all easy to add a row.
这使得添加一行变得很容易。 And each time we add a new row, we re-bind grid, clear the dropdowns.
每次添加新行时,我们都会重新绑定网格,清除下拉菜单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.