How to set initial SelectedItem in DropDownList inside ListView EditItemTemplate? - c#

I have a ListView control with DDL in its EditItemTemplate:
<asp:ListView ID="ListView1" ItemType="Project.Models.Product"
SelectMethod="GetProducts" runat="server" >
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ProducerDDL"
DataValueField="ProducerId"
DataTextField="Name"
SelectMethod="GetProducers" />
</EditItemTemplate>
</asp:ListView>
Project.Models.Product contains property Producer.
So the question is: how to set actual Producer of the edited Product item as the selected item of ProducerDDL?

So it seems the solution is just using Bind() the proper way:
<asp:DropDownList SelectedValue='<%# Bind("Producer.ProducerId") %>' />
as the binding context is my Product item.

Related

Gridview event to access controls in ItemTemplate & EditItemTemplate?

Is there one gridview event that can access a control in ItemTemplate and EditItemTemplate without additional code (ie. session, viewstate, etc)?
Let's say my gridview looks like this:
<asp:GridView ID="GridView_Sales" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesId"
OnRowDataBound="OnRowDataBound"
OnRowEditing="GridView_NSB_RowEditing"
OnRowUpdating="GridView_NSB_RowUpdating"
OnRowCommand="GridView_NSB_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView_RowDataBound has access to Label_WasSold in ItemTempplate but not the dropdown in EditItemTemplate. GridView_RowEditing has access to DropDownList_Sold but not to Label_WasSold; the same thing with GridView_RowUpdating.
I want to compare the value in Label_WasSold.Text to the value in DropDownList_Sold.SelectedValue when doing an update without having to add more code or drag session variables from one place to another.
Just add a hidden field to EditTemplate that stores the value of WasSold data item as in code below.
In your RowUpdating event, you can find the hidden field and get its value, then compare it with drop down value.
Markup to include hidden field in EditTemplate
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField id="hdnWasSold" runat="server" Value='<%# Eval("WasSold").ToString() %>' />
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
C# code to get the hidden field value in RowUpdating event
HiddenField hdnWasSold = (HiddenField)GridView_Sales.Rows[e.RowIndex].FindControl("hdnWasSold");
string wasSoldValue = hdnWasSold.Value;

C# - How to populate a DropDownList in EditItemTemplate

Here, I have a DropDownList in the EditItemTemplate:
<asp:TemplateField HeaderText="RequestedBy" SortExpression="RequestedBy">
<EditItemTemplate>
<asp:DropDownList ID="ReqUserDDL" runat="server" AppendDataBoundItems ="True" DataSourceID="ReqUsersDataS" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Name") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="ReqUsersDataS" runat="server" ConnectionString="<%$ ConnectionStrings:itassetmgmtConnectionString1 %>" SelectCommand="SELECT Firstname + Lastname AS Name FROM Users"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("RequestedBy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
However, I keep getting this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.
Is there a way to remedy this?
Use Eval instead of Bind. Bind is for read and/or write, Eval is read-only.
EDIT: Eval/Bind error is not from from the ReqUsersDataS DataSource. It is trying to for a column named 'Name' on the data source of the GridView that has the TemplateField you showed. You just need to make sure that other data source has column named 'Name'.

How to Bind related Dropdownlist in gridview

I have a Grid view, whenever I select 1st dropdownlist i.e. subject i want to bind related teachers name on second dropdownlist only one subject and teacher will updated dropdownlist is inside itemtemplate.
eg
<asp:TemplateField>
<HeaderTemplate>
Friday
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlSubjectFr" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlTeacherFr" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
If u want u change the Drop down list value of second based on first Drop down list selected value.. that should write in selected Index changed event with auto post back = "true"

Select an item in a dropdownlist in a listview

I have a listview which is databound by a list of objects.
In the listview, i have a dropdownlist on each item. Which is filled in the .._itemcreated event.
<asp:ListView ID="ListList" runat="server">
<ItemTemplate>
<asp:TextBox ID="ListItem" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:DropDownList ID="ddlAccountManagers" AutoPostBack="True" runat="server" />
<br />
</ItemTemplate>
</asp:ListView>
Depending on which item, i have to set the selectedvalue of the dropdown. But how do I do this?
How do I access the current items values in the itemcreated event?
Since you are able to fill the dropdownlist, I assume you already have access to it.
ddlAccountManagers.Items.FindByText("TextToSelect").Selected = True
or
ddlAccountManagers.Items.FindByValue("ValueToSelect").Selected = True
You could try this one:
ddlAccountManagers.SelectedValue="value you want to be selected"
In the list of objects you have, I suppose that each object will be associated with an AccountManager. An AccoutManager logically would have an id, that will distinguish him/her from the rest of account managers. Then you have to put this value as the selected value.

ASP.NET C# Nested controlls access

I've got something like that
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<label id="test" runat="server">dw</label>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Can someone tell me, how should I change label text, using C# code?
Main problem for me is- how to get access to the nested control (label, literal) from c# code?
EDITED:
<SelectedItemTemplate>
<asp:HiddenField ID="NumberEdit" runat="server"
Value='<%# Bind("numbers") %>' />
<label for="NameEdit">Name:</label>
<asp:TextBox ID="NameEdit" Width="160px" runat="server" AutoPostBack="true" OnTextChanged="NameEdit_TextChanged"
Text='<%# Bind("Name") %>' />
<br />
<label for="ShortcutEdit">Shortcut:</label>
<asp:TextBox ID="ShortcutEdit" Width="80px" runat="server"
Text='<%# Bind("Shortcut") %>' />
<br />
and I would like to generate automatically Shortcut text when user will change Name (Shortcut = 2 first letters from NameEdit)? Can you explain me, how should I do it? –
You would want to have an ItemDataBound event handler to get access to the controls for that particular item in your listview. The example on the page I linked should help you out.
First thing is that you need a data source binded with this ListView control, for example SqlDataSource, or any other allowed type you need:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT [Title], [id] FROM [Articles]"></asp:SqlDataSource>
<asp:ListView ID="lv" runat="server" DataSourceID="SqlDataSource1" >
// rest of the code
</asp:ListView>
Second thing is that controls from LayoutTemplate template will be rendered only if there is any data to show. So if you have datasource, but it is empty, this tamplate will not be applied. But you can use EmptyDataTemplate to display info when there is nothing from the datasource to display.
And then, when you already have datasource defined and binded to your ListView and there is data that will be displayed, the LayoutTemplate will be rendered. And then you can use FindControl method of the ListView. As for example of getting this literal:
Literal l = (Literal)lv.FindControl("litControlTitle");
It's returning null for you because you have no data to display, so controls are not rendered at all.
((Label)ListView1.FindControl("test")).Text = "Hello!";

Categories