I have a asp.net gridview. A template field of type dropdownlist is contained in a column:
<asp:TemplateField HeaderTextLabel="strManagedOETeamart">
<ItemTemplate>
<asp:DropDownList runat="server" AutoPostBack="True" OnSelectedIndexChanged="SelectedTeamartChanged">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
How can I get the GridViewRow when the user selects another item?
protected void SelectedTeamartChanged(object sender, EventArgs e)
{
DropDownList dropDown = (DropDownList) sender;
//I would like to know the GridViewRow this DropDownList is in
}
You're looking for the NamingContainer property:
GridViewItem row = (GridViewRow)dropDown.NamingContainer;
By the way, that works for any kind of web databound control like GridView, DataList,Repeater or ListView.
Consider this more complex requirement: you have a GridView which is nested in another GridView. Now you're handling a DropDownList's SelectedIndexChanged event that is inside the child's GridView and you want to get the reference to the GridViewRow of the parent GridView:
var control = (Control)sender;
var row = (GridViewRow)control.NamingContainer;
var parentRow = (GridViewRow)row.NamingContainer.NamingContainer;
That's the safest and easiest way to get it.
Related
Default dropdownlist values include locations for all books. When I select a checkbox the the dropdownlist should populate only the locations where that book is available.In back-end, My Stored procedure is working fine for single parameter and multiple values.
But in UI, I can get locations for only one checkbox, if I click second checkbox the dropdown list does not include the value for that second checkbox.
Also when I unclick all it should return to default.
Any hint or help will be appreciated.
<label>BooksLocations</label>
<asp:DropDownList runat="server" ID="drpdwnListLocationBooks" CssClass="formcontrol">
</asp:DropDownList>
<label>Books</label>
<asp:CheckBoxList runat="server" ID="chkboxListbookStatus">
<asp:ListItem Text="Book A"></asp:ListItem>
<asp:ListItem Text="Book B"></asp:ListItem>
<asp:ListItem Text="Book C"></asp:ListItem>
</asp:CheckBoxList>
Add below code
<asp:CheckBoxList runat="server" ID="chkboxListbookStatus"
AutoPostBack="True" OnSelectedIndexChanged="chkboxListbookStatus_SelectedIndexChanged">
Then in code behind
protected void chkboxListbookStatus_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList list = sender as CheckBoxList;
ListItem box = list.SelectedItem;
//add code to get data from SP to fill data in dropdown
}
For a Label in we bind a data using
<asp:Label ID="Label2" runat="server" Text='<%#Eval("address") %>'></asp:Label>
how to bind data to dropdownlist like that?
asp:DropDownList ID="droplist" runat="server" >
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Like this....
<asp:DropDownList ID="droplist" runat="server" SelectedValue='<%#Eval("fieldname")%>'>
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Note that intellisense will not pick SelectedValue out. You will of course need to populate the dropdown with the data... using any method that suits
Either put your datasource in the DropDownList declaration like here: populate dropdownlist
Or use Codebehind like this:
Eg: inside Page_Load():
List<string> ItemsToGoInDropDown = new List<string>{"manager", "admin", "etc"};
droplist.DataSource = ItemsToGoInDropDown;
droplist.DataBind();
Put the data in hidden field. Then asigen that in drop down in Gridview Rowdatabound Event.Like This.
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hf = (HiddenField)e.Row.FindControl("hf");
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
ddl.SelectedValue = hf.Value;
}
I have a LinkButton in an InsertItemTemplate which when clicked, should display a hidden DropDownList in the InsertItemTemplate. However, it doesn't seem to be working, but it will say, change the text of a label outside the Formview when the LinkButton is clicked. The event is firing, but the part to make the DropDownList visible in the InsertItemTemplate is not doing anything. Code is below:
.aspx:
<asp:FormView ID="formViewNewRecord" runat="server">
<InsertItemTemplate>
<asp:DropDownList ID="ddlAddSelection2" runat="server" DataSourceID="dSource1" DataTextField="Users" DataValueField="Users" AppendDataBoundItems="true" Visible="false">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton runat="server" ID="lbAddAnother" OnClick="lbAddAnother_Click">+Add Another</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
C#:
protected void lbAddAnother_Click(object sender, EventArgs e)
{
DropDownList addSelection2 = (DropDownList)formViewNewItem.Row.Cells[0].FindControl("ddlAddSelection2");
addSelection2.Visible = true;
Label2.Text = addSelection2.ID;
}
Your dropdown control is not an immediate child of your formview. So since the FindControl call is not recursive, you have to search for the control in the right location of your form view's child controls. See this for the details but at a high level, you need something along the lines of:
DropDownList ctrl = (DropDownList)FormView1.Row.Cells[0].FindControl("ddlAddSelection2");
After that, you should check it for null for safe measure.
I have a RadioButtonLIst as follows:
<asp:RadioButtonList runat="server" ID="Location" ValidationGroup="formVal">
<asp:ListItem Value="Beverly Hills" />
<asp:ListItem Value="Seattle" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator runat="server" ID="rfvLocation"
ControlToValidate="Location">
</asp:RequiredFieldValidator>
If the first radio button is selected, I need to require a selection in the following dropdownlist:
<asp:DropDownList runat="server" ID="Food" ValidationGroup="formVal">
<asp:ListItem Text="Chicken" Value="Chicken"></asp:ListItem>
<asp:ListItem Text="Beef" Value="Beef"></asp:ListItem>
<asp:ListItem Text="Fish" Value="Fish"></asp:ListItem>
</asp:DropDownList>
Would I use a CustomValidator to accomplish this? If so, what validation expression should be used? If a CustomValidator isn't a good fit, which validator is suggested?
Thanks much:)
UPDATE: Added the following to code behind
protected void LocationChanged(object sender, EventArgs e)
{
if (Location.SelectedIndexChanged == true)
{
rfvFood.Enabled = true;
}
else rfvFood.Enabled = false;
}
Is this correct?
You could do the following:
add a RequiredFieldValidator for the DropDownList and set its enabled property to false
set AutoPostBack=true on the RadioButtonList
in code behind, handle the RaditButtonList's OnSelectedIndexChanged event
Here, add logic that enables or disables the RequiredFieldValidator depending on RadioButtonList's selected item
If you don't like the whole page refreshing when the user chooses an option, wrap these controls up inside an UpdatePanel.
I have a website programmed in Asp.Net and use a ListView for displaying data. The data is coming from a LinqDataSource.
In my EditItemTemplate I have a CheckBoxList which consist of:
<asp:CheckBoxList runat="server" ID="TypeCheckBoxList" RepeatColumns="2">
<asp:ListItem Value="128">6.-10. klasse<br />Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="64">6.-10. klasse<br />Syddjurs vejleder</asp:ListItem>
<asp:ListItem Value="32">Gået ud af skolen<br/>Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="16">Gået ud af skolen<br/>Syddjurs vejleder</asp:ListItem>
<asp:ListItem Value="8">Ekstra støtte<br/>Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="4">Ekstra støtte<br />Syddjurs vejleder</asp:ListItem>
<asp:ListItem Value="2">Kontakt</asp:ListItem>
<asp:ListItem Value="1">Om os<br />Medarbejdere</asp:ListItem>
</asp:CheckBoxList>
I have a column called Type in my db and it is a tinyint. Therefore I can say (byte)Eval("Type").
But how do I databind my Eval("Type") to the CheckBoxList so if Eval("Type") is 3, then the two last items are selected?
I have tried setting a hidden value which binds to Type and then in the CheckBoxList OnLoad setting the selected items. But that did'nt work.
That's the way to do it, with the hidded value binding to Type, but on the ItemDataBound event of the ListView.
So the event would look something like this:
protected void ListViewId_ItemDataBound (object sender, ListViewItemEventArgs e)
{
HiddenField hdfType = (HiddenField)e.Item.FindControl("hdfType");
CheckBoxList TypeCheckBoxList = (HiddenField)e.Item.FindControl("TypeCheckBoxList");
// and you put the hidden just for EditItem and do:
if (hdfType != null)
foreach (ListItem item in TypeCheckBoxList.Items)
if (int.Parse(item.Value) < int.Parse(hdfType.Value))
item.Selected = true;
}
(I wrote all of this from my head, so there might be some small mistakes)
First you should write a javascript function like this
function Selected(value,type)
{
if(value<type)
return true;
else
return false;
}
<asp:ListItem Value="32" Selected= javascript:function Selected(32,Eval("Type"))>Gået ud af skolen<br />Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="16" Selected= javascript:function Selected(16,Eval("Type")>Gået ud af skolen<br />Syddjurs vejleder</asp:ListItem>
little bit modification may be required to finalize it..major focus on Selected attribute