Select an item in a dropdownlist in a listview - c#

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.

Related

How to add datasource to textbox in visual studio 2015

In my form I have drop down list that displays products
and a textbox that should display the price for the selected product
My question is how can I add datasource into the textbox ?
You can add a Event-Handler to the drop-down list that gets called, when the user selects a different value in the drop-down list.
Then simply change the value of the textbox in the event-handler method.
Hope this helps, please comment if you need more information.
I solved the problem !
I used datalist insted of textbox and I configured its datasource
to retrive the price for any chosen product from the drop down list
<ItemTemplate>
Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>

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"

How to set initial SelectedItem in DropDownList inside ListView EditItemTemplate?

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.

Change blank selection in drop down list?

I have a databound dropdown list on my page. The first value in the selection list is blank. Is there a way that I can change it so that the first selection says "Unassigned" instead of blank? I've tried the following, but it didn't work:
// Insert 'Unassigned' value for artist dropdown
ddlArtists.Items.Insert(0, "Unassigned");
After inserting the above code, the list still appears unchanged, with the first selection value being a blank. Any pointers would be great! Thank you!
EDIT: Here is the code for the dropdown:
<asp:DropDownList ID="ddlArtists" runat="server" Width="130px" TabIndex="5"
OnSelectedIndexChanged="ddlArtists_SelectedIndexChanged"
DataSourceID="sqldsArtist" DataTextField="Name" DataValueField="ID"
OnDataBound="ddl_DataBound"
AutoPostBack="True">
You don't need to do it on the CodeBehind. Just do it like that:
<asp:DropDownList ID="ddlArtists" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Unassigned" Value="0" Selected="true" />
</asp:DropDownList>
The AppendDataBoundItems property defines whether the contents of the DropDownList should be cleared out before data binding.
Don't forget to check for a PostBack when you're data binding it, to avoid duplicates.
Set the SelectedIndex property of your DropDownList to 0.

How to makes this checkbox works inside the GridView?

I am a new ASP.NET developer and trying to use a GridView control as a way for managing the employees in each division. This GridView will show all the employees in the employee table in the database. Now, I am facing the following problem:
FYI, I have the following database design:
Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
(IsActive is like a flag (bit datatype) to indicate if the employee is in an assignment or not)
IsActive will be displayed in the GridView as a Checkbox. If it is checked, it means true and the employee is existed in his division. if it is not checked, it means the employee is not with the division, and it should be hidden from the GridView. So how to do that? Also, I want to hide (True) text that appears besides the checkbox in the Edit mode. How to do that, too?
ASP.NET code:
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<%# Eval("IsActive")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server" Text='<%# Bind("IsActive")%>' />
</EditItemTemplate>
</asp:TemplateField>
UPDATE:
I want the employee with all of his information to hidden from the GridView since he is inactive.
Try this
<asp:CheckBox ID="isActive" runat="server" Visible='<%# Bind("IsActive")%>' Text='<%# Bind("IsActive")%>' />
Use this instead.
Have removed the text property, so that IsActive would not be visible. Visiblity is controled by IsActive Flag
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<%# Eval("IsActive")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server" Checked='<%# Eval("IsActive").ToString().Equals("True") %> />
</EditItemTemplate>
</asp:TemplateField>
EDIT
If you want entire row to be hidden for inactive user, you could best to do is filter the inactive user from dataset, before you do a databind() operation. You could even filter it at database end also, but since I do not know the entire scenario, I am suggesting you this.
DataSet filter = ds.Tables[0].Select("IsActive == 'True'");
gridUser.DataSource = filter;
gridUser.DataBind();

Categories