Accessing C# session varible in ASP.Net - c#

I am trying to access a session variable in aspx but I think there is an issue with my syntax somewhere.
Under page load:
Session["UserName"]= username.Substring(8).ToString(); // This is ok as far as I know.
Issue is with the asp bit:
<asp:TextBox ID="ATextBox" runat="server" Text="<% Session["UserName"] %>" />
<asp:TextBox ID="ATextBox" runat="server" Text="<%# Session["UserName"] %>" />
I have also tried the above (one at a time) with .ToString() at the end too. But I keep getting errors:
Either The server tag is not well formed or the server tag contains % %
One thing that might make a difference is the Textbox is inside a GridView, (it is inside a ContentTemplate)but not a boundfield.

Try this:
<asp:TextBox ID="ATextBox" runat="server" Text='<%# Session["UserName"] %>' />
Not sure if it would help..

Related

<%= %> tag not working to display content inside an ASP Label control

Noob question.
Why does this not work into my .aspx file?
<body>
<asp:Label ID="Label1" runat="server" Text='<%=System.DateTime.Today.Day.ToString()%>' ></asp:Label>
</body>
It does display the <%=System.DateTime.Today.Day.ToString()%> string which is obviously not what I want.
Same result if I try to display the content of a code behind variable:
<asp:Label ID="label" runat="server" Text='<%= versionNumber %>' >
versionNumber being properly instanced and set into the code behind.
You cannot mix server controls with code blocks.
There are two ways to work around that limitation:
Just use <%=System.DateTime.Today.Day.ToString()%> without a Label around it
Use codebehind to set Label1.Text = System.DateTime.Today.Day.ToString();
The first way will display the date to the user, but you cannot further change it from codebehind.
The second way does enable you to alter the text from codebehind.
It is true you can't mix server controls with Code blocks,
If its compulsory for you to use Server side control, and you don't even want to set value from code behind then you can go for this solution.
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>
Similarly you can use code behind variable as follows ,
<asp:Label ID="Label1" runat="server"><%=versionNumber %></asp:Label>
If you really want to use a asp:Label
Use it as follows:
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>

ASP Grid inside of Bootstrap popover using Eval Tags

I have a ASP Gridview that has a column with a button that opens a popover. Inside the popover I want to populate an ASP Table or ASP Gridview to show hidden related data. I am able to open the popover and it displays hard coded data. But when I want to make the data result dynamic using the Eval() tag the syntax is not able to read the text="<%# Eval('Label1') %>" tags within the string within the object.
The purpose is to load the entire data set and the popover data on one page load. I am wanting to avoid reload if possible. It seems that it cannot handle the <% within the string, is there a way to handle for this?
Any advice is welcome. Thank you in advance.
Sample code:
<asp:Button ID="Button3"
runat="server"
class="btn btn-info btn-xs"
Text="+"
OnClientClick="return false;"
data-toggle="popover"
data-trigger="focus"
TabIndex="0"
data-placement="right"
title="Owner Change"
UseSubmitBehavior="true"
data-content='<asp:Gridview runat="server" ID="gdvTest">
<Columns>
<asp:TemplateField HeaderText="Label One">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label1') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label Two">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label2') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
/>
Update: I also attempted to use ASP Table, but my class is not recognizing the ID im trying to use to dynamically populate the Label.
...data-content="<asp:Table><asp:TableRow><asp:TableHeaderCell text='TestHeader'><asp:Label runat='server' ID='lblTest' style='color:Red'></asp:TableHeaderCell></asp:TableRow></asp:Table></asp:Gridview>"
Your syntax is a bit off indeed. Remember that everything inside <%# %> should be a valid C#, so 'Label2' is an invalid literal. To fix that, swap quotes over:
text='<%# Eval("Label1") %>'
Update.
Missed the fact that GridView is itself surrounded by single quotes of data-content attribute. Even though this markup looks quite weird, there is a way to make it work, using double quotes everywhere and escaping appropriately:
text="<%# Eval("Label1") %>"

How to Insert image on web page using image name pulled from access database?

I'm developing a website. I have an Access database that I pull image names from that i want to use as parameters to post images from my folder using C# code in VS 2010. I want to know how to access the image name as a string and insert it into my asp image element. I'm guessing I need to store the results of my "Select" query in a variable in order to pass it through the "Eval" function, but I'm just guessing and nor have I figured out how to do it.
The following section of code is where I established a connection with the database, used the Datalist control to list the information on my webpage that this code is for. And so what I'm trying to ultimately do is use the file name that's in the database as a parameter to grab the correct image from my files and post it to my web page. See below in code where I say FILENAME GOES HERE????? to see where I need to put my filename for my picture.
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/TravelJoansDB.accdb"
SelectCommand="SELECT * FROM [Table2]"></asp:AccessDataSource>
<asp:DataList ID="DataList1" DataSourceID="AccessDataSource2"
ItemStyle-VerticalAlign="Middle" runat="server" DataKeyField="ID">
<ItemStyle VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
Image:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "PlaceImages/" Eval(*FILENAME GOES HERE?????*) %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
try this
<asp:Image ID="Image2" runat="server" ImageUrl='<%# "PlaceImages/" + Eval("Your_ImageName_Column")%>' />

Converting string to UniqueIdentifier - asp.net SQL search based on asp:Label

Hi I'm currently trying to perform a search using my SQLDataSource but want to use the WHERE statement to find the GUID that I have already assigned to a Label, however I'm getting the error "Conversion failed when converting from a character string to uniqueidentifier".
Ive been reading through forums just like this for days now and I'm just getting myself into all sorts of mess with code, Ive tried using C# in the codebehind based on SqlSelecting and buttons to populate the ListView that Im working with, ListView_Selecting etc etc
I'm pretty new to asp.net and C# so maybe I've not understood some of the forums Ive been on, either way I'd appreciate any and all help anyone can give, I've included the code below.
Thanks in advance.
(NESTED WITHIN ANOTHER LISTVIEW)
<asp:Label ID="CommId" Text='<%# Eval("CommentId") %>' runat="server" Visible="false"></asp:Label>
<asp:ListView ID="viewComments1" DataSourceID="AddCommentsSource" runat="server">
<LayoutTemplate> <span ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("Comment") %>/>
<asp:Label ID="CommentDateLabel" runat="server" Text='<%# Eval("CommentDate") %>' />
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="AddCommentsSource" runat="server"
ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>"
SelectCommand="SELECT myComment.Comment, myComment.CommentDate, aspnet_Users.UserName FROM aspnet_Users, myComment, GuestbookComments
WHERE myComment.CommentId = 'CommId' AND aspnet_Users.UserId = myComment.UserId
ORDER BY myComment.CommentDate DESC" >
</asp:SqlDataSource>
I'd suggest using code-behind to dynamically issue your query based on your parameters, also use named parameters for convenience and safety. As for converting a string to a GUID, then it's fairly simple:
Guid yourGuid = new Guid(yourString);

Error handling when inserting a null value, asp.net c#

i have a webpage built in asp.net c#. it always a user to create a new record in a db table. there are there are two input fields, text and score. text cannot be a null value so if the user doesn't input text onsubmit, the page errors out. i want to throw in some simple error handling code in the code behind page. i've tried including an if/else on_inserted method but ran into some java script errors. any help would be apprieciated. thanks.
aspx page -----------------
<EditItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</InsertItemTemplate>
why not use RequiredFieldValidator validator? it works inside the grid
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="TextBox1"
Display="Static"
ErrorMessage="*"
runat="server"/>
Also don't forget to use page is valid before saving to the database.
if (page.isValid){
//send to db
}

Categories

Resources