1

文字列のリストを維持するのに問題があります。リストはOnInitでインスタンス化され、のデータソースとして設定されますGridView。(基本的な考え方は、ユーザーがテキストボックスに何かを入力すると、それがGridView何度でもに表示されるというものです。)

ユーザーが入力したものが何であれ、最初のエントリで問題なく機能しGridViewます。ただし、次のエントリでは、以前に入力された値はすべて消えます-OnInit再度実行され、List<string>が再インスタンス化され、以前の値が上書きされます。OnInitロジックをに移動しようとしましOnPreInitたが、リストにNull参照例外が表示されました。

これが私がやろうとしていることの不自然な例です:

私は、、とを持ってTextBoxButtonますGridview

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add"
     onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="true"></asp:GridView>

背後にあるコード:

protected override void OnInit(EventArgs e)
{
    List<string> gvValues = new List<string>();
    GridView1.DataSource = gvValues;
    GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
     gvValues.Add(TextBox1.Text);
     GridView1.DataBind();
}

私はOnInit過去にオブジェクトを作成しましたが、それらの状態が持続することに問題はありませんでした。明らかに、私はここで何かが欠けています。誰かが私のロジックの欠陥を指摘し、この機能を実現する方法を提案してください。

4

3 に答える 3

1

以下のコードはテストされ、動作します。

// ASPX Code
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:Button ID="Button1" runat="server" Text="Add"
 onclick="Button1_Click" />
 <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="true"></asp:GridView>

// Code Behind
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<string> gvValues = new List<string>();
        GridView1.DataSource = gvValues;
        GridView1.DataBind();
        Session["Data"] = gvValues;
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    List<string> lt = new List<string>();
    lt = (List<String>)Session["Data"];
    lt.Add(TextBox1.Text);
    GridView1.DataSource = lt;
    GridView1.DataBind();
    Session["Data"] = lt; // Save it in Session, so next time available
}

// Add the values one by one, and it will show you all the values
// OUTPUT
  Test
  Test1
  Test2 
于 2012-08-29T19:35:01.750 に答える
1

リストをグリッドビューに再割り当てし、gvValuesをセッションに入れようとしましたか?

protected override void OnInit(EventArgs e)
{
    GridView1.DataSource = GvValues;
    GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
     GvValues.Add(TextBox1.Text);
     GridView1.DataSource = GvValues;
     GridView1.DataBind();
}

private List<string> GvValues
{
  get
  {
    if(Session["list"] != null)
    {
      return (List<string>)Session["list"];
    }

     return new List<string>();
  }

  set
  {
    Session["list"] value;
  }
}
于 2012-08-29T19:29:28.273 に答える
0

私は私のためにうまくいった小さな例をしました

default.aspx

<form id="form1" runat="server">
    <asp:GridView ID="gvEntries" runat="server" AutoGenerateColumns="true">
    </asp:GridView>
    <asp:TextBox ID="txtNewEntry" runat="server"></asp:TextBox>
    <asp:Button ID="btnSave" runat="server" Text="Save entry" OnClick="btnSave_Click" />
</form>

default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        List<string> source = new List<string>();
        source.Add("entry1");
        source.Add("entry2");
        source.Add("entry3");
        Session["source"] = source;
        gvEntries.DataSource = source;
        gvEntries.DataBind();
    }
}
protected void btnSave_Click(object sender, EventArgs e)
{
    List<string> source = (List<string>)Session["source"];
    source.Add(txtNewEntry.Text);
    gvEntries.DataSource = source;
    gvEntries.DataBind();
}

この例では、 pageLoad(GET)で初期データをバインドします。したがって、データソースはgridviewsビューステート内に保存されます。別のエントリを追加する場合は、Session-var内に保存されている元のソースにアクセスする必要があります。したがって、それへの参照を取得し、値を追加して、dataSourceを再バインドします。

于 2012-08-29T20:00:54.597 に答える