Forum Moderators: open

Message Too Old, No Replies

Language option not working

Switching languages with ASP.NET and xml

         

Barbacena

2:17 pm on May 26, 2005 (gmt 0)

10+ Year Member



Hello all,

I’m a newbie in ASP.NET programming and I’m trying to practice by writing this little application which allows users to choose a language from three options on a web page. All the different language files sit in a directory called “lang/” on the root of the website. The files are named as follows: lang/German.xml, lang/English.xml and lang/Spanish.xml.

The users get to pick a language from the listbox menu control and as soon as a language is selected, the XML file is loaded into my “dgLang” datagrid control. I do this by using a switch statement which changes according to the selected item DataValueField which is set to the language ID in the database.

The problem is that I must be getting the structure wrong because I’m getting the following error:

'System.Web.UI.WebControls.ListItem' does not contain a definition for 'DataValueField'

Here is the code for my aspx page:


<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<!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>
<title>Reading in different languages</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>

<h2>Please chose a language</h2>
<form runat="server">
<asp:ListBox id="lbxLang" runat="server" AutoPostBack="true" Rows="4" OnSelectedIndexChanged="selectLang" cssClass="ListBox">

</asp:ListBox>
</form>
<asp:DataGrid id="dgLang" runat="server" /><br />
</body>
</html>
<script language="c#" runat="server">
public string strRoot = "http://local.website/lang/";
public string strDir = "";
public string strLang = "";
private void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack){

String strConnection = ConfigurationSettings.AppSettings["WebDB"];

SqlConnection objConnection = new SqlConnection(strConnection);

string strSQLforListBox = "SELECT " + "ID, lang " + "FROM " + "langs ORDER BY lang_ID";
SqlCommand objCommand = new SqlCommand(strSQLforListBox, objConnection);

objConnection.Open();
lbxLang.DataSource = objCommand.ExecuteReader();
lbxLang.DataTextField = "lang";
lbxLang.DataValueField = "ID";
lbxLang.DataBind();
objConnection.Close();
}
}
private void selectLang(object s, System.EventArgs e)

{

int intSelected = lbxLang.SelectedItem.DataValueField;
switch (intSelected){
case "1":
strDir = "English/";
break;
case "1":
strDir = "German/";
break;
case "3":
strDir = "Spanish/";
break;
}

DataSet objDataSet = new DataSet();
objDataSet.ReadXml(strRoot + strDir + "file.xml");

dgLang.DataSource = objDataSet.Tables[0].DefaultView;
dgLang.DataBind();

}

</script>

Obviously I'm a complete beginner in ASP.NET and C# so I'm expecting syntax and structural errors. Could anyone help me with this?

Thanks in advance!

Barbacena

3:22 pm on May 26, 2005 (gmt 0)

10+ Year Member



Sorry, it seems like I fixed the problem by correcting:

"lbxLang.SelectedItem.DataValueField;"

to

"lbxLang.SelectedItem.Value;"

But I'd still welcome suggestions for improving my code.

Regards