Вопрос

i have a Gridview having a dropdownlit and button. now I want to get selected value of that dropdown list on button click.here is my sample code

<asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddPStatud" runat="server" DataSourceID="sdsProductStatus" DataTextField="StatusName"
                            DataValueField="StatusId">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdsProductStatus" runat="server" ConnectionString="<%$ ConnectionStrings:cs %>"
                            SelectCommand="SelectProductStatus" SelectCommandType="StoredProcedure">
                            <SelectParameters>
                                <asp:SessionParameter Name="Branchid" SessionField="Branchid" Type="Int16" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </ItemTemplate>
                </asp:TemplateField>

and here is the button

<asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="tst" runat="server" Text="text" CommandName="Test" CommandArgument='<%# Eval("EntryID") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>

and this is RowCommand function

if (e.CommandName == "Test")
            {
                String row = e.CommandArgument.ToString();
                lblError.Visible = true;
                lblError.Text = row;}
Это было полезно?

Решение

You can use this:

DropDownList dll = (DropDownList)((Control)e.CommandSource).NamingContainer.FindControl("ddPStatud");
String selectedValue = dll.SelectedValue;

NaimingContainer is the row in which the button is. And then in it you find the control with the ID you need.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top