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

Posted by Ravinderk under ASP.NET on 8/11/2012 | Points: 10 | Views : 926 | Status : [Member] | Replies : 5
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




Responses

Posted by: Vikash on: 8/12/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Shameer.Ali87 on: 8/13/2012 [Member] Starter | Points: 25

Up
0
Down

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

Posted by: Ravinderk on: 8/13/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Shameer.Ali87 on: 8/13/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Ravinderk on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Login to post response