1

DropDownListで新しいアイテムを選択すると、DropDownListがスペイン語から英語に切り替わるのはなぜですか?そして、それを防ぐにはどうすればよいでしょうか。

   <asp:DropDownList ID="ddl_r1pc" runat="server" AutoPostBack="True"
       OnSelectedIndexChanged="ddlRelationship_SelectedIndexChanged">
       <asp:ListItem></asp:ListItem>
       <asp:ListItem Value="Spouse" Text="<%$Resources:messages, RelSpouse %>"></asp:ListItem>
       <asp:ListItem Value="Parent(s)" Text="<%$Resources:messages, RelParents %>"></asp:ListItem>
       <asp:ListItem Value="Other" Text="<%$Resources:messages, Other %>"></asp:ListItem>
   </asp:DropDownList>

次に、Page_Load()では、これは常に実行されます(つまり、asIsPostBackとの両方!IsPostBack):

   try {
       culture = (string) Session["culture"];
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
   }
   catch {
       Server.Transfer("~/sessiontimeout.aspx");
   }

言語としてスペイン語を選択した後でこのページに最初にアクセスすると、ドロップダウンに、スペイン語で(予想どおりに)表示されるListItemsテキストが表示されます。ただし、ドロップダウンから別のアイテムを選択すると、すべてのアイテムが英語で返されます。

AutoPostBackの前のドロップダウン(サーバー側とFireBugの両方)を調べると、各ListItemは次のように適切に設定されています。

  Value="Some English" Text="Some Español"

一方、PostBackの後は、次のようになります。

  Value="Some English" Text="The same English"

なぜこれが起こっているのですか、そしてスペイン語をPostBacksの前に見続けるためにそれを取得するにはどうすればよいですか?

  1. で指摘されたルーチンOnSelectedIndexChangedは現在コメントアウトされているので、問題はありません。
  2. DropDownListに追加EnableViewState="true"しましたが、違いがなかったので削除しました。
  3. 以下の一番で提案されているように、Thread.CurrentThread.CurrentUICulturefromからPage_Loadに設定を移動しましたPage_Init()が、それでも違いはありませんでした。
4

2 に答える 2

0

CultureInfoの代わりに、をPage_Initイベントに設定するコードを追加してみてくださいPage_Load

protected override void OnInit(object source, EventArgs e) {
   try {
       culture = (string) Session["culture"];
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
   }
   catch {
       Server.Transfer("~/sessiontimeout.aspx");
   }
}
于 2010-07-13T19:49:19.973 に答える
0

CurrentUICultureをオーバーライドして設定する必要があることがわかりましたInitializeCuture()

protected override void InitializeCulture() {
    try {
        culture = (string) Session["culture"];
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
    }
    catch {
        Server.Transfer("~/sessiontimeout.aspx");
    }

    base.InitializeCulture();
}

それを入れると、AutoPostBacksの後、ドロップダウンは選択した言語のままになります!

于 2010-07-15T19:02:33.463 に答える