What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3638 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > how to link two Drop Down Boxes without using database ...
Shailesh21235

how to link two Drop Down Boxes without using database

Replies: 8 | Posted by: Shailesh21235 on 5/30/2012 | Category: ASP.NET Forums | Views: 1581 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Santhi
Santhi  
Posted on: 5/30/2012 8:29:07 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Santhi
Santhi  
Posted on: 5/30/2012 8:42:43 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Santhi
Santhi  
Posted on: 5/30/2012 8:44:12 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Pankajkrbabloo
Pankajkrbabloo  
Posted on: 5/30/2012 9:24:42 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Shailesh21235
Shailesh21235  
Posted on: 5/30/2012 1:56:25 PM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Shailesh21235
Shailesh21235  
Posted on: 5/30/2012 1:58:41 PM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Santhi
Santhi  
Posted on: 5/31/2012 12:25:45 AM
Level: Starter | Status: [Member] | Points: 50

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. | Reply | Alert Moderator 

Shailesh21235
Shailesh21235  
Posted on: 5/31/2012 1:47:20 AM
Level: Starter | Status: [Member] | Points: 25

tahnks shanti,
you solved the problem.
Cheers.

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

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 10:46:15 PM