简体   繁体   中英

how to get dropdown selected index value without postback with button click

below code; code not return value empty string

private void DisplayDplace()
    {
        BusinessLogicLayer.Businesslogiclayer objll = new BusinessLogicLayer.Businesslogiclayer();
    DataSet ds = objll.Binddname();
    drpfromplace.DataSource = ds;
    drpfromplace.DataTextField = "dname";
    drpfromplace.DataValueField = "did";
    drpfromplace.DataBind();
    drptoplace.DataSource = ds;
    drptoplace.DataTextField = "dname";
    drptoplace.DataValueField = "did";
    drptoplace.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DisplayDplace();
    }
}

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
    TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
}

protected void Button1_Click(object sender, EventArgs e)
{
    Session["doj"] = TextBox1.Text;
    Session["noofseats"] = DropDownList6.SelectedItem.ToString();
    Session["fd"]= drpfromplace.SelectedItem.Value.ToString();
    Session["td"] = drptoplace.SelectedItem.Value.ToString();
}      

}

You will have to make use of client side scripting like javascript/jquery if you want to get value of dropdown withot postback. In case you want to get the value in the code behind then you will need to create an ajax call.

Create a webmethod in the code behind:

[WebMethod]
        public static string MyWebMethod(string name )
        {
            /your logic goes here
            return ("Yeey");
        }

Now Create an Ajax Call on the aspx.cs:

$('#mybutton').on('click',function(){

 $.ajax({
            type: "POST",
            url: "Default.aspx/MyWebMethod",
            data: JSON.stringify({ name: $('#drpname').val() }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (data, status) {
                alert("victory");
            },               
            failure: function (data) {
                alert(data.d);
            },
            error: function (data) {
                alert(data.d);
            }
        });

});

You can add a ScriptManager to the page and wrap the code inside an UpdatePanel .

   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Text="A" Value="a"></asp:ListItem>
                <asp:ListItem Text="B" Value="b"></asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:Button ID="Button1" runat="server" Text="getValue" OnClick="Button1_Click" />
            <br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

        </ContentTemplate>
    </asp:UpdatePanel>

And code behind works the same as always.

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DropDownList1.SelectedValue;
    }

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