How to use the SelectedValue value of DropDownList?

3

Next, I have a table "animals" with field "sex" (varchar (1)). In it I hedge M or F, for Male or Female.

Use detailsView to show / edit these fields and would like to have a DropDownList in the EditTemplate with the options Male or Female and when you click edit, update in the database with M or F.

How do I do it?

My code:

<asp:TemplateField HeaderText="Sexo:" SortExpression="sexo">
                <EditItemTemplate>
                    <asp:DropDownList ID="sexoDrop" runat="server">
                        <asp:ListItem Value="M">Macho</asp:ListItem>
                        <asp:ListItem Value="F">Fêmea</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("sexo") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("sexo") %>'></asp:Label>
                </ItemTemplate>
</asp:TemplateField>

<UpdateParameters>
<asp:ControlParameter Name="sexo" ControlID="sexoDrop" PropertyName="SelectedValue" />
</UpdateParameters>

UpdateCommand="UPDATE [animais] SET [sexo] = @sexo WHERE [id] = @original_id" 
    
asked by anonymous 08.03.2014 / 20:09

3 answers

3

Assuming your DropDownList is called 'ddlSexo', to get the value use:

string sexo = ddlSexo.SelectedValue;

And for popular DropDownList, assuming you're reading from a DataTable named 'dt' and have a column named 'sex', use:

ddlSexo.DataSource = dt;
ddlSexo.DataTextField = "sexo";
ddlSexo.DataValueField = "sexo";
ddlSexo.DataBind();
    
10.03.2014 / 14:26
2

In the way that our friend @ Daniel Manfrini quoted is correct:

  

Assuming your DropDownList is called 'ddlSexo', to get the value   use:

     

sex string = ddlSexo.SelectedValue;

     

And to popular the DropDownList, assuming you're reading from a   DataTable name 'dt' and that has a column named 'sex', use:

     

ddlSexo.DataSource = dt;

     

ddlSexo.DataTextField="sex";

     

ddlSexo.DataValueField="sex";

     

ddlSexo.DataBind ();

however you can create a char field that stores the acronym for Sex in the database and assign the Display text to 'Male' and the hidden value that is actually used you assign the 'M' or 'F' ).

That would look like this:

ddlSexo.DataSource = dt; ddlSexo.DataTextField = "sexo"; //Nome do campo que tem a descricao completa ex.: Macho ou Femea ddlSexo.DataValueField = "sigla"; //Nome do campo que guarda o valor da Sigla usada para aquela descricao ddlSexo.DataBind();

And when you get the value in the code-behind you would already get the acronym you're going to use in the bank. string siglaSexo = ddlSexo.SelectedValue;

    
13.03.2014 / 22:00
1

To upgrade to the database each time you modify the selection, you need to capture the SelectedIndexChanged event and add a logic that connects to the database and update it. Add in your dropdownlist the attribute: OnSelectedIndexChanged="sexoDrop_SelectedIndexChanged" and in the code-behind add method:

protected void sexoDrop_SelectedIndexChanged(object sender, EventArgs e)
    {
        //CódigoVaiAqui.
    }

Remembering that since you are using a detailsView, to access the DropDownList, it is necessary to look for the same within the detailsview, since it is not directly accessible by the code-behind. Example:

DropDownList ddlSexo = detailsViewID.FindControl("sexoDrop") as DropDownList;
    
14.03.2014 / 11:43