Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

asp.net - Dropdownlist loses its value content upon being redirect to another webform page

I require a bit of assistance to solve this problem. I've been a bit stuck for a week now and I even asked for help to another programmer and he didn't know what to do too. I will explain everything.

Basically I have this gridview in one webform page called "Tabla_Gestiones.aspx"

 <asp:GridView ID="gvwGestiones" runat="server" CellPadding="10" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="2px" AutoGenerateColumns="False" DataKeyNames="intIdGestiones" AllowSorting="True" CellSpacing="10" HorizontalAlign="Center" Width="50%" >
      <Columns>
          <asp:buttonfield buttontype="Button" runat="server" commandname="Select" text="Detalle" />
          <asp:HyperLinkField Text="Modificar" DataNavigateUrlFields="intIdGestiones" DataNavigateUrlFormatString="Modificar_Gestiones-SistemaRegistroGestiones.aspx?intIdGestiones={0}" />                                                                    
           <asp:BoundField DataField="intIdGestiones" HeaderText="Código de Gestión"    >
               <HeaderStyle HorizontalAlign="Center" />
           </asp:BoundField>
           <asp:BoundField DataField="vchTipoGestiones" HeaderText="Tipo de Gestión"  >
               <HeaderStyle Width="200px"></HeaderStyle>
           </asp:BoundField>
           <asp:BoundField DataField="intCedulaUsuario" HeaderText="Cédula del Denunciante"  >
               <HeaderStyle Width="200px"></HeaderStyle>
           </asp:BoundField>
           <asp:BoundField DataField="vchNombreUsuario" HeaderText="Nombre del Denunciante"  >
                <HeaderStyle Width="200px"></HeaderStyle>
           </asp:BoundField>
           <asp:BoundField DataField="dtiFechaIngreso" HeaderText="Fecha de Ingreso"  HeaderStyle-Width="120px"> 
                <HeaderStyle Width="120px"></HeaderStyle>
           </asp:BoundField>
            <asp:BoundField DataField="vchFuenteGeneradora" HeaderText="Fuente Generadora"  HeaderStyle-Width="120px">
               <HeaderStyle Width="120px"></HeaderStyle>
            </asp:BoundField>                           
            <asp:BoundField DataField="vchDescripcionUnidad" HeaderText="Descripción Unidad"  HeaderStyle-Width="120px" >
               <HeaderStyle Width="120px"></HeaderStyle>
            </asp:BoundField>
            <asp:BoundField DataField="vchDescripcionDespacho" HeaderText="Descripción Despacho" HeaderStyle-Width="120px">
                <HeaderStyle Width="120px"></HeaderStyle>
             </asp:BoundField>                                                                 
     </Columns>                         
</asp:GridView>                     
<asp:SqlDataSource runat="server" ID="sdsGridView_Gestiones" ConnectionString='<%$ ConnectionStrings:bda_SIREGE_Connection %>' SelectCommand="mostrarGestiones" SelectCommandType="StoredProcedure" ></asp:SqlDataSource>

Is a simple gridview the problem comes here. When I select the HyperLinkField it takes me to another webform called "Modificar_Gestiones.aspx"

The purpose of this page is to read the data of the gridview and print the information to the textboxes inside this second aspx page. Here is the code I use to get the data from the gridview:

Private ReadOnly Property id As Integer
   Get
       Return If(Not String.IsNullOrEmpty(Request.QueryString("intIdGestiones")), Integer.Parse(Request.QueryString("intIdGestiones")), 0)
   End Get
End Property

Private Sub TomarGestion()
        Dim strQuery As String = "palAgarrarGestiones"
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = strQuery
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@intIdGestiones", id)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        For Each dr As DataRow In dt.Rows
            Me.txtId_Gestiones.Text = dr("intIdGestiones").ToString()
            Me.rblTipo_Gestion.Text = dr("vchTipoGestiones").ToString()
            Me.txtCedula_Usuario.Text = dr("intCedulaUsuario").ToString()
            Me.txtNombre_Usuario.Text = dr("vchNombreUsuario").ToString()
            Me.txtFecha_Ingreso.Text = Convert.ToDateTime(dr("dtiFechaIngreso").ToString())
            Me.ddlFuente_Generadora.Text = dr("vchFuenteGeneradora").ToString()
            Me.ddlDescripcion_Unidad.Text = dr("vchDescripcionUnidad").ToString()
            Me.txtDespacho.Text = dr("vchDescripcionDespacho").ToString()            
        Next
    End Sub

Here txt means a textbox and ddl means a dropdownlist. This code works but only one gives me problem this one: "Me.ddlDescripcion_Unidad.Text = dr("vchDescripcionUnidad").ToString()"

It always returns

Details of the Exception: System.ArgumentOutOfRangeException: 'ddlDescripcion_Unidad'has a SelectedValue that is not valid since doesn't exist in the element list. 
Parameter name: value

I debug it and apparently the values inside ddlDescripcion_Unidad dissapear so since the dropdownlist is empty the selected item cannot work. And I dont know what do to. ddlDescripcion_Unidad is a special dropdownlist I will show you:

<td class="auto-style3"><asp:DropDownList ID="ddlDescripcion_Unidad" runat="server" CssClass="form-control input-sm" Height="29px" Width="283px" AutoPostBack="True" OnSelectedIndexChanged="ddlDescripcion_Unidad_SelectedIndexChanged" DataSourceID="sdsDescripcion_Unidad" DataTextField="vchDescripcionUnidad" DataValueField="intIdUnidad" ></asp:DropDownList >
   <asp:SqlDataSource runat="server" ID="sdsDescripcion_Unidad" ConnectionString='<%$ ConnectionStrings:bda_SIREGE_Connection %>' SelectCommand="palConsultarUnidades" SelectCommandType="StoredProcedure"></asp:SqlDataSource></td>

This was done this way because Unidad is a table and Gestiones is another table and Gestiones needed vchdescripcionesUnidad from Unidad table so to insert it, I needed to do this way.

Please anyone help me here this is the last thing I must fix to finish the system and I don't know what to do I ran out of ideas...I will have to try to mask the error but I really dont want to.

Before someone ask the stored procedure palAgarrarGestiones does show the information. I think this problem happens at the front end not the database...

question from:https://stackoverflow.com/questions/65950752/dropdownlist-loses-its-value-content-upon-being-redirect-to-another-webform-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, why do we need to know or care about some listview or grid view from a previous page? (answer: we don't need that information nor does it help).

(so your providing all kinds of information that does not matter - this might help explain your troubles here).

Your problem as stated is this:

I have a drop down list - I am having trouble assigning that drop down list a value.

One line of code!!!

Well, the first issue is a drop down list has TWO values. The display value, and the "behind" value. Thus it is VERY common to fill a combobox (dropdownlist) with two values.

You have:

DataTextField="vchDescripcionUnidad" 
DataValueField="intIdUnidad"

So which kind of value are you attempting to assign here?

You can NOT directly assign the dropdown list a "text" value. You have to shove in the DataValue - not the text.

So, setting by "behind value" you can use this code:

DropDownList1.Text = 78

or

DropDownList1.SelectedValue = 78

But what if we only have the "text" value and not the number (DataValue).

(so just keep in mind that dropdown.Text = the data value behind - not the display text field).

To set by some text (display) value? We have to find + search the list.

MS-Access and maybe VB6 allows assign of either way.

But, we can't do this:

 DropDownList1.SelectedItem = "Athabasca Hotel"

So, we have to do this:

 DropDownList1.SelectedValue = DropDownList1.Items.FindByText("Athabasca Hotel").Value

So the problem as stated here is one line of code. So the problem as stated here is how to assign a dropdownlist a text value as opposed to the value behind.

All the other code, comments etc. is not really relevant to this problem.

But, if we want to assign the drop list by text display value? You have to translate that "text" supplied value to the number value used for that dropdown list.

So this:

Me.ddlDescripcion_Unidad.Text = dr("vchDescripcionUnidad")

becomes

Me.ddlDescripcion_Unidad.Text = ddlDescripcion_Unidad.Items.FindByText(dr("vchDescripcionUnidad")).Value

Edit:

Ok, while all of the above is much "fun and games"? The FIRST issue and ONLY issue we need to work on, think about, deal with, and resolve is to get/grab/pull the row we want to work on - that's quite much REALLY all we need to get working. All of that code, setting the combo box on the next page etc? That all comes AFTER we get the simple idea, the simple concept of selecting the ONE row value to pass to that other routine.

So, lets work on that:

we have this:

   <asp:HyperLinkField Text="Modificar" 
    DataNavigateUrlFields="intIdGestiones" 
      DataNavigateUrlFormatString="Modificar_Gestiones- 
      SistemaRegistroGestiones.aspx?intIdGestiones={0}" />                                                                    

We could change above to:

   <asp:HyperLinkField Text="Modificar" 
    DataNavigateUrlFields="intIdGestiones" 
      DataNavigateUrlFormatString="Modificar_Gestiones- 
      SistemaRegistroGestiones.aspx?intIdGestiones='<%# Eval("intIdGestiones") %>' />                                                                    

So now when you click on the link field, the value of intIdGestionses should be passed to your 2nd page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...