I am databinding a GridView to an object datasource. The gridview contains a TemplateField which contains a RadioButtonList with ListItems defined inline.
I want to be able to databind the SelectedValue of the RadioButtonList to the same underlying table as the other grid columns, but it doesn't work!
Do I have my syntax wrong, or is this impossible and requires looping code to individually select the proper item in each row?
<llblgenpro:LLBLGenProDataSource ID="llbComputerApplication" DataContainerType="EntityCollection" runat="server"></llblgenpro:LLBLGenProDataSource>
<asp:GridView ID="gridComputerApps" DataSourceID="llbComputerApplication" runat="server" AutoGenerateColumns="False"
EmptyDataText ="NO APPLICATIONS FOUND FOR THIS COMPUTER."
DataKeyNames="ComputerID, ApplicationID" EnableViewState="False"
style="border-style:dotted;border-width:thin"
>
<Columns>
<asp:BoundField DataField="ApplicationID" HeaderText="Application ID" SortExpression="ApplicationID" Visible="True" />
<asp:TemplateField HeaderText="Application Name"><ItemTemplate><%#Eval("Application.ApplicationName")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Normalized Name"><ItemTemplate><%#Eval("Application.NormalizedAppName")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Notes"><ItemTemplate><%#Eval("Application.NormalizedNotes")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>' ID="rblRequirementOption" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="Need Now" Text="Need Now"></asp:ListItem>
<asp:ListItem Value="Need Someday" Text="Need Someday"></asp:ListItem>
<asp:ListItem Value="Do Not Need" Text="Do Not Need"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NormalizedNotes" HeaderText="Notes" Visible="False" />
</Columns>
</asp:GridView>
What you have should work. Are you getting an error? Here's a working example copied from my current project. I'm binding to a nullable bit field - so have a hidden list item to accept the nulls.
<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
CssClass="NormalTextBox" RepeatDirection="Horizontal">
<asp:ListItem Value="false" Text="No" />
<asp:ListItem Value="true" Text="Yes" />
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
I also experienced this problem (nothing selected in radiobuttonlist) when binding against boolean values in MS SQL:
radDefault.Items.Add(new ListItem("Yes", "true"));
radDefault.Items.Add(new ListItem("No", "false"));
In my case, the solution was to capitalize the first letter of the true/false values, then the radiobuttonlistworked as expected:
radDefault.Items.Add(new ListItem("Yes", "True"));
radDefault.Items.Add(new ListItem("No", "False"));
Or, declaratively:
<asp:RadioButtonList runat="server" ID="radDefault" SelectedValue='<%# Bind("DB_FIELD") %>'>
<asp:ListItem Value="False" Text="No" />
<asp:ListItem Value="True" Text="Yes" />
</asp:RadioButtonList>
I didn't like the idea of using css to hide an item. Instead, I found this solution to add a blank item but remove it in the code behind.
<asp:RadioButtonList ID="MyRadioButtonList" runat="server"
SelectedValue='<%# Bind("Blah") %>'
OnDataBound="MyRadioButtonList_DataBound">
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="A"></asp:ListItem>
<asp:ListItem Value="B"></asp:ListItem>
<asp:ListItem Value="C"></asp:ListItem>
</asp:RadioButtonList>
protected void MyRadioButtonList_DataBound(object sender, EventArgs e)
{
RadioButtonList list = (RadioButtonList)sender;
ListItem blank = list.Items.FindByValue("");
if (blank != null)
list.Items.Remove(blank);
}
<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
CssClass="NormalTextBox" RepeatDirection="Horizontal">
<asp:ListItem Value="false" Text="No" />
<asp:ListItem Value="true" Text="Yes" />
<asp:ListItem Value="" Text="" selected="true" style="display: none" />
</asp:RadioButtonList>
It work me .....
gnanasekar.s vilangulathur
Related
I have a datagrid that has dropdowns in one column :
<asp:TemplateColumn HeaderText="">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" ID="lblResultId" Visible="false" text='<%# DataBinder.Eval(Container, "DataItem.CurrentId", "{0:g}") %>'/>
<asp:DropDownList ID="ddlCurrentValue" DataTextField="Description" DataValueField="Id" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCurrentValue_OnSelectedIndexChanged">
<asp:ListItem Text="" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="1" Value="1">1</asp:ListItem>
<asp:ListItem Text="2" Value="2">2</asp:ListItem>
<asp:ListItem Text="3" Value="3">3</asp:ListItem>
<asp:ListItem Text="4" Value="4">4</asp:ListItem>
<asp:ListItem Text="5" Value="5">5</asp:ListItem>
<asp:ListItem Text="6" Value="6">6</asp:ListItem>
<asp:ListItem Text="7" Value="7">7</asp:ListItem>
<asp:ListItem Text="8" Value="8">8</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
I set the selected value for each dropdown based on a value taken from the database on ItemDataBound.
Now if the user changes the value of one of the dropdowns, i would like to save the value from that dropdown.
My id is that i have the database-id in lblResultId and use that to save the selectedvalue from the dropdown to the database via a linq query. However, i am already stuck on the 'onselectedindexchanged' for the dropdownlists not firing.....
Any suggestions how to procede or how to do this differently ?
In a gridview, to display data in view mode, I use a label.
In Edit mode I have a dropdownlist. So, How can I set text in that label as selected value for dropdownlist when gridview is in edit mode?
Here is my code in aspx page:
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="true" />
<asp:TemplateField HeaderText="Zone Name">
<HeaderStyle Width="220px"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lbDisplayName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" CssClass="customddlZoneName">
<asp:ListItem Value="">--Select Zone Name--</asp:ListItem>
<asp:ListItem Value="Value1">Text 1</asp:ListItem>
<asp:ListItem Value="Value2">Text 2</asp:ListItem>
<asp:ListItem Value="Value3">Text 3</asp:ListItem>
<asp:ListItem Value="Value4">Text 4</asp:ListItem>
<asp:ListItem Value="Value5">Text 5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlInsertName" runat="server">
<asp:ListItem Value="">--Select Zone Name--</asp:ListItem>
<asp:ListItem Value="Value 1">Text 1</asp:ListItem>
<asp:ListItem Value="Value 2">Text 2</asp:ListItem>
<asp:ListItem Value="Value 3">Text 3</asp:ListItem>
<asp:ListItem Value="Value 4">Text 4</asp:ListItem>
<asp:ListItem Value="Value 5">Text 5</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
I mean when I put my grid view in Edit mode, I don't want my Dropdownlist show "--Select Zone Name--". I want it show "text 1",2,3 or 4...which is displaying in my Label in View mode. And you can do this in Gridview_RowEditing event. See the code below:
protected void Gridview_RowEditing(object sender, GridViewEditEventArgs e)
{
Label lbDisplayName = (Label)Gridview.Rows[e.NewEditIndex].FindControl("lbDisplayName");
string name = lbDisplayName.Text;
GridViewRow gvr = Gridview.Rows[e.NewEditIndex];
var dr = (DropDownList)gvr.FindControl("ddlName");
dr.SelectedItem.Text = name;
}
I have the following code
<div>
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue="Choose" ControlToValidate="testDropDownList"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
<br />
</div>
in which i validate Dropdownlist by RequiredFieldValidator
If changed the value of initialvalue property to read from static property in stactic classs .. but it always give me emtpy string in runtime unless this property have the value "Choose" ...
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue='<%# Util.ChooseValue %>' ControlToValidate="testDropDownList">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
Could Any one help me to know what's the issue in my code ??
Please call
testRequiredFieldValidator.databind()
in page load event and let me know if this is still an issue.
Set InitialValue of RequiredFieldValidator on page_load event in aspx.cs page.
for testing i have tried customvalidation
function ClientValidate(sender, args) {
//return false for testing...
args.IsValid = false;
}
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="ddldetail"
Text="Please select" ValidateEmptyText="true"
ClientValidationFunction="ClientValidate"
Display="Dynamic">
</asp:CustomValidator>
edit: here is what i exaclty wants to happen:
how to validate a dropdownlist and i have done this zillion times but what i am doing wrong here? any second pair of eye might spot it? i am trying to validate the dropdownlist if the user have not select any help?
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" CausesValidation="true"/>
<asp:GridView ID="GVInputMapping" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
EnableModelValidation="True" onrowdatabound="GVInputMapping_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" ControlStyle-Width="250px" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddldetail">
<asp:ListItem Selected="True" Value="0">Select me</asp:ListItem>
<asp:ListItem Value="1">abc</asp:ListItem>
<asp:ListItem Value="2">GHt</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredDDL" runat="server"
ControlToValidate="ddldetail" ErrorMessage="Please select" InitialValue="Select me" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have the InitialValue of the validator set to Select me, but the Value of that item is actually 0:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select Me" Value="0" />
<asp:ListItem Text="Foo" Value="1" />
<asp:ListItem Text="Bar" Value="2" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="Please select"
InitialValue="0"
Display="Dynamic">
</asp:RequiredFieldValidator>
You'll also need to assign a unique validation group for each row, otherwise validation will kick off for every row. To assign a unique validation group, you can use the Id column:
ValidationGroup='<%# string.Format("Group_{0}", Eval("Id")) %>'
You would add this to the validator and the button in the row.
I am using an asp.net details view. I added an entry into the details view like so...
<asp:BoundField DataField="DTMON_F" HeaderText="Monday Start:" InsertVisible="False"
ReadOnly="True" SortExpression="DTMON_F" Visible="false" />
<asp:TemplateField HeaderText="*Monday Start: " SortExpression="DTMON_F">
<EditItemTemplate>
<asp:DropDownList ID="ddlMondayStartHour" runat="server">
<asp:ListItem Value="6">6am</asp:ListItem>
<asp:ListItem Value="7">7am</asp:ListItem>
<asp:ListItem Value="8">8am</asp:ListItem>
<asp:ListItem Value="9">9am</asp:ListItem>
</asp:DropDownList>
:
<asp:DropDownList ID="ddlMondayStartMin" runat="server">
<asp:ListItem Value="00">00</asp:ListItem>
<asp:ListItem Value="15">15</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
<asp:ListItem Value="45">45</asp:ListItem>
</asp:DropDownList>
:
<asp:DropDownList ID="ddlMonAMPM" runat="server">
<asp:ListItem Value="AM">AM</asp:ListItem>
<asp:ListItem Value="PM">PM</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMonday" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now I need to FIRST concatinate the values from the dropdown then bind the data gathered in the edit template form the 3 dropdowns. How might I do that?
You can use the updated event of the detailsview which is called after you changed your values. The you can concatinate your values by code.
void CustomerDetail_ItemUpdated(object sender,
DetailsViewUpdatedEventArgs e)
{
// set your label value consisting of 3 dropdown values
CustomersView.DataBind();
}