Select RadioButtonList Item by matching TextBox value in ASP.Net

Last Reply 6 days ago By pandeyism

Posted 8 days ago

I had a dropdown where on  dropdown change event I retrieved the

values from db and populated to textboxex accordingly on

selection of dropdownlistitem but finding difficult to get the 

value from db and populate radiobuttonlist item checked exactly 

   {
            string ConnectionString = ConfigurationManager.ConnectionStrings["PizzaDelievery"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                String strQuery = "select DriverID, DriverName,AssignedSkills from Skills where DriverID =@DriverID";
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.AddWithValue("@DriverID", Driver_Namedropdwn.SelectedItem.Value);
               
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                //String str = "select AssignedSkills from Skills where ID =@ID";
                //cmd.Parameters.AddWithValue("@ID", Driver_Namedropdwn.SelectedItem.Value);
                //SqlCommand cmd1 = new SqlCommand();
                

                connection.Open();


                cmd.Connection = connection;

                string selectedval = radioskills.SelectedValue;


                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    Driver_Id.Text = sdr["DriverID"].ToString();
                    Drivername.Text = sdr["DriverName"].ToString();
                    if (radioskills.Items.FindByValue(sdr["AssignedSkills"].ToString()) != null)
                    {
                        radioskills.SelectedItem.Value = sdr["AssignedSkills"].ToString();
                    }
                   // selectedval = sdr["AssignedSkills"].ToString();
                    //radioskills.SelectedItem.Text = sdr["AssignedSkills"].ToString();
                    //radioskills.SelectedItem.Text = sdr["AssignedSkills"].ToString();

                }
            }

 

vyas has viewed this question 5 days ago.
Posted 8 days ago

Hi vyas,

Please take reference the below sample code and correct your code to set the selected value.

HTML

<asp:RadioButtonList ID="rblFruits" runat="server">
    <asp:ListItem Text="Mango" Value="Mango"></asp:ListItem>
    <asp:ListItem Text="Banana" Value="Banana"></asp:ListItem>
    <asp:ListItem Text="Apple" Value="Apple"></asp:ListItem>
    <asp:ListItem Text="Orange" Value="Orange"></asp:ListItem>
</asp:RadioButtonList>
Name Of Fruits :
<asp:TextBox ID="txtFruitName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />

C#

protected void Submit(object sender, EventArgs e)
{
    string fruitName = txtFruitName.Text.Trim();
    if (rblFruits.Items.FindByText(fruitName) != null)
    {
        rblFruits.Items.FindByText(fruitName).Selected = true;
    }
}

Screenshot


Posted 6 days ago

Thanks pandeyism for the reply,but thats not what I am looking

for My question is totally different . OK I had an edit page where I had 

few textboxes and 1 radiobuttonlist where if i go to edit page i had 

 

adropdown on dropdown selected list item I need to retrieve the data 

 

from db and populate textboxes and populated radiobuttonlist item checked(which matches the string in db)

 

 


Posted 6 days ago

Hi vyas,

Please take reference the below code and change your code.

Database

I have made use of the table Customers. You can download the database table SQL by clicking the download link below.

Download SQL file

HTML

Customer:<asp:DropDownList ID="ddlCustomers" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
</asp:DropDownList>
<br />
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Country:
<asp:RadioButtonList ID="rblCountries" runat="server">
</asp:RadioButtonList>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.PopulateCustomersDropDownList();
    }
}

protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
    string customerID = ddlCustomers.SelectedItem.Value;
    this.PopulateCountriesRadioButtonList();
    string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Name,Country FROM Customers WHERE CustomerId = @CustomerId", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@CustomerId", customerID);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                txtName.Text = dr["Name"].ToString();
            }
            if (rblCountries.Items.FindByText(dr["Country"].ToString()) != null)
            {
                rblCountries.Items.FindByText(dr["Country"].ToString()).Selected = true;
            }
            con.Close();
        }
    }
}

private void PopulateCustomersDropDownList()
{
    string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("select CustomerID, Name from Customers", con))
        {
            con.Open();
            ddlCustomers.DataSource = cmd.ExecuteReader();
            ddlCustomers.DataTextField = "Name";
            ddlCustomers.DataValueField = "CustomerID";
            ddlCustomers.DataBind();
            con.Close();
        }
    }
}
private void PopulateCountriesRadioButtonList()
{
    string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Customers", con);
    con.Open();
    rblCountries.DataSource = cmd.ExecuteReader();
    rblCountries.DataTextField = "Country";
    rblCountries.DataValueField = "Country";
    rblCountries.DataBind();
    con.Close();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.PopulateCustomersDropDownList()
    End If
End Sub

Protected Sub ddlCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim customerID As String = ddlCustomers.SelectedItem.Value
    Me.PopulateCountriesRadioButtonList()
    Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT Name,Country FROM Customers WHERE CustomerId = @CustomerId", con)
            con.Open()
            cmd.Parameters.AddWithValue("@CustomerId", customerID)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.Read() Then
                txtName.Text = dr("Name").ToString()
            End If
            If rblCountries.Items.FindByText(dr("Country").ToString()) IsNot Nothing Then
                rblCountries.Items.FindByText(dr("Country").ToString()).Selected = True
            End If
            con.Close()
        End Using
    End Using
End Sub

Private Sub PopulateCustomersDropDownList()
    Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("select CustomerID, Name from Customers", con)
            con.Open()
            ddlCustomers.DataSource = cmd.ExecuteReader()
            ddlCustomers.DataTextField = "Name"
            ddlCustomers.DataValueField = "CustomerID"
            ddlCustomers.DataBind()
            con.Close()
        End Using
    End Using
End Sub

Private Sub PopulateCountriesRadioButtonList()
    Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Customers", con)
    con.Open()
    rblCountries.DataSource = cmd.ExecuteReader()
    rblCountries.DataTextField = "Country"
    rblCountries.DataValueField = "Country"
    rblCountries.DataBind()
    con.Close()
End Sub

Screenshot

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html