locked
Listview label into a session variable RRS feed

  • Question

  • User1289871205 posted

    Hi,

    After having my previous problem solved here I thought I would again turn to this site for help.

    It seems such a simple event, but I can't get it to work correctly. To aid with security I've changed all the names, but the idea is still the same.

    I have a aspx wep page that has a label (idlabel). As per my code it currently reads "Nothing".

    <asp:Label ID="idlabel" runat="server" Text="Nothing"></asp:Label>

    On Page_Load I can change that label to read "Loaded".

    protected void Page_Load(object sender, EventArgs e)
            {   idlabel.Text = "Loaded";     }

    The problem is when I try and change the label again after using a button located within my listview.

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" OnItemDataBound="ListView1_ItemDataBound"
                DataSourceID="DataSource" OnItemCommand="ListView1_SelectedIndexChanged">
                <ItemTemplate>
                    <tr>
                            <td>
                                <asp:Button ID="Button1" runat="server" Text="Change Text" />
                            </td>
                    </tr>
                </ItemTemplate>
    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                idlabel.Text = "Changed";
                Response.Redirect("MyPage.aspx");
            }

    Button uses a command which when using the debug runs with no problems, however the label does not change. The Listview and gridviews contained within (data not posted here) work with no problems.

    Hopefully someone can help and let me know where I am going wrong. My overall aim is to put the label into a session variable so I can execute a query with it as a variable.

    Thanks in advanced

    Wednesday, November 18, 2015 10:43 AM

All replies

  • User533502567 posted

    idlabel.Text = "Changed"; Response.Redirect("MyPage.aspx");

    Code redirects to same page(?)...so page_load will execute again

    protected void Page_Load(object sender, EventArgs e)
            {   idlabel.Text = "Loaded";     }

    Wednesday, November 18, 2015 11:27 AM
  • User1289871205 posted

    Hi,

    Such a silly mistake. I can't believe I did that. Sorry. I knew I was doing something wrong, but never thought of that. Thank you.

    Any idea how to get the ID into a session variable, depending which record is selected?

    Wednesday, November 18, 2015 11:55 AM
  • User533502567 posted

    Any idea how to get the ID into a session variable, depending which record is selected?

    Something like this...

            protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
             {
                  //if you need index...
                   Session["mySelectedValue"] = ListView1.SelectedIndex;
                
             }

    Wednesday, November 18, 2015 12:42 PM
  • User1289871205 posted

    Hi,

    I get "-1" but I need the ID that my listview shows in the first column, which changes every record.

    Wednesday, November 18, 2015 1:10 PM
  • User533502567 posted

    I get "-1" but I need the ID that my listview shows in the first column, which changes every record.

    ListView selected items are a collection, can get values like this..

    if (ListView1.SelectedItems.Count > 0)
    {
         var item = ListView1.SelectedItems[0];
         var sValue = item.Value;
    }

    Wednesday, November 18, 2015 9:06 PM
  • User61956409 posted

    Hi chrismusic79,

    You could save ID field in CommandArgument

    <asp:Button ID="Button1" runat="server" CommandName="Select" CommandArgument='<%# Eval("ID") %>' Text="Change Text" />
    

    Then we could retrieve the value and save it in session variable in ItemCommand event.

    if (e.CommandName == "Select")
    {
        string id = e.CommandArgument.ToString();
        Session["id"] = id;
    }
    

    Best Regards,

    Fei Han

    Thursday, November 19, 2015 4:38 AM
  • User891964834 posted

    Hi,

    Sorry, can't get into the old account. Had to create a new one.

    Using your suggestion I can't seem to get it working. I wonder if we are not on the same wavelength. 

    My Listview is as per an older thred of mine (http://forums.asp.net/p/2076610/5989505.aspx?Re+Showing+a+gridview+within+a+listview)

    I have created a button in each line of the listview which I need to use to access another page, but it's the employeeid in column 1 that I use to bring up the correct data.

    Using the 2nd suggestion (below yours) I can get the employeeid as text on the button, but can not manage to get it into a session variable, which is what I want.

    Thank you for your time so far.

    Tuesday, November 24, 2015 11:41 AM
  • User-1716253493 posted

    If you want to change the label text property, you should not change the label text in page_load event in every postback

    protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostBack)
      {
        idlabel.Text = "Loaded";
      }
    }
       

    Wednesday, November 25, 2015 7:37 AM
  • User891964834 posted

    Hi,

    Thank you Oned_gk for your suggestion, but don't want to change the label text, it's already set as per my employeeID. I need the label to be a session variable. I can normally get it working, it's just it changes on every row.

    Thursday, November 26, 2015 10:23 AM