Link to home
Start Free TrialLog in
Avatar of conceptdata
conceptdataFlag for Denmark

asked on

Session Variables in aspx and gridview

Hi

Can someone help with setting a session variable in the attached code snippet.
 Session("xmlfile") = .......................................
Thanks
<ItemTemplate>
                        <asp:HyperLink ID="FileLink" NavigateUrl='<%# "LabelsEdit.aspx" + Container.DataItem.ToString() %>'
                            Text='<%# Container.DataItem.ToString() %>' runat="server" Target="_self" />
                    </ItemTemplate>

Open in new window

Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

Avatar of conceptdata

ASKER

Okay, but In my code I need ( didn't explain before) to set the session("xmlfile") to the value from Container.DataItem.ToString()...

Can you give a hint or better, some code ..
The code you posted will be called for each item in the list. Setting the session to the current item will have the effect of the session containing only the last item in the (repeater/gridview/etc). Is that what you were after?
I give you the whole code :


    <form id="form1" runat="server" method="post" name="LabelsNavigate">
    <div>
        <asp:Label ID="labelStatus" runat="server"></asp:Label><br />
        <asp:FileUpload ID="FileUpload1" runat="server" visible="false"/><br />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" visible="false" /><br /> 
        <br />
        <asp:GridView ID="GridView1" runat="server" DataSource="<%# GetUploadList() %>" OnRowDeleting="GridView1_RowDeleting"
            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField HeaderText="Choose data-file">
                    <ItemStyle HorizontalAlign="Center" Width="70%" />
                    <ItemTemplate>
                        <asp:HyperLink ID="FileLink" NavigateUrl='<%# "LabelsEdit.aspx" + Container.DataItem.ToString() %>'
                            Text='<%# Container.DataItem.ToString() %>' runat="server" Target="_self" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete?" visible="false">
                    <ItemStyle HorizontalAlign="Center" Width="30%" />
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                            OnClientClick='return confirm("Are you sure you want to delete this entry?");'
                            Text="Delete?" visible="false" />
                    </ItemTemplate>
                </asp:TemplateField>
 
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    </form>

Open in new window

Avatar of b_levitt
b_levitt

You would need to use the DataGrid.OnItemDataBound event.  However, I'm not sure that your question makes sense - Container.DataItem exists for EACH item in the collection that your data grid is bound too.  Even if I gave you the code to do it, you only have a single session variable for potentially multiple items.

What are you trying to accomplish?
Agree with b_levitt.
Something like this in the ItemBound event:
<% Session["xmlfile"] =
DataBinder.Eval(Container.DataItem, "playerID") %>  
Have a look at this link as well:
http://www.velocityreviews.com/forums/t89335-assign-value-from-datagrid-to-a-session-variable.html
 
I'm not sure what faheem is doing in his example - OnItemDataBound is an event, yet he has inline code in his example.

But in any case I don't want to go down that road until I understand what you're trying to do.
the code of faheem will not work (tiny bug), because Container is not available once you are using normal code expression (i.e., outside <%#... %>, and inside <% ... %>). This would work, though:

<%# Session["xmlfile"] = DataBinder.Eval(Container.DataItem, "playerID"); %>
but like I said before, it will not have the effect you are after, which was seconded by b_levitt and faheem...
What it seems that you want to accomplish is to keep a reference to some object, but you do not want to make that visible to the end-user, and it is a different object for each row. You can accomplish that by doing the following and whenever you need it, you can use the usual FindControl technique to get your data from the hidden field:

<asp:HiddenField runat="server" ID="xmlfile" Value="<%# Container.DataItem.ToString() %>" />
BUT! Looking again at your code, you seem to want to redirect the user to some other page and send the current dataitem along. That's easy, try the following instead, and use:

Dim selectedXmlFile As String = Request.QueryString("xmlfile")
inside your LabelsEdit.aspx to get the selected xmlfile.

<asp:HyperLink ID="FileLink" 
    NavigateUrl='<%# "LabelsEdit.aspx?xmlfile" + Container.DataItem.ToString() %>'
    Text='<%# Container.DataItem.ToString() %>' 
    runat="server" Target="_self" />

Open in new window

small error not seen before posting. Use this instead (and you may need to UrlEscape it, I don't know what data is in there):

<asp:HyperLink ID="FileLink" 
    NavigateUrl='<%# "LabelsEdit.aspx?xmlfile=" + Container.DataItem.ToString() %>'
    Text='<%# Container.DataItem.ToString() %>' 
    runat="server" Target="_self" />

Open in new window

I appologize abel.  I didn't notice you had already informed the OP about the one-to-many problem with session and his DataItems.  At least we're all in agreement ;).
no apologies needed, we're all on the same page and it is good to find out that one's "backed up" by others ;-)
Hi Abel
I have thought about using URL passing variables.
But how do I GET the value on the next page in the "code Behind", and the use it in ALL the classes there ... ??

> But how do I GET the value on the next page in the "code Behind"

I showed you that line already, here it is again, use it in the Page_Load event. You can put it into a property or do whatever you want with it.

Dim selectedXmlFile As String = Request.QueryString("xmlfile")
> and the use it in ALL the classes there

Create a property in the class (page) that receives the request. Say we call that property "XmlFile" and it is a string, then you can do this in the Page_Load:

XmlFile = Request.QueryString("xmlfile")
that's really all there is to it ;-)
PS: any class that is run or called during a request/response cycle (which is basically anything you are doing), can use the Request object. So, it isn't that bad if you do not use a property and just use Request.QueryString("xmlfile"). You can call it anytime anywhere. Make note, that when someone accesses your page from somewhere else, that the string can be empty or Nothing.
I'm getting this failure on your Dim line :

Non-invocable member 'System.Web.HttpRequest.QueryString' cannot be used like a method.      

protected void Page_Load(object sender, EventArgs e)
        {
            String selectedXmlFile = Request.QueryString("xmlfile");

            if (!IsPostBack)
            {
                BindGrid();
            }
        }
Ok, now i'm getting it to work.
BUT I can't edit/update my xmldata in the gridview :
The sessionID that i'm using in my tmp-xmlfilename is changing on update.... an then it is no updated


     public String selectedXmlFile;
     public String selectedXmlFileTemp;
     public String RunOnce;
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
 
                selectedXmlFile = Server.MapPath("~/database/xmlfiles/" + Request.QueryString["xmlfile"]);
                selectedXmlFileTemp = Server.MapPath("~/database/xmlfiles/temp/" + Session.SessionID + Request.QueryString["xmlfile"]);
                File.Copy(selectedXmlFile, selectedXmlFileTemp);
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I close. Point to abel
What is that always when you try to understand askers and help them all the way through with extensive information and comments and then all you get is an undergrade? Please read the EE guidelines about the 10pts must principle. Remember, the expert trying to help you today you may need tomorrow.