Set SelectedValue of DropDownList inside DataGrid - asp.net

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 >

Related

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

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.

User control (ascx) in asp.net and its bindable properties dont show up in the data container

When I add a user control which has bindable properties to an asp.net page I do not see its bindable properties on the designer's dialog box when I click the edit Data bindings on the Repeater-Listview
should I add any other propery descriptors?
the markup would be like this inside a repeater or similar control
<uc1:Menu ID="Menu1" superman="<% Eval("userid") %>" runat="server" />
on the bindable property I only use
[System.ComponentModel.DefaultBindingProperty("superman")]
[System.ComponentModel.Bindable(true)]
public int superman
{
get;
set;
}
People discuss it here but they couldn't find a solution
I think this might be a problem with Visual Studio, I have not found a way to show the properties of user controls, but still, you can bind the property even when it does not appear in the options box:
This is how you bind it:
In the user control code behind:
[Bindable(true)]
public string MyProperty
{
get
{
return this.lblMessage.Text;
}
set
{
this.lblMessage.Text = value + DateTime.Now.ToString();
}
}
In the ASCX file:
<asp:Label ID="lblMessage" Text="something" runat="server" />
In the ASPX page:
<asp:DataList runat="server" ID="dataList" Width="50%"
DataSourceID="ObjectDataSource1">
<ItemTemplate>
<uc1:WebUserControl1 runat="server" ID="myUC" MyProperty='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetProducts" TypeName="WebApplication1.Repository">
</asp:ObjectDataSource>
Simple just add the property you created in your UC in the tag:
<uc1:WebUserControl1 runat="server" ID="myUC" MyProperty='<%# Eval("Name") %>' />
This is the output:
I think you just make the markup by typing it,
the user control must be drag and drop to the page in the design view.
try this may helps.

ASP.NET Binding integer to CheckBox's Checked field

I have a following ListView item template, in which I am trying to bind integer value to Checked property of CheckBox.
IsUploaded value contains only 0 and 1...
<asp:ListView ID="trustListView" runat="server">
<ItemTemplate>
<asp:CheckBox ID="isUploadedCheckBox" runat="server"
Checked='<%# Bind("IsUploaded") %>' />
</ItemTemplate>
</asp:ListView>
But ASP.NET complains that
Exception Details: System.InvalidCastException: Sepcified cast is not valid
Even though following code using DataBinder.Eval() works,
I need to have a 2-way binding, thus need to use Bind().
<asp:CheckBox ID="isUploadedCheckBox2" runat="server"
Checked='<%# Convert.ToBoolean(
DataBinder.Eval(Container.DataItem, "IsUploaded"))) %>' />
How can I convert 0's and 1's to boolean using Bind()?
[ANSWER]
I have extended auto-generated type through partial class by adding a new property mentioned in the answer by Justin
If you're willing to change the class, add a property on the class that's a boolean
public bool IsUploadedBoolean
{
get { return IsUploaded != 0; }
set { IsUploaded = value ? 1 : 0; }
}
If not, you may have success with a TypeConverter:
Create a custom TypeConverter which will handle 0 and 1 to boolean conversions
Stick the TypeConverterAttribute on the IsUploaded property to direct .NET to your custom typeconverter.
How about adding a property to your class which does the conversion?
public bool IsUploadedBool
{
get { return IsUploaded == 1; }
}
and then bind to this IsUploadedBool property instead of directly to the underlying INT.
Marc
Kind of a cheesy work around would be to use a drop down list with list items to give the same effect:
<asp:DropDownList ID="ddlBool" runat="server" SelectedValue= '<%# Bind("IsUploaded") %>'>
<asp:ListItem Text="True" Value="1" />
<asp:ListItem Text="False" Value="0" />
</asp:DropDownList>
For more information visit:
http://dhondiyals.wordpress.com/2010/05/03/binding-checkbox-with-integer-value-in-gridviewtrick/
How about (btw i am using a stored procedure)
Aspx page
<asp:CheckBox ID="social_facebook" runat="server" Checked='<%# Bind("True") %>' />Facebook
Code behind
cmd.Parameters.Add("#p_facebook", SqlDbType.Bit).Value = social_facebook.Checked;
Solution:
Hi, I'm also using Ajax rating inside a GridView and I ran into this problem. After looking through several forums, I tried this and it worked for me. I hope it helps you:
<%# Convert.ToInt32(Eval("EVALUATION")) %>
returns an integer, since you're using Checked which is an integer.

ASP.NET: Bind a value to a custom user control inside a repeater

I have an ASP.NET control that binds data to a repeater. Inside that repeater, I have another custom user control. I want to pass a value to this second control based on the current binding item.
<asp:Repeater runat="server" ID="ProductList">
<ItemTemplate>
<p>Product ID: <%# Eval("ProductID") %></p>
<myControl:MyCoolUserControl runat="server" ProductID='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:Repeater>
The repeater item template correctly prints out my Product ID with the Eval statement, but when I do the same thing to pass the Product ID to MyCoolUserControl, it doesn't work (if ProductID on MyCoolUserControl is a Nullable Int32 - I can debug it and it's always null).
Any ideas how I can do this?
I did a small test and I got it working if the ProductID is a string. After I changed it to and int in the usercontrol I got kind of the same problems.
I did a int.Parse in the datasource to the repeater and got it working again.
Check to see that the ProductId that you pass into the repeaters datasource is of type int.
Mytest app.
string[] values = new string[]{ "12", "13" };
MyRepeater.DataSource = from v in values
select new
{
ProdId = int.Parse(v)
};
MyRepeater.DataBind();
<asp:Repeater runat="server" ID="MyRepeater">
<ItemTemplate>
<My:control runat="server" ProductId='<%# Eval("ProdId") %>' />
</ItemTemplate>
</asp:Repeater>
and in the usercontrol:
public int? ProductId
{
set { MyLabel.Text = value.Value.ToString(); }
}

CheckBoxList with multiple values bound to a single flag enum

I have a FormView (bound to an ObjectDataSource) that contains a CheckBoxList that I'd like to bind to a single property of the underlying object that is an Enum with the FlagsAttribute applied to it. Binding to the SelectedValue property always gives me just the FIRST selected value from the list as the property's value. Anyone know how to get around this without overriding the Inserting or Updating methods and manually getting the values of the checkbox list and stuffing it into the parameters of the datasource? Sample code below of what I'm trying to do...
<asp:FormView runat="server" ID="MyFormView" DataSourceID="MyDataSource">
<InsertItemTempate>
<asp:CheckBoxList runat="server" ID="MyCbl" SelectedValue='<%# Bind("MyProperty") %>'>
<asp:ListItem Text="Choice 1" Value="ChoiceOne"></asp:ListItem>
<asp:ListItem Text="Choice 2" Value="ChoiceTwo"></asp:ListItem>
</asp:CheckBoxList>
</InsertItemTemplate>
</asp:FormView>
<asp:ObjectDataSource runat="server" ID="MyDataSource" TypeName="MyClass" ...></asp:ObjectDataSource>
behind the scenes, my object is declared like this...
public class MyClass
{
public MyEnum MyProperty { get; set; }
}
[Flags()]
public Enum MyEnum
{
ChoiceOne = 1,
ChoiceTwo = 2
}
You will have to iterate through the Items collections and build up the enum values from there.
A search on Google for FlaggedEnumTypeConverter should also be helpful.

Resources