How to Bind related Dropdownlist in gridview - c#

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"

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>

asp.net DropDownList inside GridView

I'm using GridView and inside that I have DropDownList
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:DropDownList ID="drop_mail" runat="server" OnSelectedIndexChanged="drop_mail_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Dropdownlist is already populated by values(dynamically) and if user will select some other value from dropdownlist I would like to get that value without postback action.
I hope you will help me.
Thanks
"I would like to get that value without postback action."
If you dont want postback at all , you can catch dropdown change event in jquery.
<asp:DropDownList class="dropdown" ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
Note class 'dropdown' above.
$(document).ready(function(){
$(".dropdown").change(function(){
var selectedValue= $(this).val();
$(".ddl-value").val(selectedValue);
});
});
EDIT:
You can put one hidden variable in page like
<input type="hidden" id="selectedvalue" runat="server" class="ddl-value"/>
on server side access it like :
selectedvalue.Value
Add a hidden field with each dropdownlist and set the hidden field value to dropdownlist value using javascript. On submit loop through hidden field and get the value.

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.

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.

First record in dropdown list will not update the other dropdownbox

I have a dropdown box that is populated from results from a SQL query. The selected value from dropdownlist1 successfully populates the lbAuthors dropdown list. During testing I realized that the first record from dropdownlist1 never updates into the lbAuthors dropdownlist. Here is an example: if I have three authors name in the 2nd dropdown box (Frost, Kipling, Poe) the first name - Frost - does not update into the first dropdown box. Kipling or Poe do - but not Frost.
My question is - What do I need to include in my event to allow Frost (or whatever the first record is) to update into the first dropdown box? –
Code-behind:
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Items.Add(new ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedItem.Text));
lbAuthorList.Items.FindByValue(DropDownList1.SelectedItem.Text).Selected = true;
}
Markup:
<asp:DropDownList runat="server"
ID="lbAuthors"
style="float:left;"
DataSourceID="odsAuthorList"
DataTextField="DisplayAuthorName" DataValueField="AuthorID"
onselectedindexchanged="lbUserList_SelectedIndexChanged"
AppendDataBoundItems="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1"
runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
</asp:DropDownList>
That is because when you select the first item, selcted index does not change. You need to insert a dummy item like this::
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
<asp:ListItem Text="--Select One--" Value="-1"></asp:ListItem>
</asp:DropDownList>
This issue rises because u wrote code for dropdownlist changed but initially first value selected at that time dropdownlist changed event not fired. If u choose second and then choose first one it will work fine.
Just add one dummy variable in dropdownlist item..
dropdownlist1.Items.Add("--Select--");

Categories