Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 47688 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > how to get a field value based on the previus field in asp.net ...
Ravinderk

how to get a field value based on the previus field in asp.net

Replies: 5 | Posted by: Ravinderk on 8/11/2012 | Category: ASP.NET Forums | Views: 272 | Status: [Member] | Points: 10  


Hi All,
I am very new to .net. I have requirement that, I have a dropdown list which have 106 countries. Now I need to display country code(ex: India +91) based on the selected country name from the country dropdown list. Can any one help me out....


Thanks
ravi


Reply | Reply with attachment | Alert Moderator

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

 Replies

Vikash
Vikash  
Posted on: 8/12/2012 7:08:01 AM
Level: Starter | Status: [Member] | Points: 25

Here is your .aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>--select--</asp:ListItem>
<asp:ListItem Value="+91">India</asp:ListItem>
<asp:ListItem Value="+90">Pakistan</asp:ListItem>

</asp:DropDownList>
</div>
</form>
</body>
</html>

Below is code behind file

using System;
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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0)
{
Response.Write(DropDownList1.SelectedValue.ToString());
}
}
}

i hope this will help you...............

Regards,
Vikash Pathak

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

Shameer.Ali87
Shameer.Ali87  
Posted on: 8/13/2012 4:20:00 AM
Level: Starter | Status: [Member] | Points: 25


Just tell me, you have 1 dropdownlist for country..

and the result (country code for ex: india: +91), it should also come in drop down list or as a label...

and your country list is coming from database or from the dropdownlist it self..

just tell me these things first..

shameer ali shaik

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

Ravinderk
Ravinderk  
Posted on: 8/13/2012 5:01:45 AM
Level: Starter | Status: [Member] | Points: 25

I am using Asp.net MVC3...
Yes , I have 1 dropdownlist for country..

and the resultthe result should come in a label...

and your country list is coming from database ..

just tell me these things first..

Thanks,
ravi

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

Shameer.Ali87
Shameer.Ali87  
Posted on: 8/13/2012 7:25:12 AM
Level: Starter | Status: [Member] | Points: 25

ok, thanks for getting back..

here's the answer..

first you create a table(here country is my table) in your DataBase (SQL or any other) with countryname, countryID as columns in the table..

you can add values for both the columns at the time of table creation..

then in your aspx page drag a dropdownlist (make it auto post back as true) & add two labels..
Initially, make the labels visible as false
then go to your code behind...

here's my c# code..
here I used MS Acess as my database, you can use any DB, just replace the keywords...
--------------------------------------------------------
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.OleDb;

public partial class Default2 : System.Web.UI.Page
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\sameer.mdb");
// here sameer is my database name..
// for ms acess, you should put your DB inside any drive as above..
// if it is on any other location, it might not properly.
OleDbCommand cmd = new OleDbCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}

public void databind()
{
con.Open();
cmd = new OleDbCommand("select * from country", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "countryname";
DropDownList1.DataValueField = "countryID";
DropDownList1.DataBind();

// this dynamically(explicitly) adds items to dropdownlist.
// both the zero's indicate the index position in dropdownlist.
DropDownList1.Items.Insert(0, new ListItem("select", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.ToString();
Label2.Text = DropDownList1.SelectedValue;
Label1.Visible = true;
Label2.Visible = true;

}
}
--------------------------------------------------------------------------------

Keep Rocking...


shameer ali shaik

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

Ravinderk
Ravinderk  
Posted on: 8/14/2012 2:34:19 AM
Level: Starter | Status: [Member] | Points: 25

Thanks for your reply. But am using MVC3 architecture. So could u please give me this solution in Asp.Net MVC3..

Thanks,
Ravi

Ravinderk, 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/20/2013 8:54:52 AM