This is a migrated thread and some comments may be shown as answers.

Problem populating comboBoxes from SQL database after RequiredField Validation

1 Answer 213 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
newguy
Top achievements
Rank 1
newguy asked on 16 May 2013, 09:15 AM
Hi guys,

I have 2 ComboBoxes, The selection from the first comboBox Will populate the Second ComboBox and will bind the data from a SQL database. This all work perfectly but since i added a RequiredFieldValidator on the first ComboBox if the user fails to select a option from the first combobox the Validation Error will Fire but then when the user selects an item from the first ComboBox ofter being prompted to please make a selection the Second ComboBox fails to populate.

Here is and example of Code i am using for the validation

<td>
<telerik:RadComboBox ID="cboAccount" runat="server" DataTextField="AccountName"
DataValueField="AccountID" EnableLoadOnDemand="True" EmptyMessage="Select Account"
MarkFirstMatch="True"
OnSelectedIndexChanged="cboAccount_SelectedIndexChanged"
AutoPostBack="True" >
</telerik:RadComboBox>
 
 <asp:RequiredFieldValidator ID="ValidateAccount" runat="server"ControlToValidate="cboAccount" ErrorMessage="Please select a value"></asp:RequiredFieldValidator>
 </td>

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 May 2013, 10:40 AM
Hi newguy,

Please have a look at the sample code I tried for a similar scenario which works fine at my end.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" EnableLoadOnDemand="true" OnItemsRequested="RadComboBox1_ItemsRequested"
    EmptyMessage="Select Country" MarkFirstMatch="True" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"
    AutoPostBack="True">
</telerik:RadComboBox>
<asp:RequiredFieldValidator ID="ValidateAccount" runat="server" ControlToValidate="RadComboBox1"
    ErrorMessage="Select a Country"></asp:RequiredFieldValidator>
<br />
<br />
<telerik:RadComboBox ID="RadComboBox2" runat="server" Width="186px" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT Name FROM [Country] ORDER By Name"></asp:SqlDataSource>
<br />
<br />
<telerik:RadButton ID="RadButton1" runat="server" Text="Click" CausesValidation="true">
</telerik:RadButton>

C#:
protected void RadComboBox1_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
{
    RadComboBox1.DataSourceID = "SqlDataSource2";
    RadComboBox1.DataTextField = "Name";
    RadComboBox1.DataBind();
}
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    LoadCities(e.Text);
}
protected void LoadCities(string countryName)
{
    SqlConnection connection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM City WHERE Countryname  in ('" + countryName + "') ORDER By Countryname,Cityname", connection);
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    RadComboBox2.DataTextField = "Cityname";
    RadComboBox2.DataSource = dt;
    RadComboBox2.DataBind();
}

Thanks,
Shinu.
Tags
ComboBox
Asked by
newguy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or