How can I do Eval() in a DropDownList that are inside a GridView?
I tried the follow code, but still wrong:
<asp:TemplateField HeaderText="Prioridade">
<ItemTemplate>
<asp:DropDownList ID="ddlPrioridade" runat="server"
SelectedValue='<%# Eval("Prioridade") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Try this way
<asp:DropDownList ID="ddlPrioridade" runat="server" AppendDataBoundItems="true" DataSourceID="datasource" DataTextField="key" DataValueField="data" AutoPostBack="True" SelectedValue='<%# Eval("Prioridade") %>'>
also have a look at this DropDownList inside a GridView (or DataGrid). It might help you
You are receiving this error because the drop down list is not loaded with items and asp.net is trying to set the selected value of an empty drop down list and that's why it gives you error. So fill the drop down list first before setting it's selected value.
Related
I'm working on an aspx page where I'm trying to reference a dropdownlist from my code behind, now I add the dropdownlist to the page (ddlProgram) and its fine but soon as I move it into the InsertItemTemplate or EditItemTemplate or my GridView where I need it to go the designer entry disappears and I can no longer reference it from the code behind, is there a way around this? I have tried using the FindControl("ControlID") method but it always returns null. I need to set the selected index to 0 when the cancel button is click on my page. Thanks!
<asp:TemplateField HeaderText="Program" SortExpression="IDProgram">
<EditItemTemplate>
<asp:DropDownList ID="ddlProgram" AppendDataBoundItems="true" runat="server" DataSourceID="odsProgram" DataTextField="NameWithCode" DataValueField="IDProgram" CssClass="form-control ddl-normalize">
<asp:ListItem Text="No Program" Value="-1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="programName" runat="server" Text='<%# getProgramName((Eval("IDProgram")?? string.Empty).ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I'm working on an ASP.net Web Forms application (not MVC) and I have a DropDownList wrapped by a TemplateField in a DetailsView like this:
<asp:TemplateField>
<ItemTemplate> </ItemTemplate>
<EditItemTemplate> </EditItemTemplate>
<InsertItemTemplate>
<div class="form-group">
<asp:DropDownList ID="myStatus" runat="server" DataSourceID="DBStatus"
DataTextField="DESC" DataValueField="CODE"
Enabled="false" SelectedValue='<%# Bind("Status") %>'>
</asp:DropDownList>
</div>
</InsertItemTemplate>
</asp:TemplateField>
as you can see at insertion its not enabled (Enabled="false")
for simplicity I wanted to use the insertion items/fields to achieve a search
so I want to enable my DropDownList for that search
I tried using these from code-behind on DetailsView_PreRender and Page_Load but nothing worked:
DropDownList Status = DetailsView1.FindControl("myStatus") as DropDownList;
Status.Enabled = true;
and i also tried
Status.Attributes.Add("enabled", "enabled");
i also debugged it, and these commands are executed but the DropDownList doesn't want to be enabled.
any thoughts ?
thanks.
Hello Everyone
I have a detailsview, where there are 3 bound fields and a template field.
The template field has a DropDownList, which I have connected to an AccessDataSource.
But when I run, the dropdownlist has just the "System.Data.DataRowView" as it's items.
I wish to get the items from DB to be listed down in DropDownList
This is my code
asp:TemplateField HeaderText="State/Province" SortExpression="State/Province">
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="AccessDataSource1"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/db1.mdb"
SelectCommand="SELECT [State/Province_name] FROM [State/Province_List ]">
</asp:AccessDataSource>
</EditItemTemplate>
</asp:TemplateField>
Should I add "DataBinding" or "DataBound" event for DropDownList?? to make it perfect?
Help me regarding this issue
Thanks,
Arjun
Define the filed to use in the drop down list
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="State/Province_name"
DataValueField="State/Province_name"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
If you have problems with this non-standard column name, try to use an alias
SELECT [State/Province_name] AS StateProv FROM [State/Province_List ]
(Is the space in [State/Province_List ] OK?)
Then use this one
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="StateProv"
DataValueField="StateProv"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
Im using a LINQDataSource to populate a GridView of Universities.
Each University has an associated State, which is in another table and associated by a foreign key (StateID).
I have a TemplateField in the GridView so that when you view it normally it displays the StateName which comes from the State table and when you edit it shows a DDL populated with everything from the State table.
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStateEdit" runat="server"
DataSourceID="LinqDataSourceStates" DataTextField="StateName" DataValueField="StateID"
SelectedValue='<%#Eval("StateID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("State.StateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
If I debug, inside of RowUpdating, GridViewUpdateEventArgs.NewValues doesnt even have a key for State.
Question: How do I let my gridview know I want it to update this column? All the BoundFields just seem to work...
In EditItemTemplate you should use #Bind("StateID") instead of #Eval("StateID").
I have a TemplateField in a DetailsView and its input should be one of a few choices in a lookup table. Currently it's a text field, but I want it to be a group of radio buttons, and it should work in both insert and edit mode (the correct current value should be selected in edit mode).
How do I create mutually exclusive radio buttons and databind them in a DetailsView TemplateField?
I'm on ASP.NET 3.5 using an Oracle database as a datasource.
<EditItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
DataSourceID="LookupSqlDataSource" DataTextField="LOOKUPITEM_DESCRIPTION"
DataValueField="LOOKUPITEM_ID" SelectedValue='<%# Bind("ITEM_ID")%>'>
</asp:RadioButtonList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
DataSourceID="LookupSqlDataSource" DataTextField="LOOKUPITEM_DESCRIPTION"
DataValueField="LOOKUPITEM_ID" SelectedValue='<%# Bind("ITEM_ID")%>'>
</asp:RadioButtonList>
</InsertItemTemplate>