Binding single value to aspx page - c#

Trying to bind a value in code behind to aspx page.
<div>
<table>
<tr>
<th>Item</th>
</tr>
<tr>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("ItemId")%>'/></td>
</tr>
</table>
</div>
Code behind:
int ItemId = 3;
NOTE: I have tried:
<%=ItemId%> AND <%:ItemId%>
But no joy! What am I doing wrong? All I want to do is to bind to a single value.

<asp:Label ID="Label1" runat="server" Text='<%# ItemId %>'/>
This is the proper format on the page, but you also need to execute a data binding event in your codebehind:
int ItemId = 3;
this.DataBind();
Although philreed has it right that since you are already using a label, you might as well just update the label.Text property with your value, rather than go through the data binding process. Using the <%# ItemId %> is best for when you want to stick a random data value in the middle of regular page markup, not an ASP control, like so:
<div>
The number you picked is <%# ItemId %>.
</div>
It still needs the DataBind() in your codebehind when doing this.

If its only ever the single value, why not use this in your code behind?
Label1.Text = ItemId.ToString();
and remove the <%# Eval("ItemId")%> from your markup.

<asp:Label ID="Label1" runat="server" Text='<%# ItemId %>'/>

Related

Custom user control with repeating data

Below is code that I'm aiming for. I need a custom user control that can be data bound to, but also contain other content (so just a raw repeater wont suffice). The end goal is something along the lines of:
<MyControls:Control1 runat="server" id="Control1">
<headertemplate>
<tr>
<td>ID</td>
<td>Username</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%#((User)Container.DataItem).ID %></td>
<td><%#((User)Container.DataItem).Username %></td>
</tr>
</itemtemplate>
</MyControls>
And:
var users = GetUsersList();
Control1.DataSource = users;
Control1.DataBind();
And looks like this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl1.ascx.cs" Inherits="Controls.MyControl1" %>
<asp:PlaceHolder runat="server" ID="Wrapper">
<h2>Results</h2>
<table>
<%=HeaderTemplate%>
<%
if(ItemTemplate.AnyItems()){
foreach(var item in ItemTemplate){
}
}
else
{
%>Nothing here<%
}
%>
</table>
<MyControls:AnotherControl runat="server" />
</asp:PlaceHolder>
I've found a page on ScottGu's Blog that appears to show what I want:
https://weblogs.asp.net/scottgu/Supporting-Templates-with-ASP.NET-User-Controls
But the linked to tutorial 404's now! All other examples I've found don't seem to be well written and very hard to pick apart.
Any help on how to achieve the above would be much appreciated.
It looks like you are getting 2 different Controls mixed up. UserControl and Repeater (I think).
To bind data in to a Control inside a UserControl, you need to make that Control accessible from the parent. You can do this by creating a public property inside the UserControl.
public Repeater myRepeater
{
get
{
return Repeater1;
}
set
{
Repeater1 = value;
}
}
UserControl ascx with the Repeater Control
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr>
<td>ID</td>
<td>Username</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ID") %></td>
<td><%# Eval("Username") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Now you have access to Repeater1 in the parent due to the public property.
Control1.myRepeater.DataSource = users;
Control1.myRepeater.DataBind();
And leave the control on the aspx parent empty.
<MyControls:Control1 runat="server" id="Control1"></MyControls>

How can I pass values of a listview from aspx page to another ?

I am working on class project where I am creating a listview in one .aspx page. I can display database through this list view but I cannot transfer the value of selected item from this .aspx page to another.
My designing code is like below:
<asp:ListView ID="lvPresent" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="lvPresent_ItemDataBound" >
<LayoutTemplate>
<table>
<tr>
<td></td>
</tr>
</table>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<td>
<asp:HyperLink ID="HyperLink1" runat="server">
<asp:Image ID="ImageButton1" runat="server" ImageUrl='<%#Eval("url")%>' Height="200px" Width="250px" />
</asp:HyperLink>
</td>
</ItemTemplate>
</asp:ListView>
What should I do to get this work done ?
To send URL to another page you can use QueryString.
Modify your HyperLink and add NavigateUrl
NavigateUrl='<%#"yourNextPageName.aspx?imgURL="+ Eval("url")%>'
just replace you code:-
<asp:HyperLink ID="HyperLink1" runat="server">
<asp:Image ID="ImageButton1" runat="server" ImageUrl='<%#Eval("url")%>' Height="200px" Width="250px" />
</asp:HyperLink>
with
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"yourNextPageName.aspx?imgURL="+ Eval("url")%>'>
<asp:Image ID="ImageButton1" runat="server" ImageUrl='<%#Eval("url")%>' Height="200px" Width="250px" />
</asp:HyperLink>
On that page, you can access image url like this:-
string imgurl = Request.QueryString["imgURL"];
Put an image control in that page assign this image on page load,
if your image control id is Image1 then you can assign on page load of that page like this:-
Image1.ImageUrl = imgurl
Look into server.transfer or response.redirect to pass data between pages..
A link on SO on the difference between the 2 and what they do:
Server.Transfer Vs. Response.Redirect
You can redirect to the other page and send the value in a query string
You can send the value in a session object

How to hide a column in a repeater?

Here, I found once the code to hide a repeater column easily. It works great like this.
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="label1" /></td>
<% if (MustBeVisible) { %>
<td"><asp:Label runat="server" ID="label2" /></td>
<% } %>
</tr>
</ItemTemplate>
But now, I need to apply a CLASS to the TableRow and make it runat="server" in order to apply a color condition in the ItemDataBound but when I add the attribute of runat="server" I have a conflict at runtime and a warning.
ASP.NET runtime error: Code blocks are not supported in this context
The idea is, for example, to evaluate in the ItemDataBound the label1, if it's true must apply a Class on TR to make it grey.
Any idea of the best approach or how to resolve this?
Approach 1:
First, Define a bool property, say ShouldBeGreyed in your data-item class (if possible). This property should return whether the data-item will be greyed out or not.
Then, Use this in your repeater markup:
<ItemTemplate>
<tr<%# ((bool)Eval("ShouldBeGreyed"))?"class='grey'":"" %>>
...
</ItemTemplate>
Approach 2:
First, define a method in code-behind, say ShouldBeGreyed, like this:
protected bool ShouldBeGreyed(object item)
{
// cast to your data-item
var dataItem = (<class-of-your-data-item>)item;
// Determine if it should be greyed out
// bool shouldBeGreyed = ...
...
return shouldBeGreyed;
}
Now use this in your repeater markup:
<ItemTemplate>
<tr<%# ((bool)ShouldBeGreyed(Container.DataItem))?"class='grey'":"" %>>
...
</ItemTemplate>
Use something like this.. Write the method in the Visible portion of the label.
<td><asp:Label runat="server" ID="label2" Visible="<%# MustBeVisible() %>" /></td>
if you want the td to be invisible/visible use this
<td runat="server visible="<%# MustBeVisible() %>"><asp:Label runat="server" ID="label2" /></td>
If you made the <td id="tablerow" runat="server"/> could you use do something like:
tablerow.Attributes.Add("class", className);

disabling clientidmode

I have a Repeater like this
<asp:Repeater runat="server" DataSourceID="HeaderFooterSqlDataSource">
<HeaderTemplate>
<table border="0" width="100%">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<input runat="server" id="SelectRadio" type="radio"
name="HeaderFooter" onclick='SelectAndSetHeaderFooter(this);" %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Now here when this Repeater is rendered the name attribute of my input radio "SelectRadio" gets auto generated but name attribute for all the radiobuttons in my repeater should be same so that they can work like a group & get checked / unchecked automatically according to other elements in that group, So how can I overcome this situation ??
Edit
I got the solution my self, Actually I have defined my input radio control as runat="server" because I thought otherwise Eval() binding wouldn't work but I was wrong Eval() binding does work without runat="server" , So when I remove that attribute name doesn't generated automatically and everything is fine now, But thanx to all for sparing time for my question.
Instead of hacking this you should use the built-in RaidoButtonList control.
<asp:RadioButtonList id="RadioButtonList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:RadioButtonList>
You want to look into the asp:RadioButton control (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.aspx).
In particular the GroupName property on this control can be used to "specify a grouping of radio buttons to create a mutually exclusive set of controls"
So roughly speaking:
<asp:RadioButton runat="server" id="SelectRadio"
GroupName="HeaderFooter" %>' />
Edit: It seems that in this particular situation GroupName doesn't do what it is designed for. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.groupname.aspx discusses it in the community content at the end but it boils down to the fact that it uses its current NamingContainer still as part of its name rather than just using the groupname you have given it. Thanks to the mysterious user1429080 for bringing that to my attention in comments.
The reason your radio buttons are not acting as a group is because you have runat="server" which will cause the name and id attributes to be prepended with parents' ids. The following will cause the radio buttons to behave as a cohesive group (note the lack of runat="server"):
ascx code:
<asp:Repeater ID="uxRadioSelections" runat="server">
<HeaderTemplate>
<table border="0" width="300px">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<input type="radio" value='<%# Eval("Name") %>' name="HeaderFooter" onclick="alert(this.value);"><%# Eval("Name") %></input>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
string[] names = new string[] {"Alvin", "Simon", "Theodore"};
uxRadioSelections.DataSource = names.Select(x => new { Name = x });
uxRadioSelections.DataBind();
}
However, the following will NOT perform as a cohesive group:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
Test
<asp:Repeater ID="uxRadioSelections" runat="server">
<HeaderTemplate>
<table border="0" width="300px">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton Text='<%#Eval("Name") %>' runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Content>
Due to the naming conventions you alluded to, these individual radio buttons will not find each other. ASP.NET uses RadioButtonLists to overcome this:
ascx code:
<asp:RadioButtonList ID="uxRadioButtons" runat="server">
</asp:RadioButtonList>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
string[] names = new string[] {"Alvin", "Simon", "Theodore"};
uxRadioButtons.DataTextField = "Name";
uxRadioButtons.DataValueField = "Name";
uxRadioButtons.DataSource = names.Select(x => new { Name = x });
uxRadioButtons.DataBind();
}
This does not give you as granular control, but will provide easy server-side access to the selections. If all you need is javascript-related functionality and increased formatting flexibility, the first format may (which is nearly identical to what you provided) is your best bet. Good luck!
Another option is to set the ClientIDMode as 'static' which will set its value to that of the ID specified. Since it is a repeater control, all the items generated will thus have the same generated ID. So you simply set:
<asp:RadioButton runat="server" id="SelectRadio" GroupName="HeaderFooter" ClientIDMode="static">' />
This means that all generated controls will have the same ID specified i.e. "SelectRadio". This may or may not be problematic and you should be careful that this is exactly what you want.
Just a note, ClientIDMode is a feature of ASP.Net 4.0. More info here: ClientIDMode in ASP.NET 4.0 and here: MSDN. I hope this helps.
Update 1:
You can get more insights from this question asked before on StackOverflow. (It's jQuery though).

Need help with repeater

This is my repeater:
<asp:Repeater ID="myRepeater" OnItemCommand="myRepeater_ItemCommand" runat="server" OnItemDataBound="myRepeater_OnItemDataBound">
<HeaderTemplate>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tr class="lgrey">
<td>Default</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
What I want is that when user clicks on any of
the "lnk1" link button in the list that repeater renders,
the link should be replaced with the label "label1"..
i.e. when the user clicks on "Make Default" link, it should be replaced with "Yes" label
Calling this method obj.SetDefaultAddress(); is setting the default address in the DB alright..
problem is with the display of the label1 and lnk1 when the repeater renders...
what is happening is that BOTH "Make Default" LinkButton and the "YES" label are getting displayed
under the "Default" column of the table inside my repeater.
I want some code that will check the "IsDefault" value in my DB and display "Make Default " link button
and "YES" label accordingly... i.e. if IsDefault's value in the DB is TRUE then "YES" should be displayed in the repeater
otherwise "Make Default"
Are you sure your piece of code in code behind under ItemCommand is executing?
I only changed the CommandName from SetDefault to SetDefaultAddress in aspx file to match with the one in code behind, it worked.
Where to start...
I think what's causing your problem is that the SelectedItem and the DefaultAddress are not mapped to each other, so when you click the button you're getting the selected index set and the OnItemDatabound event is showing/hiding what you want, but when the grid is initialized from the database, the SelectedItem is not being set.
I don't know what your datasource is, and there's obviously more code to this than what you've posted, but if you can look at the e.Item.DataItem in the myRepeater_ItemDataBound handler, you can set the current item as selected when the address is the default (e.Item.ItemType... or use your "selectedIndex" counter)
I will probably do it from markup itself - this is assuming that you have "IsDefault" column/property of bit/boolean type in your data-source indicating the address is default. So use following markup:
...
<tr>
<td>
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible='<%# !Eval("IsDefault") %>' CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible='<%# !Eval("IsDefault") %>'></asp:Label>
</td>
</tr>
...
You need to control visibility based on property in your data source (either using markup or ItemDataBound event). Also when SetDefault link is clicked, you must either re-bind the repeater new state or toggle visibility explicitly (as your current code is doing).
EDIT:
If data binding expression are not working then you have to do it in ItemDataBound event. I see that you have already tried that but there is one mistake - bllUsers obj=new bllUsers(); will always have IsDefault as false - you need to use data item. For example,
protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
bllUsers obj = e.Item.DataItem as bllUsers;
((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault;
((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault;
}
}

Categories