How to use the Dropdownlist Selectedvalue value?

Asked

Viewed 9,419 times

3

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

Use detailsView to show/edit these fields and would like to have a Dropdownlist in Edittemplate with the Male or Female options and when clicking edit, update in the database with M or F.

How do I do?

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" 

3 answers

3

Suppose your Dropdownlist is called 'ddlSexo', to get the value use:

string sexo = ddlSexo.SelectedValue;

And to popular the Dropdownlist, assuming that you are reading from a datatable named 'dt' and that you have a 'sex' column, use:

ddlSexo.DataSource = dt;
ddlSexo.DataTextField = "sexo";
ddlSexo.DataValueField = "sexo";
ddlSexo.DataBind();
  • In case I use Sqldatasource, is there any way using it?

2

The way our friend @Daniel Manfrini quoted is correct:

Suppose your Dropdownlist is called 'ddlSexo', to get the value use:

string sex = ddlSexo.Selectedvalue;

And to popular the Dropdownlist, assuming you’re reading from a Datatable named dt and having a 'sex' column, use:

ddlSexo.Datasource = dt;

ddlSexo.Datatextfield = "sex";

ddlSexo.Datavaluefield = "sex";

ddlSexo.Databind();

however you can create a char field that keeps the acronym of Sex in the database and assign the Display text as 'Male' and the hidden value that should be really used you assign the Acronym field (’M' or 'F').

Then I’d be 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 by taking the value in the code-Behind would already get the acronym you will use in the bank. string siglaSexo = ddlSexo.SelectedValue;

1

To update in the database each time you modify the selection, it is necessary to capture the Selectedindexchanged event and within it add a logic that connects in the database and update. Add the attribute to your dropdownlist: OnSelectedIndexChanged="sexoDrop_SelectedIndexChanged" and in code-Behind add the method:

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

Remembering that as you are using a detailsView, to access the Dropdownlist, it is necessary to search the same inside the detailsview, since it is not directly accessible by the code-Behind. Example:

DropDownList ddlSexo = detailsViewID.FindControl("sexoDrop") as DropDownList;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.