Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to have db values show for dropdownlist & radiobutton in editmode

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
0
0
US
Hello.

I have a detailsview where I placed textboxes, radiobuttonlists, and dropdownlists. I am able to successfully show the database values for the textboxes, but how do i do the same for radiobuttonlists and dropdownbuttonlists? Both lists shold show the values entered from the database as the initial selection, but I also want to show new selections within the dropdown. Currently, my users have reselect everything.

Here's an example of what I have done:

Code:
<asp:TemplateField HeaderText="Master/Dub" ConvertEmptyStringToNull="False">
<HeaderStyle Font-Bold="True" />                            <EditItemTemplate>                            
<asp:Label ID="videotypelbl" runat="server" Text='<%#Bind("VideoType")%>'></asp:Label><br />
<asp:RadioButtonList ID="videoTypeRadioList" runat="server" Font-Size="11px" RepeatDirection="Horizontal">
<asp:ListItem Value="0">Master</asp:ListItem>
<asp:ListItem Value="1">Dub</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="orgvideotypelbl" runat="server" Text='<%#Bind("VideoType")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


CODE BEHIND for DetailsView:

Code:
private void bindMusicDetails()
    {       
        string showdet = ConfigurationManager.ConnectionStrings["UpdInventory"].ConnectionString;
        SqlConnection detconn = new SqlConnection(showdet);
        SqlCommand detcomm = new SqlCommand("select * from view_showAudio WHERE ID=@ID", detconn);
        DataTable mdt = new DataTable();
        try
        {
            foreach (GridViewRow abc in musicGrid.Rows)
            {
                CheckBox bCheck = (CheckBox)abc.FindControl("selectMusic");
                if (bCheck != null && bCheck.Checked)
                {               
                    int Req = Convert.ToInt32(musicGrid.DataKeys[abc.RowIndex].Value);
                    detcomm.Parameters.AddWithValue("@ID", Req);
 
                    SqlDataAdapter joy = new SqlDataAdapter(detcomm);
                    joy.Fill(mdt);
                }
                musicDetailsGV.AutoGenerateRows = false;        
                musicDetailsGV.DataSource = mdt;           
                musicDetailsGV.DataBind();   
            }
        }
        catch (SqlException risk)
        {
            detErrors.Text = "Error binding details<br>" + risk;
        }
}
 
create a handler for the GridViewRowDataBound event. within this event
1. verify it's a data row
2. find the radio button list control
3. set the selected index
Code:
protected void SetDefaultValues(object sender, GridViewRowDataBoundEventArgs e)
{
   if(!e.Row.RowType == DataRowType.DataRow) return;
   RadioButtonList radio = e.Row.FindControl("name of control") as RadioButtonList;
   if(radio == null) return;
   DataRowView view = e.Row.DataItem as DataRowView;
   string value = view["name of field"].ToString()
   radio.SelectedIndex = radio.Items.IndexOf(radio.Items.FindByValue(value));
   //or if the list item is guaranteed to be in the list
   //the above option is safer.
   //radio.Items.FindByValue(value).Selected = true;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top