Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<select  önchange="getCountry('country');" id="country" name = "country"><select>


HTML
function getCountry(x)
{
var y=document.getElementById(x).value;
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML =y;
}


HTML
function load()
{
document.getElementById('country').value = document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML;
}


C#
protected void Page_Load(object sender, EventArgs e)
{
//if(!ISPostBack)
//{
string connectionString = "Data Source = .\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();

cmd.CommandText = "SELECT * FROM [product] where ID = '1'";

conn.Open();

System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
    this.lblCountry.Text = dr["country"].ToString();                
}
//}
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
string connectionString = "Data Source = .\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();

cmd.Parameters.Add("country", SqlDbType.VarChar);
cmd.Parameters["country"].Value = this.lblCountry.Text;  //retrieve as 'Japan' Instead of 'Korea' 

cmd.CommandText = "UPDATE [product] SET [country] = @country WHERE [ID] = '1'";

conn.Open();
int numberOfRows = cmd.ExecuteNonQuery();
conn.Close();
}


1. On page load, the SELECT dropdownlist is show 'Japan', so as lblCountry.
2. If i change the Item from 'Japan' to 'Korea', lblCountry also will update to 'Korea'.
3. btnUpdate been click, page will load again, but it stills show 'Japan' instead of 'Korea'. Check at database, it still remains as 'Japan'. The same code/query been used on asp:dropdownlist and it works as expected.
Any advice for this question please? :)
Posted
Updated 27-Dec-11 21:24pm
v2
Comments
demouser743 28-Dec-11 3:03am    
Have you added if(!IsPostback) in your page load
melvintcs 28-Dec-11 3:22am    
yes, i did :) still the same
demouser743 28-Dec-11 3:27am    
Why you are assigning the values from JavaScript why don't you get it from sever side code
melvintcs 28-Dec-11 5:34am    
if i add runat="server", then the country list wont appear... the only way is to transfer the value to Label, then get the value from codeBehind. My country list is stored in the countries.js

It may be the browser problem try

JavaScript
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML =y;

document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').value=y;

document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').text=y;


it works fine for me and also include ur page load definations in IsPostback.

It works well for me.
 
Share this answer
 
v2
Comments
melvintcs 28-Dec-11 5:26am    
tried on chrome, IE and firefox, only the first one (innerHTML) is working, my 'working' here means lblCountry will update if change the value of the dropdownlist.
amit28august 28-Dec-11 5:41am    
yes, lblCountry is updating onchange of select dropdownlist.
amit28august 28-Dec-11 5:44am    
<select önchange="getCountry('country');" id="country" name = "country">
<option value ="Japan">Japan</option>
<option value ="Korea">Korea</option>
<option value ="Africa">Africa</option>
</select>

<asp:Label ID ="lblCountry" runat ="server" >



<script language ="javascript" type ="text/javascript">

function getCountry(x)
{
var y=document.getElementById(x).value;
document.getElementById('lblCountry').innerHTML =y;

alert (y);


}
</script>




You try first with this normal code then u go with ur databse assinment.
melvintcs 28-Dec-11 6:15am    
ok, this is working. but did u try to store the selected value?
this is what i did:

protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source = .\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();

cmd.Parameters.Add("country", SqlDbType.VarChar);
cmd.Parameters["country"].Value = this.lblCountry.Text;

cmd.CommandText = "INSERT INTO [member] ([country]) VALUES (@country)";

conn.Open();

int numberOfRows = cmd.ExecuteNonQuery();
conn.Close();
}

it store nothing into the field... this is the main problem i facing right now :(
amit28august 28-Dec-11 6:23am    
do one thing make select available at code side by making it runat="server" in html


<select runat="server" önchange="getCountry('country');" id="country" name = "country">
<option value ="Japan">Japan</option>
<option value ="Korea">Korea</option>
<option value ="Africa">Africa</option>
</select>


and then u can use

cmd.Parameters["country"].Value =country.Value.Tostring();
to get the selected value

this works fine.
Try using this code:

cmd.Parameters.AddWithValue("@country", this.lblcountry.Text);


to replace:

SQL
cmd.Parameters.Add("country", SqlDbType.VarChar);
cmd.Parameters["country"].Value = this.lblCountry.Text;
 
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