简体   繁体   中英

DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c#

我在GridView中有一个DropDownList,并且想在GridView中的TextBox中绑定DropDown的选定值,以在asp.net c#中的查询字符串中传递选定的值,请帮助我指导GridView的DropDownList 屏幕快照的 SelectedIndexChanged和代码

You could attach a method to the SelectedIndexChanged event on each child dropdown, either in the markup, either using a AddHandler in the RowDataBound event of your gridview

    <asp:GridView ID="gv1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSelectedIndexChanged">
                        <asp:ListItem Value="1"></asp:ListItem>
                        <asp:ListItem Value="2"></asp:ListItem>
                        <asp:ListItem Value="3"></asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:TextBox ID="txt1" runat="server" />
    <asp:Button ID="btn1" runat="server" Text="redirect" />

Save the value wherever you want (hidden or textbox) when the selected item is changed

protected void ddlSelectedIndexChanged(object sender, System.EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    txt1.Text = ddl.SelectedValue;
}

And use this value when clicking the button

private void btn1_Click(object sender, EventArgs e)
{
    Response.Redirect("mypage.aspx?q=" + txt1.Text);
}

or use jquery onChange event atttached to the dropdowns ?

$('select').on('change', function() {
  alert( this.value );
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM