How to call a code-behind method from aspx page? - asp.net

I've an object that contains a field called DevList which is defined like this
public List<string> DevList { get; set; }
I also defined a method called DisplayListOfDevelopers that is supposed to concatenate the list of developers and display it as a one string.
This is how I'm calling the method from aspx.
<asp:TemplateField HeaderText = "Developer(s)">
<ItemTemplate>
<asp:Label
ID="_lblDevList"
runat="server"
Text= '<%# DisplayListOfDevelopers(DevList) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
But, I'm getting this error: The name 'DevList' does not exist in the current context
Am I missing something?
EDIT
_gvStatus = ds;
_gvStatus.DataBind();
Where ds is just a list of objects that contains the DevList for now.
Thanks for helping

Assuming this is how your class looks:
public class MyItem
{
public List<string> DevList { get; set; }
}
And that
ds = List<MyItem>();
Do this:
In your code-behind:
protected string DisplayListOfDevelopers(object _devList)
{
//Cast your dev list into the correct object
}
In your markup:
<asp:TemplateField HeaderText="Developer(s)">
<ItemTemplate>
<asp:Label
ID="_lblDevList"
runat="server"
Text='<%# DisplayListOfDevelopers(Eval("DevList")) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Just be sure to make the function in your code-behind is protected or public.

Related

Set SelectedValue of DropDownList inside DataGrid

I got a datagrid where a datasource #1 is bound to, e.g.
public class Class
{
public string Val { get; set; }
public string Val2 { get; set; }
}
List<Class> classes = new List<Class>();
dgr.DataSource = classes;
Inside this datagrid i got a listbox for each row with a datasource #2 bound to:
<Columns>
<asp:TemplateColumn HeaderText="Spaltenname">
<ItemTemplate>
<asp:ListBox runat="server" DataTextField="Text" DataValueField="Value" DataSource="<%#oParentTablesHandler.DataTableXYZ%>" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
This works, but I have problems setting the SelectedValue.
SelectedValue="<%# "" %>
will work but I need a selection depending on Val from datasource #1. How can I do that? I need to use Eval i guess, but
SelectedValue="<%# Eval("Val") %> did not work...
edit: I found out that I want to select an item by text and not by value, argh. Is there a way to do that?
Try like this..
<%# ((Class)Container.DataItem).Val %>
UPDATE:
well.there may be some value in 'Val' property that may not exist in your Datasource#2's corresponding column...
So for test purpose try following..
<asp:ListBox .. AppendDataBoundItems="true">
<Items>
<asp:ListItem Text="NA" Value="" />
</Items>
</asp:ListBox >

How to Bind complex data to control in ListView (class User contain instance of class UserType)

I have 2 value class
public class UserType
{
public int ID;
public string TypeName;
}
public class User
{
public int ID;
public string UserName;
public UserType Type;
}
1 proccessing class
public class Users
{
public User[] GetUsers()
{
//Retrive and re turn User array
}
public int Update(User user, User old_user)
{
//Update user
}
}
1 ListView and 1 ObjectDataSource
<asp:ListView ID="lsvUser" runat="server" DataKeyNames="ID" DataSourceID="odsUser"
ItemPlaceholderID="plhItem" onitemupdating="lsvUser_ItemUpdating">
<LayoutTemplate>
<ul><asp:PlaceHolder ID="plhItem" runat="server"></asp:PlaceHolder></ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
<asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
<asp:Label ID="lblTypeName" runat="server" Text='<%# Eval("Type.TypeName") %>'></asp:Label>
<asp:LinkButton ID="btEdit" runat="server" CssClass="button" CommandName="Edit" Text="Edit"></asp:LinkButton>
</li>
</ItemTemplate>
<EditItemTemplate>
<li class="editRow">
<asp:Textbox ID="txbUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:Textbox >
<asp:Textbox ID="txbTypeName" runat="server" Text='<%# Bind("Type.TypeName") %>'></asp:Textbox >
</li>
</EditItemTemplate>
</asp:ListView>
<asp:ObjectDataSource ID="odsUser" runat="server" TypeName="BLL.Users" DataObjectTypeName="BLL.User" SelectMethod="GetUsers" UpdateMethod="Update" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="old_{0}"></asp:ObjectDataSource>
When data is loaded into the ItemTemplate, the ListView interprets Type.TypeName and binds correctly,
but when updating the record, I cannot retrieve the old_user.Type from the old values. So old_user.Name and old_user.ID have values but old_user.Type is null.
Help me. I have been searching for about 2 days and cannot find a solution. I can bind data for a new user like this:
protected void lsvUser_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues.Add("Type", new BLL.UserType() { TypeName = Convert.ToString(((TextBox)lsvUser.EditItem.FindControl("txbTypeName")).Text) });
}
But I don't know how to retrieve the old values (how to retrive value of lblTypeName).
Help me. If there is a solution that doesn't require additional code in the ItemUpdating event, please teach me.
Thank you.
Add Type.TypeName as a DataKey, like this:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Type.TypeName">
And in your ItemUpdating event handler, retrieve the key value like this:
string typeName = (string)e.Keys["Type.TypeName"];
I'm not positive that "Type.TypeName will work, but some variation of that should work, whether it's Type.TypeName or just TypeName.

Repeater and public array as NOT datasource.

I want to using two dataSources with repeater. One is binded to dataSource property so my question is is that possible to f.ex. to also repeat some array of string as public property?
It is possible to bind to a property of your datasource that is also a collection. For example:
class Person
{
List<Phone> Phones { get; set; }
string Name { get; set; }
}
class Phone
{
string Number { get; set; }
}
void Page_Load(...)
{
List<Person> people = GetPeople();
peopleRepeater.DataSource = people;
peaopleRepeater.DataBind();
}
aspx page
<asp:Repeater ID="peopleRepeater" runat="server">
<ItemTemplate>
Name : <%# Eval("Name") %>
Phones: <br/>
<asp:Repeater ID="phonesRepeater" runat="server" DataSource='<%# (Container.DataItem as Person).Phones %>'>
<ItemTemplate>
<%# Eval("Number") %> <br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Most ASP.NET Data controls only will bind to a single data source at a time. It is possible to have nested controls that can be bound to multiple data sources. Some 3rd party controls such as RadGridView are designed to handle multiple datasource binding (e.g. hierachical).

extension method in gridview eval

I have an extension method that I wrote but when I use it on an aspx page to render a label inside a gridview, it doesn't work.
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("TheStatus").ToMyStatus() %>'></asp:Label>
The extension method is a file called Appfunctions.cs:
public static class Extensions
{
public static string ToMyStatus(byte TheStatus)
{
//mycode
}
}
I've seen posts that say a namespace needs to be added with the Imports statement in the aspx page but the extention ToMyStatus is not in any particular namespace.
Any suggestions much appreciated.
Thanks.
You have missed this and type conversion.
public static class Extensions
{
public static string ToMyStatus(this byte TheStatus)
{
return "Hello : " + TheStatus;
}
}
Markup
<asp:Label ID="lblStatus"
runat="server"
Text='<%# ((byte)Eval("TheStatus")).ToMyStatus() %>'>
</asp:Label>

calling a function in repeater asp.net

I want to call a function to bind data to Repeater . Do I need to Set dataSource Property of this control or Repeater .DataBind() will work.
<asp:Repeater ID="RepeaterDays" runat="server">
<ItemTemplate>
<ul>
<asp:Label ID="lblDays" runat="server" Text='<%#ChandanIdiot()%>'></asp:Label>
</ul>
</ItemTemplate>
</asp:Repeater>
I have written RepeaterDays.Databind(), but the function is not called.
This is displaying nothing.
Is ChandanIdiot() a protected function that returns a string?
protected string ChandanIdiot() {
return "test";
}
If you want to actually do some data processing, you will have to include a parameter:
protected string ChandanIdiot(object obj) {
return "test " + obj;
}
And, assuming that there is a property called "Name" on the object that you are reapeating, you would have the following:
<asp:Label ID="lblDays" runat="server" Text='<%# ChandanIdiot(Eval("Name")) %>' />
Source:
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<%# ChandanIdiot( Eval("product_unitprice"))%>
<!--product_unitprice is table colomn name -->
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
C#:
protected string ChandanIdiot(object ob) {
string typ = ob.ToString(); //selected value stored in ob
if (typ == "some function") {
//do somthing
}
return typ ; //value return to <%# ChandanIdiot( Eval("product_unitprice"))%>
}

Resources