conceptdata
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
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>
ASKER
Okay, but In my code I need ( didn't explain before) to set the session("xmlfile") to the value from Container.DataItem.ToStrin g()...
Can you give a hint or better, some code ..
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?
ASKER
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>
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?
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/forum s/t89335-a ssign-valu e-from-dat agrid-to-a -session-v ariable.ht ml
Something like this in the ItemBound event:
<% Session["xmlfile"] =
DataBinder.Eval(Container.
Have a look at this link as well:
http://www.velocityreviews
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.
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.ToStrin g() %>" />
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("xmlfi le")
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" />
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" />
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 ;-)
ASKER
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 ... ??
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.
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:
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("xmlfi le")
> 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("xmlfi le")
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("xmlfi le"). 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.
ASKER
I'm getting this failure on your Dim line :
Non-invocable member 'System.Web.HttpRequest.Qu eryString' cannot be used like a method.
protected void Page_Load(object sender, EventArgs e)
{
String selectedXmlFile = Request.QueryString("xmlfi le");
if (!IsPostBack)
{
BindGrid();
}
}
Non-invocable member 'System.Web.HttpRequest.Qu
protected void Page_Load(object sender, EventArgs e)
{
String selectedXmlFile = Request.QueryString("xmlfi
if (!IsPostBack)
{
BindGrid();
}
}
ASKER
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
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);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
http://kb2.adobe.com/cps/1