简体   繁体   中英

Variables lost on postback in asp.net

I have a gridview with some data and I want to add a checkbox column which can choose multiple rows. By clicking on it I want to save an primary key of row and change css class of row.

Using this article(step 2) I created itemtemplate,added there a checkbox(specifying ID as TransactionSelector), and add a checkedChange() to it. There I only change a css class of row and add a row index to arraylist. But when I click button with event which show this list, it has no items.

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="TransactionSelector" runat="server" 
                        oncheckedchanged="TransactionSelector_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="iTransactionsId" HeaderText="iTransactionsId" 
                SortExpression="iTransactionsId" />
            <asp:BoundField DataField="mAmount" HeaderText="mAmount" 
                SortExpression="mAmount" />
            <asp:BoundField DataField="vchTransactionType" HeaderText="vchTransactionType" 
                SortExpression="vchTransactionType" />
            <asp:BoundField DataField="dtDate" HeaderText="dtDate" 
                SortExpression="dtDate" />
            <asp:BoundField DataField="cStatus" HeaderText="cStatus" 
                SortExpression="cStatus" />
            <asp:BoundField DataField="test123" HeaderText="test123" 
                SortExpression="test123" />
        </Columns>
        <RowStyle CssClass="unselectedRow" />
    </asp:GridView>

</asp:Panel>

<asp:Panel ID="InfoPanel" runat="server" CssClass="infoPanel">

    <asp:Button ID="ShowSelected" runat="server" Text="Button" 
        onclick="ShowSelected_Click" />

    <asp:Label ID="InfoLabel" runat="server"></asp:Label>

</asp:Panel>

C Sharp code:

public partial class WebForm1 : System.Web.UI.Page
{
    ArrayList indices = new ArrayList();


protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
     {
       GridView1.DataSourceID = "SqlDataSource1";
       GridView1.DataBind();
     }
}



protected void TransactionSelector_CheckedChanged(object sender, EventArgs e)
{
   CheckBox cb = (CheckBox)sender;
   GridViewRow row = (GridViewRow)cb.NamingContainer;
   // row.CssClass = (cb.Checked) ? "selectedRow" : "unselectedRow";
   if (cb.Checked)
   {
     row.CssClass = "selectedRow";
     indices.Add(row.RowIndex);
   }
   else
   {
     row.CssClass = "unselectedRow";
     indices.Remove(row.RowIndex);
   }
}

protected void ShowSelected_Click(object sender, EventArgs e)
{
  InfoLabel.Text = "";
  foreach (int i in indices)
  {
   InfoLabel.Text += i.ToString() + "<br>";
  }
}

}
}

You have to persist variable in postback using ViewState . Also its better if you use List<T> generic implementation rather than ArrayList

ViewState["Indices"] = indices;

And to recover it back

indices = ViewState["Indices"] as ArrayList;

As Habib said, you could use ViewState . You could also use ControlState instead, as shown here . If your code is in a custom control or user control, you may also need to override OnInit to

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Page.RegisterRequiresControlState(this);
}

Please feel free to respond with feedback. I'm new at posting answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM