我的第二个下拉列表是选择财政年度与它相关的年份,当我尝试选择像 2011-2012 这样的年份时,它没有在按钮单击时显示其值,而是将我的选择更改为 2010-2011 的第一个财政年度,如果发生同样的情况我从我的第一个下拉列表中选择任何其他项目代码我能够选择第二个下拉列表中的第一个值而不是第二个项目?.....[发生的事情是我的第二个下拉列表显示链接到 my1st 下拉列表的值不允许我选择它的第二个值并提交,如果我选择第二个值,它会自动将其更改为第一个值并显示结果?]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (Page.IsPostBack == false)
{
string query = "select * from project";
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataValueField = ds.Tables[0].Columns[0].ToString();
DropDownList1.DataTextField = ds.Tables[0].Columns[1].ToString();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "--Select--");
cmd.Dispose();
}
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
try
{
string query1 = "Select * from yearly where pid = " + DropDownList1.SelectedItem.Value.ToString();
SqlCommand cmd = new SqlCommand(query1, con);
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
DropDownList2.DataSource = ds;
DropDownList2.DataValueField = ds.Tables[0].Columns[0].ToString();
DropDownList2.DataTextField = ds.Tables[0].Columns[1].ToString();
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "--Select--");
cmd.Dispose();
}
catch (Exception)
{
}
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("select pcode,fyyear,date,salary,ta,contigency,nrc,institcharges,others from monthly where pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND fyyear=('" + DropDownList2.SelectedItem.ToString() + "')",con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
i am also attaching aspx code:
<%@ Page Title="" Language="C#" MasterPageFile="~/grid.master" AutoEventWireup="true" CodeFile="yearwiseex.aspx.cs" Inherits="Default2" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="Select Project Code"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"
Height="20px" Width="130px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True"> </asp:DropDownList>
<asp:Label ID="Label2" runat="server" Text="Select Financial Year"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server"
Height="20px" Width="130px"></asp:DropDownList>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
<br />
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" style="margin-left: 82px"
Width="90%" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
<br />
<br />
</asp:Content>