Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the value of drop downlist4 is not changing when i select other.It is getting only first record from table which satisfied first condition.The drop down list is linked with dropdownlist 3.

My HTML part and CS part is given below .


HTML part

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Selection4go.aspx.cs" Inherits="selection4go" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
</head>
<body>
    <form id="form1"  runat="server">
    <div align="center">
    
        <table class="style1">
            <tr>
                <td>
                     </td>
            </tr>
            <tr>
                <td>
                    <table class="style2">
                        <tr>
                            <td>
                                 </td>
                            <td>
                                 </td>
                            <td>
                                <asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
                            </td>
                            <td>
                                 </td>
                        </tr>
                        <tr>
                            <td>
                                 </td>
                            <td>
                                Class</td>
                            <td>
                                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                                    DataSourceID="SqlDataSource1" DataTextField="class_name" 
                                    DataValueField="class_name">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
                                    SelectCommand="SELECT * FROM [class]"></asp:SqlDataSource>
                            </td>
                            <td>
                                 </td>
                        </tr>
                        <tr>
                            <td>
                                 </td>
                            <td>
                                Board</td>
                            <td>
                                <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
                                    DataSourceID="SqlDataSource2" DataTextField="board" DataValueField="board">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
                                    SelectCommand="SELECT * FROM [board]"></asp:SqlDataSource>
                            </td>
                            <td>
                                 </td>
                        </tr>
                        <tr>
                            <td>
                                 </td>
                            <td>
                                City</td>
                            <td>
                                <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" 
                                    DataSourceID="SqlDataSource3" DataTextField="cityname" 
                                    DataValueField="stateid">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
                                    SelectCommand="SELECT * FROM [city]"></asp:SqlDataSource>
                            </td>
                            <td>
                                 </td>
                        </tr>
                        <tr>
                            <td>
                                 </td>
                            <td>
                                State</td>
                            <td>
                                <asp:DropDownList ID="DropDownList4" runat="server" 
                                    DataSourceID="SqlDataSource4" DataTextField="statename" 
                                    DataValueField="stateid" AutoPostBack="True">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
                                    SelectCommand="SELECT * FROM [state] WHERE ([stateid] = @stateid)">
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="DropDownList3" DefaultValue="-99" 
                                            Name="stateid" PropertyName="SelectedValue" Type="String" />
                                    </SelectParameters>
                                </asp:SqlDataSource>
                            </td>
                            <td>
                                 </td>
                        </tr>
                        <tr>
                            <td>
                                 </td>
                            <td>
                                 </td>
                            <td>
                                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                                    Text="PROCEED==>" Width="215px" />
                            </td>
                            <td>
                                 </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                     </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

CS Part

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class selection4go : System.Web.UI.Page
    {
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        
        if (Request.QueryString["t1"].ToString() != null)
        {
          string  mail = Request.QueryString["t1"].ToString();
            SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            con.Open();
            ds = new DataSet();
            string sql = "select email,rclass,board,city,state from registration where email='" + mail +"'";
            da = new SqlDataAdapter(sql, con);
            da.Fill(ds, "registration");
            DataRow dRow = ds.Tables["registration"].Rows[0];
            string em = dRow.ItemArray.GetValue(0).ToString();
            string s = dRow.ItemArray.GetValue(1).ToString();
            string t = dRow.ItemArray.GetValue(2).ToString();
            string u = dRow.ItemArray.GetValue(3).ToString();
            string v = dRow.ItemArray.GetValue(4).ToString();
            
            int r = 0;
            DataRow[] rs;
            rs = ds.Tables["registration"].Select("email='" + mail + "'");
            r = rs.Length;
            if (r > 0)
            {
                DataRow dr;
                dr = rs[0];
                string m = dr["rclass"].ToString();
                string n = dr["board"].ToString();
                string o = dr["city"].ToString();
                string p = dr["state"].ToString();
                string a, b, c, d;
                a = DropDownList1.SelectedItem.Text.Trim();
                b = DropDownList2.SelectedItem.Text.Trim();
                c = DropDownList3.SelectedItem.Text.Trim();
                d = DropDownList4.SelectedItem.Text.Trim();
                   
                if (m != a)
                {
                    System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                    lbl1.ForeColor = System.Drawing.Color.Yellow;
                    lbl1.BackColor = System.Drawing.Color.Blue;
                    lbl1.Text = "This ID is not Valid for Class " + a ;
                    ph1.Controls.Add(lbl1);
                }
                else if (n != b)
                {
                    System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                    lbl1.ForeColor = System.Drawing.Color.Yellow;
                    lbl1.BackColor = System.Drawing.Color.Blue;
                    lbl1.Text = "This ID is not Valid for Board " + b;
                    ph1.Controls.Add(lbl1);
                }
                else if (o != c)
                {
                    System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                    lbl1.ForeColor = System.Drawing.Color.Yellow;
                    lbl1.BackColor = System.Drawing.Color.Blue;
                    lbl1.Text = "This ID is not Valid for City " + c;
                    ph1.Controls.Add(lbl1);
                }
                else if (p != d)
                {
                    System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                    lbl1.ForeColor = System.Drawing.Color.Yellow;
                    lbl1.BackColor = System.Drawing.Color.Blue;
                    lbl1.Text = "This ID is not Valid for Board " + d;
                    ph1.Controls.Add(lbl1);
                }
                else
                {
                    Response.Redirect("ANScontrol.aspx");
                }
                
                    
            }
            else
            {
                System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                lbl1.ForeColor = System.Drawing.Color.Yellow;
                lbl1.BackColor = System.Drawing.Color.Blue;
                lbl1.Text = "Pleasse select Valid Selection as per ur registration";
                ph1.Controls.Add(lbl1);
            }
            
        }
        
        
    }
}
Posted
Updated 29-Jan-12 23:38pm
v2
Comments
Herman<T>.Instance 30-Jan-12 5:39am    
added pre-tags
Janardan Pandey 30-Jan-12 10:43am    
Please give me a short example to understand me batter.Please

you do not have a
C#
OnSelectedIndexChanged
event defined for DropDownList4 only
C#
AutoPostBack="True"
 
Share this answer
 
Hi,

You are not initializing event OnSelectedIndexChanged in aspx and cs page and write code which you want on change. do this and if have some problem then you can tell me, i will assist you.
 
Share this answer
 
Comments
Janardan Pandey 30-Jan-12 10:41am    
Please give me a short example to understand me batter.
Check your DropdownList's selectedvalue. It should be unique.
If the value is not unique, the SelectedIndexChanged event may not get fire.
 
Share this answer
 
use postback property in pageload

if(!Page.IsPostBack)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900