0

Soy nuevo en ASP.NET y en mi investigacion autodidacta me encontre con un problema:

Tengo un formulario con sus respectivos label y textbox y boton guardar que los insertaba en la base de datos, hasta hay todo bien.

Luego agregue como otra opcion un DropDownList y aunque me sigue guardando el valor que toma del DropDownList es = 0.

No se nada de ASP.NET, estoy partiendo con este lenguaje, me podrian orientar como aprender y como guardar el value del DropDownList, no el texto seleccionado:

CODIGO DE FORMULARIO:

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Usuarios.aspx.vb" Inherits="Usuarios" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <style type="text/css">

    .auto-style24 {
        width: 99%;
    }
        .auto-style23 {
        width: 138px;
    }
    .auto-style20 {
        width: 138px;
        height: 51px;
    }
    .auto-style21 {
        width: 579px;
        height: 51px;
    }
    .auto-style22 {
        height: 51px;
    }
        .auto-style10 {
            width: 121px;
            height: 30px;
        }
        .auto-style11 {
            width: 14px;
            height: 30px;
        }
        .auto-style12 {
            width: 166px;
            height: 30px;
        }
        .auto-style13 {
            width: 17px;
            height: 30px;
        }
        .auto-style14 {
            width: 25px;
            height: 30px;
        }
        .auto-style15 {
            width: 21px;
            height: 30px;
        }
        .auto-style16 {
            height: 30px;
        }
        .auto-style19 {
            width: 198px;
            height: 30px;
        }
        .auto-style25 {
            width: 121px;
            height: 29px;
        }
        .auto-style26 {
            width: 14px;
            height: 29px;
        }
        .auto-style27 {
            width: 198px;
            height: 29px;
        }
        .auto-style4 {
            width: 14px;
        }
        .auto-style6 {
            width: 166px;
        }
        .auto-style7 {
            width: 17px;
        }
        .auto-style8 {
            width: 25px;
        }
        .auto-style9 {
            width: 21px;
        }
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <table class="auto-style24">
        <tr>
            <td class="auto-style23">&nbsp;</td>
            <td class="auto-style2" align="center">
                <h2>Agregar Usuario</h2>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style20"></td>
            <td class="auto-style21"></td>
            <td class="auto-style22"></td>
        </tr>
        <tr>
            <td class="auto-style23">&nbsp;</td>
            <td class="auto-style2">
                <table width="100%">
                    <tbody>
                        <tr>
                            <td class="auto-style10">Rut</td>
                            <td class="auto-style11">:</td>
                            <td class="auto-style12">
                                <asp:TextBox ID="rut" runat="server" Width="157px" ErrorMessage="Campo Requerido" ></asp:TextBox>
                            </td>
                            <td class="auto-style13"></td>
                            <td class="auto-style14">DV</td>
                            <td class="auto-style15">:</td>
                            <td class="auto-style16">
                                <asp:TextBox ID="dv" runat="server" Width="34px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style10">Nombre Usuario</td>
                            <td class="auto-style11">:</td>
                            <td class="auto-style19" colspan="5">
                                <asp:TextBox ID="nom_usuario" runat="server" Width="340px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style25">Email Usuario</td>
                            <td class="auto-style26">:</td>
                            <td class="auto-style27" colspan="5">
                                <asp:TextBox ID="mail_user" runat="server" Width="339px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style25">Tipo de Usuario</td>
                            <td class="auto-style26">:</td>
                            <td class="auto-style27" colspan="5">
                                <!--<asp:TextBox ID="p_user" runat="server" Width="339px"></asp:TextBox>-->
                                <!--<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="DESC_PERFIL" DataValueField="PERFIL_USUARIO">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationGestion %>" SelectCommand="SELECT [PERFIL_USUARIO], [DESC_PERFIL] FROM [RPT_PERFIL_USUARIO]
"></asp:SqlDataSource>-->
                                   <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true">
                                    <asp:ListItem Value="0">Seleccione Tipo...</asp:ListItem>
                                       <asp:ListItem Value="1">Usuario</asp:ListItem>
                                       <asp:ListItem Value="2">Administrador</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style3">&nbsp;</td>
                            <td class="auto-style4">&nbsp;</td>
                            <td class="auto-style6">&nbsp;</td>
                            <td class="auto-style7">&nbsp;</td>
                            <td class="auto-style8">&nbsp;</td>
                            <td class="auto-style9">&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="auto-style3">&nbsp;</td>
                            <td class="auto-style4">&nbsp;</td>
                            <td class="auto-style6">
                                <asp:Button ID="Btn_add" runat="server" Height="24px" Text="Agregar" Width="75px" />
                            </td>
                            <td class="auto-style7" colspan="3">
                                <asp:Button ID="Btn_limp" runat="server" Text="Limpiar" Width="76px" />
                            </td>
                            <td>&nbsp;</td>
                        </tr>
                    </tbody>
                </table>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style23">&nbsp;</td>
            <td class="auto-style2">&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
</asp:Content>
 

      

CODIGO DE COMO GUARDO LOS DATOS:

Protected Sub Btn_add_Click(sender As Object, e As EventArgs) Handles Btn_add.Click
    conex.Open()

    Dim sqlct As New SqlCommand("insert into dbo.RPT_USUARIO (RUT_USUARIO,DV_USUARIO,NOM_USUARIO,EMAIL_USUARIO,PERFIL_USARIO) values('" & rut.Text & "','" & dv.Text & "','" & nom_usuario.Text & "','" & mail_user.Text & "','" & p_user.Text & "')", conex)
    sqlct.ExecuteNonQuery()

    conex.Close()

End Sub

SALUDOS!!!!!!!!!!!!!!!!!!

Y GRACIAS A QUIEN SE ANIME A AYUDARME!!!!!

1

1 respuesta 1

0

Tenes varias formas de seleccionar un elemento dentro del DropDownList. Depdende de que necesites.

var item1 = DropDownList2.SelectedItem;
var item2 = DropDownList2.SelectedIndex;
var item3 = DropDownList2.SelectedValue;
1
  • ramiro, creo que mi pregunta no está del todo formulada; intentare en esta ocasión expresarme de forma correcta: lo que necesito realizar es que teniendo estas opciones en el dropdownlist: “<asp:ListItem Value="2">Administrador</asp:ListItem>” es que la persona que interactúa con el formulario web, solo ve “Administrador” y cuando lo envíe a guardar a la base de datos se guarde Value="2" y no “Administrador”. Saludos
    – Charlees
    el 2 mar. 2020 a las 20:33

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.