how to link two Drop Down Boxes without using database [Resolved]

Posted by Shailesh21235 under ASP.NET on 5/30/2012 | Points: 10 | Views : 6517 | Status : [Member] | Replies : 8
i am new to asp.net. i have created two dropdown list. When i select the data in country than its equivalent capital should be displayed in text box without using database.


<asp:DropDownList ID="ddlCoutry" runat="server" onselectedindexchanged="Ddl1_SelectedIndexChanged" >
<asp:ListItem Value ="1" >India</asp:ListItem>
<asp:ListItem Value ="2" >Maldives</asp:ListItem>
<asp:ListItem Value="3" >Srilanka</asp:ListItem>

<asp:DropDownList ID="ddlCapital" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

<asp:ListItem Value="1" >NewDelhi</asp:ListItem>
<asp:ListItem Value="2" >Male</asp:ListItem>
<asp:ListItem Value="3" >Colombo</asp:ListItem>

How to link these two boxes?? I had tried by equating the values of the selected text??? But not got the results???
Any help would give me a huge relief..
Regards




Responses

Posted by: Santhi on: 5/31/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

To print the capital in the textbox you should write this code...
cs page:
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlcountry.SelectedIndex == 0)
{
ddlcapital.SelectedIndex = 0;
TextBox1.Text = ddlcapital.SelectedItem.Text.ToString();
}
else if (ddlcountry.SelectedIndex == 1)
{
ddlcapital.SelectedIndex = 1;
TextBox1.Text = ddlcapital.SelectedItem.Text.ToString();
}
else
{
ddlcapital.SelectedIndex = 2;
TextBox1.Text = ddlcapital.SelectedItem.Text.ToString();
}
}

your design page should be like this:
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:dropdownlist ID="ddlcountry" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlcountry_SelectedIndexChanged">
<asp:ListItem Value="0">INDIA</asp:ListItem>
<asp:ListItem Value="1">MALDIVES</asp:ListItem>
<asp:ListItem Value="2">SRILANKA</asp:ListItem>
</asp:dropdownlist>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlcapital" runat="server">
<asp:ListItem Value="0">NEWDELHI</asp:ListItem>
<asp:ListItem Value="1">MALE</asp:ListItem>
<asp:ListItem Value="2">COLOMBO</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>

<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</td>
</tr>
</table>


Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/30/2012 [Member] Starter | Points: 25

Up
0
Down
hi,

if you select india in the first drop down you have to display the capital as new delhi in the second drop down list ..
Am i right..
or you have to display new delhi in the text box..
your question is not clear.

Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/30/2012 [Member] Starter | Points: 25

Up
0
Down
If i am right:
then you aspx page design should be like this:
<table>
<tr>
<td>
<asp:dropdownlist ID="ddlcountry" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlcountry_SelectedIndexChanged">
<asp:ListItem Value="0">INDIA</asp:ListItem>
<asp:ListItem Value="1">MALDIVES</asp:ListItem>
<asp:ListItem Value="2">SRILANKA</asp:ListItem>
</asp:dropdownlist>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlcapital" runat="server">
<asp:ListItem Value="0">NEWDELHI</asp:ListItem>
<asp:ListItem Value="1">MALE</asp:ListItem>
<asp:ListItem Value="2">COLOMBO</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>

your code behind page should be like this:

if (ddlcountry.SelectedIndex == 0)
{
ddlcapital.SelectedIndex = 0;
}
else if (ddlcountry.SelectedIndex == 1)
{
ddlcapital.SelectedIndex = 1;

}
else
{
ddlcapital.SelectedIndex = 2;
}


This coding will work perfectly.. if you select srilanka you will get colombo in the second dropdown list .
This colombo will display first in the dropdownlist..

You can also use selected value instead of using selected index




Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/30/2012 [Member] Starter | Points: 25

Up
0
Down
write that code in the first dropdown list selected index changed.
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlcountry.SelectedIndex == 0)
{
ddlcapital.SelectedIndex = 0;
}
else if (ddlcountry.SelectedIndex == 1)
{
ddlcapital.SelectedIndex = 1;

}
else
{
ddlcapital.SelectedIndex = 2;
}
}

Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pankajkrbabloo on: 5/30/2012 [Member] Starter | Points: 25

Up
0
Down
Dear Shailesh!
you can use Javascript or JQUERY to do so without refreshing pages. In javascript you can use if/elseif statement or switch to do so.



PG

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 5/30/2012 [Member] Starter | Points: 25

Up
0
Down
THANKS pankaj,
but i am not familiar with java script or jquery. later on i may need your help.
regards.

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 5/30/2012 [Member] Starter | Points: 25

Up
0
Down
thanks santhi ,
It helped a lot thank you for your help.
by this code the selected country's capital is displayed in 2nd dropdownlist. But How to print capital in the text box ???
regards.

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 5/31/2012 [Member] Starter | Points: 25

Up
0
Down
tahnks shanti,
you solved the problem.
Cheers.

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response