0

これを使用してDropDownList、からの値をデータベースに保存する必要がCheckBoxListあります。

から別のインデックスを選択する前にDropDownList、 の値をCheckBoxList保存する必要があり、ユーザーに「続行する前に保存してください」という警告が表示されます。

上記の警告メッセージを表示できます。しかし問題は、インデックスを変更するとDropDownList、以前に選択したインデックスが失われることです。

誰かが以前に選択した値を取得し、同じ値を動的に選択するのを手伝ってくれますかDropDownList? 値をデータベースに保存する必要があるためです。

警告メッセージを表示するコードは次のとおりです。

protected void LOC_LIST2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CheckBoxList2.Items.Count > 0)
        {
            Label7.Visible = true;
            Label7.Text = "*Save List Before Proceeding";
        }
4

5 に答える 5

1

ページを最初に読み込んだときに、選択した値を次のように取得できます。

 protected void Page_Load(object sender, EventArgs e)
 {

    if (!IsPostBack) // Because When postback occurs the selected valued changed.
    {
        ViewState["PreviousValue"] = ddl.SelectedValue;
    }
 }

選択したインデックス変更イベントで、以前の値を新しい値で更新します。

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    ViewState["NewValue"] = ddl.SelectedValue;

    // Do your work with PreviousValue and then update it with NewValue so next you can acces your previousValue using ViewState["PreviousValue"]

    ViewState["PreviousValue"] = ViewState["NewValue"];   
}

または別のページで選択した値にアクセスする場合は、セッションに保存します。

于 2012-08-18T13:29:49.717 に答える
1

グローバル変数を使用します。

以下のコードを使用します。PreviousIndex前を保持し、CurrentIndex現在を保持します。

int PreviousIndex = -1;
int CurrentIndex = -1;

protected void LOC_LIST2_SelectedIndexChanged(object sender, EventArgs e)
{
    PreviousIndex = CurrentIndex;
    CurrentIndex = myDropdownList.Position; // Or whatever the get position is.
    if (CheckBoxList2.Items.Count > 0)
    {
        Label7.Visible = true;
        Label7.Text = "*Save List Before Proceeding";
    }
}
于 2012-08-18T13:18:40.593 に答える
0
    protected void LOC_LIST2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["SavedItem"] =  LOC_LIST2.SelectedItem;
            if (CheckBoxList2.Items.Count > 0)
            {
                Label7.Visible = true;
                Label7.Text = "*Save List Before Proceeding";
            }
        }

after you access on value or text
SelectedItem item = Session["SavedItem"] as SelectedItem;
if(item !=null)
{
string something= item.Value;
string otherthing =item.Text;
}
于 2012-08-18T13:26:39.487 に答える
0

このコードで試すことができます-セッションキャッシングを使用してbu

public string YourOldValue
{
  get
  {
     if(Session["key"] != null) 
        return (string) Session["key"];
  }
  set
  {
     Session["key"] = value;
  }   

 }

 //Set value
 YourOldValue = yourControl.SelectedValue; 
于 2012-08-18T13:19:21.813 に答える
0

だから、これが最終的に私のために働いたものです。上記の回答の組み合わせ。

ページ/コントロールの OnLoad ハンドラーで、以前および現在選択されているインデックス/値の両方を追跡する必要があります。

private int PreviousSelectedIndex
{
    get { return (Page.ViewSate["prevIdx"] == null) ? -1 : (int)ViewSate["prevIdx"]; }
    set { Page.ViewSate["prevIdx"] = value; }
}

private int CurrentSelectedIndex
{
    get { return (Page.ViewSate["currIdx"] == null) ? -1 : (int)ViewSate["currIdx"]; }
    set { Page.ViewSate["currIdx"] = value; }
}

protected override void OnLoad(EventArgs e)
{
    if (!Page.IsPostBack)
    {
        PreviousDropDownValue = ddlYourDropDownList.SelectedValue;
        CurrentDropDownValue = ddlYourDropDownList.SelectedValue;
    }
    else if (Page.IsPostBack && CurrentDropDownValue != ddlYourDropDownList.SelectedValue)
    {
        PreviousDropDownValue = CurrentDropDownValue;
        CurrentDropDownValue = ddlYourDropDownList.SelectedValue;
    }
}

その後、以前の値と現在の値を相互に比較できます。

于 2020-05-01T15:34:59.167 に答える