Databound DropDownList not selecting correct item - c#

I have a DropDownList whose SelectedValue and DataSource are both databound. The control always selects the first item in the list regardless of the SelectedValue. The correct value is passed to the database when updating the value but the first item is always selected. What am I missing here?
<asp:DropDownList ID="SendAsDdl" runat="server"
SelectedValue='<%# Bind("SendAsId") %>' EnableViewState="true"
DataSource='<%# CM.Email.Users.GetSendAsList(OfficeId) %' />

You can't put scriplets into server side controls. You have to set the SelectedValue from your code behind:
SendAsDdl.SelectedValue = this.SendAsId;

Related

DataBinder.Eval Issue

I have a some controls inside an EditItemTemplate within a RadListView control:
<telerik:RadComboBox ID="cbCategoryTypeTueET" runat="server" Skin="Office2010Black"
SelectedValue='<%# DataBinder.Eval(Container.DataItem, "CategoryTypeID") %>'
TabIndex="1" Width="100%" EmptyMessage="--Select Category Type--" DataSourceID="edsCatTypeTueET"
DataTextField="CategoryName" DataValueField="CategoryTypeID" AutoPostBack="True"
OnSelectedIndexChanged="cbCategoryTypeTueET_SelectedIndexChanged" AccessKey="t" AppendDataBoundItems="True">
</telerik:RadComboBox>
<asp:EntityDataSource ID="edsCatTypeTueET" runat="server" ConnectionString=""
DefaultContainerName="ATITimeEntry" EnableFlattening="False" EntitySetName="TimeTrackingCategoryTypes"
Select="it.[CategoryTypeID], it.[CategoryName]"
Where="it.deletedFlag = false AND it.activeFlag = true" >
</asp:EntityDataSource>
The Entity datasource does have a connection string - I am using a new code generation template - so this is not an issue.
My problem is I want the combobox to bind on edit. But if activeFlag is false or deletedFlag is true (or both) the Radlistview will not go into edit mode. Is there an elegant way to do this with markup or some elegant query?
My understandig is, you want to forbid edit on some conditions.
You have to subscripe the ItemDataBound Event from the RadListView. There you can cast DataItem to your object and check the condition (Get Data being bound to ListView).
Then you can access your controls and manipulate them (like hide them)...
Conditionaly disable command button

Deleting Selected ListViewItems from the Database

I have a ListView that I populate from the database. The ListView has a Checkbox on each row which is a built-in property of ListView.
I have been trying to get the text from the ID column next to the Checkbox which I will use in deleting the respective rows from the database but it all fails. I thought I could get the ListView.CheckedIndices and use them in return to point to the SelectedText of each index.
However all fails. More specifically i wanted to use the OnItemDeleting Event of list view but the ListView I have does not seem to have such event.
Is there any way i can achieve this?
I have had this problem before. What I did was this below:
<asp:ListView ID="xyz" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="blah" OnClick="blah_Click"
CommandArgument="<%# myObject.Id %>" />
</ItemTemplate>
</asp:ListView>
I got the passed id through the command argument.Then I deleted the selected item and refreshed the data for listView. I am sure there must be a cleaner way of doing it.
Let me know how it goes.

Dynamic textbox ID in .aspx.cs

I have a question on C#. I use the GridView ItemTemplate to add a textbox to a whole column. I add the ID to the drop down list in ItemTemplate. Therefore, the generated ID of drop down list is 'GridViewID_dropdownListID_number' in each row when I launch the project.
However, I cannot set the drop down list to .Visible = true and .Visible = false in .aspx.cs file. I try to type the 'dropdownListID' and 'GridViewID_dropdownListID_number' to 'Page_Load' function. However, it displays the error message which is under light the statement.
'The name 'GridViewID_dropdownListID_0' does not exist in the current content'
Can I set the drop down list visible to true and false in .aspx.cs?
P.S I can retrieve the row number by GridViewRow
you can use FindControl
DropdownLIst tvSeries = (DropdownLIst)tableOfTVSeries.Rows[0].Cells[2].FindControl("tvSeriesTableCategoryDropdownLIst");
Here is an example how to do this in the item template of a repeater -- this is typically how this problem is solved:
<asp:DataList Runat="server" ...>
<ItemTemplate>
<asp:Label runat="Server" Text='<%# Container.DataItem("data") %>'
Visible='<%# Container.DataItem("makevisible") %>'/>
</ItemTemplate>
</asp:DataList>

DataBind DataSource Result to ASP Labels

I have an AccessDataSource TestSummaryADS. I can easily view the results in a GridView or DropDownList by setting its DataSourceID property.
How do I bind a value from the results TestSummaryADS to the text of a label?
I'm just trying to populate labels on my page with results from the DB entry.
C# or VB.NET answers okay.
If you only have a single record, use a FormView
<asp:FormView ID="FormViewTestSummary" runat="server"
DataSourceID="TestSummaryADS"
DefaultMode="Edit">
<EditItemTemplate>
<fieldset>
<asp:TextBox ID="txtMyText" Text='<%# Bind("ProjectNumber") %>' runat="server" />
Use Repeater control that contains Label controls to display the values you want. Bind your DataSource control to the repeater.
Also you can use TemplateColumn with GridView and add your labels into this template column.
But IMO, Repeater control is simpler to customize your view by templates.

Add item to databound DropDownList

I have dropdownlist control where item lists are coming from database
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="semester"
DataValueField="semester">
</asp:DropDownList>
But I want to add at the beginning 1 list item more "ALL" .. How can I add this one .
Thanks !
To add a new list item to the DropDownList, in the Properties window, click on the ellipses in the Items property.
Add a new list item with Text "ALL" & Value -1.
Or you can add the list item by adding this markup to the DropDownList:
<asp:DropDownList ID="categories" runat="server" ...>
<asp:ListItem Value="-1">
ALL
</asp:ListItem>
</asp:DropDownList>
Set the DropDownList's AppendDataBoundItems=True
With Items.Insert method you can add an item at a specific index :
DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL"));
Be sure to disable the viewstate of DropDownList1 so that it doesn't retain each resultset from the db!

Categories

Resources