Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
My question is :
here by i have two dropdownlists both are fetching data from database
i have textboxes also
when i doesnot select value from first dropdownlist then i want value of textbox automatically set to 1.




This is Design page Coding

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="abc.aspx.cs" Inherits="Abhimanu_ADMIN_abc" %>

<!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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

                            Select Test Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:DropDownList ID="ddlTestType" runat="server" AutoPostBack="True"
                                onselectedindexchanged="ddlTestType_SelectedIndexChanged" Width="100px">
                                <asp:ListItem></asp:ListItem>
                            </asp:DropDownList>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:TextBox ID="txtTestType" runat="server"></asp:TextBox>
                        <br />
                            Select Test Section&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:DropDownList ID="ddlTestSection" runat="server" AutoPostBack="True"
                                onselectedindexchanged="ddlTestSection_SelectedIndexChanged" Width="140px">
                            </asp:DropDownList>
                        &nbsp;&nbsp;
                            <asp:TextBox ID="txtTestSection" runat="server"></asp:TextBox>

    </div>
    </form>
</body>
</html>




this is codebehind

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Net;
using System.IO;

public partial class Abhimanu_ADMIN_abc : System.Web.UI.Page
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();

//SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ToString());
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conTestMaster"].ToString());
DataTable dtbladd = new DataTable();
clsAddTest objtest = new clsAddTest();
SqlCommand cmdAd;
//string test;
bool IsPageRefreshed = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack == true)
{
dtbladd = objtest.dtTestType();
ddlTestType.Items.Clear();
for (int i = 0; i < dtbladd.Rows.Count; i++)
{
ddlTestType.Items.Add(dtbladd.Rows[i]["TestName"].ToString());
}
ddlTestType.DataSource = dtbladd;
dtbladd = objtest.dtTestSection(Convert.ToInt16(ddlTestType.SelectedIndex.ToString()) + 1);
ddlTestSection.Items.Clear();
for (int i = 0; i < dtbladd.Rows.Count; i++)
{
ddlTestSection.Items.Add(dtbladd.Rows[i]["TestSection"].ToString());

}
ddlTestSection.Items.Insert(0, "----------Select----------");
}

}
protected void ddlTestType_SelectedIndexChanged(object sender, EventArgs e)
{
dtbladd = objtest.dtTestSection(Convert.ToInt16(ddlTestType.SelectedIndex.ToString()) + 1);
ddlTestSection.Items.Clear();
for (int i = 0; i < dtbladd.Rows.Count; i++)
{
ddlTestSection.Items.Add(dtbladd.Rows[i]["TestSection"].ToString());

}

DataTable dt1 = new DataTable();
dt1 = objtest.dtGetTesttypeID(ddlTestType.SelectedItem.Text.ToString());

if (dt1.Rows.Count > 0)
{

ddlTestType.SelectedValue = dt1.Rows[0]["TestName"].ToString();

txtTestType.Text = dt1.Rows[0]["ID"].ToString();
}
}
protected void ddlTestSection_SelectedIndexChanged(object sender, EventArgs e)
{
dtbladd = new DataTable();
dtbladd = objtest.dtGetTestSectionId(ddlTestSection.SelectedItem.Text.ToString());
if (dtbladd.Rows.Count > 0)
{
ddlTestSection.SelectedValue = dtbladd.Rows[0]["TestSection"].ToString();

txtTestSection.Text = dtbladd.Rows[0]["ID"].ToString();

}


}
}
Posted
Comments
CodeBlack 9-Oct-13 4:44am    
what is the structure of the 'dtbladd'

Just use below statement after binding your dropdownlist

ddlTestSection.Items.Insert(0, new ListItem("----------Select----------","1"));
 
Share this answer
 
Comments
Mann Rai 9-Oct-13 6:32am    
sir, this codeline is working for 2nd dropdownlist
its fine
but i want to display ---select--- at zero index of first dropdownlist
Sharma Richa 9-Oct-13 7:54am    
add the same line for first dropdown
You can use above code with respected dropdownlist id also as if your ddl id is ddlFirst then

ddlFirst.Items.Insert(0, new ListItem("----------Select----------","1"));

after binding ddlFirst.
 
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