页面里有两个DropDownList,第二个是在第一个PostBack后通过第一个的值来搜索数据库取得的,问题就在这里,我要对其中的值转换输出,如1-中国,2-美国这样。
请问这个转换的函数要放在哪里,是在前台界面还是在后台页面,我两者都试了,不行,可能方法不对
下面是一些代码:
<asp:DropDownList ID="MainNameList" AutoPostBack="True" Runat="server" />
<asp:DropDownList ID="QuestList" Width="200" Runat="server" />string MainName = MainNameList.SelectedValue;
string mysqlConnectionString = ConfigurationSettings.AppSettings["MysqlConnection"];
OdbcConnection mysqlConnection = new OdbcConnection(mysqlConnectionString);
string mysqlCommandString = "SELECT QuestIndex,ScriptQuestIndex FROM pos_quest WHERE MainName='"+MainName+"' AND ScriptQuestIndex<>0 AND QuestType=2";
OdbcDataAdapter mysqlCommand = new OdbcDataAdapter(mysqlCommandString,mysqlConnection);
DataSet mysqlDS = new DataSet();
mysqlCommand.Fill(mysqlDS,"Quest");
QuestList.DataSource = mysqlDS.Tables["Quest"].DefaultView;
QuestList.DataValueField = "QuestIndex";
QuestList.DataTextField =  "ScriptQuestIndex";
QuestList.DataBind();
还有一个转换函数:
public string ShowQuest(string scriptQuestIndex)
{
string QuestDescription = "";
//讀取任務列表
FileStream fs = new FileStream(Server.MapPath("task.ini"),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs);
string questInfo;
string[] questItems;
char split = '\t';
while(sr.Peek() >= 0)
{
questInfo = sr.ReadLine();
if(questInfo.Length > 0)
{
if(questInfo.Substring(0,1) != ";")
{
questItems = questInfo.Split(split);
if(questItems[0] == scriptQuestIndex)
{
QuestDescription = questItems[1];
}
}
}
}
if(QuestDescription == "")
{
QuestDescription = scriptQuestIndex;
}
return QuestDescription;
//--End--
}

解决方案 »

  1.   

    第一个DropDownList的AutoPostBack="True"设置了吗
      

  2.   

    <asp:DropDownList ID="MainNameList" AutoPostBack="True" Runat="server" />
    请看,设置了,如果我不用那个转换函数,是正常的
    那个函数本身也是没有问题的
      

  3.   

    在第一个的SelectedIndexChanged事件中写第二个的绑定代码
      

  4.   

    写了,内容就是:
    string MainName = MainNameList.SelectedValue;
    string mysqlConnectionString = ConfigurationSettings.AppSettings["MysqlConnection"];
    OdbcConnection mysqlConnection = new OdbcConnection(mysqlConnectionString);
    string mysqlCommandString = "SELECT QuestIndex,ScriptQuestIndex FROM pos_quest WHERE MainName='"+MainName+"' AND ScriptQuestIndex<>0 AND QuestType=2";
    OdbcDataAdapter mysqlCommand = new OdbcDataAdapter(mysqlCommandString,mysqlConnection);
    DataSet mysqlDS = new DataSet();
    mysqlCommand.Fill(mysqlDS,"Quest");
    QuestList.DataSource = mysqlDS.Tables["Quest"].DefaultView;
    QuestList.DataValueField = "QuestIndex";
    QuestList.DataTextField =  "ScriptQuestIndex";
    QuestList.DataBind();这一段了,现在的问题是在显示上,就是把DataTextField 里的值通过ShowQuest这个函数转换输出。
      

  5.   

    不要直接绑定第二个DDL,把值取出来后一个一个的加上去。做一个for循环
    QuestList.Items.Add(new ListItem(text,value));
      

  6.   

    谢谢 lovefootball(蓝色咖啡) ,你这个办法我有想过,只是想那样效率低了点,,想看看有没有直接绑定显示的办法
      

  7.   

    函数放在后台,if(IsPostBack){},还要把第一个DropDownList的EnableViewstate设为true。第二个要不要设你可以自己试一下。
      

  8.   

    這跟第一個的EnableViewState沒什么關系吧,如果我不用那個函數,一切都正常,只是顯示的值是數字,非常的不直觀。