Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from the row of a Dropdown in a Gridview

I have a Gridview with a column that has a DropDownList. I've binded this Dropdownlist with an event on the "SelectedIndexChanged". The problem is i can't get the value of a label of another column in the same row.

The code is the next:

protected void grid_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        grdCredenciales.DataBind();

        var dropdown = (DropDownList)sender;
        var row = (GridViewRow)dropdown.NamingContainer;
        var label = (Label)row.FindControl("lblMatricula");

        var value = label.Text; // I get "" in this line.
    }

And in the grid i have:

<asp:ObjectDataSource ID="CredencialesDS" runat="server" />
<asp:GridView ID="grdCredenciales" runat="server" BackColor="White" DataSourceID="CredencialesDS"
    CssClass="DDGridView" RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6" AllowSorting="True"
    AllowPaging="True" AutoGenerateColumns="False" PageSize="10" OnRowDataBound="grdCredenciales_OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
               <asp:Label ID="Label7" ToolTip="Matrícula" runat="server" Text="Matrícula"/>
           </HeaderTemplate>
            <HeaderStyle HorizontalAlign="Left" Width="15%"/>
            <ItemStyle HorizontalAlign="Left" />
            <ItemTemplate>
                <asp:Label ID="lblMatricula" runat="server"><%# Eval("Matricula") %></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
               <asp:Label ID="Label19" ToolTip="Estado" runat="server" Text="Estado" />
           </HeaderTemplate>
            <HeaderStyle HorizontalAlign="Left" Width="15%"/>
            <ItemStyle HorizontalAlign="Left" />
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="dpEstadoCredencial" AutoPostBack="True" OnSelectedIndexChanged="grid_OnSelectedIndexChanged" CssClass="comboEstado"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I don't know why, but label.text returns an empty string. As you can see, i am calling the DataBind before, so the label should have a value at this point.

Do you know how can i get the value i need from the label in another column?

Thanks for everyone.

like image 893
ascherman Avatar asked Dec 06 '25 00:12

ascherman


1 Answers

Check the GridView's DataSource before you do the DataBind(). Since you're missing the full ASPX markup, I'm not sure if you're setting the data source programmatically or with a SqlDataSource.

In any case, what will happen often with programmatically-set Data Sources is that they disappear on a PostBack, and when you call that DataBind, you're really DataBinding it to null, which would explain why you're getting string.Empty ("") for the Label's Text property.

like image 113
Garrison Neely Avatar answered Dec 08 '25 15:12

Garrison Neely