简体   繁体   中英

DropDownList event not firing with AutoPostBack set to true

I have DropDownList in my webpage and I would like to fill TextBoxes every time when user change selected value in DropDownList.

My problem is that this event is not firing when I change this value. Interesting thing is, that this event is fired only when I change value AND press button, which create new request.

I tried to change AutoPostBack value to true, tried to change ViewStateMode and i tried fire this function on textChanged event. I also added condition !isPostBack before databinding function.

if (!IsPostBack)
{
    LoadDatabaseContent();
}

Here is event function.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();

    byte[] buf = new byte[8192];

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://localhost:44333/Backend2.aspx?update=true&id=" + DropDownList1.SelectedItem.Text);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    Stream resStream = response.GetResponseStream();
    int count;  
    do
    {
        count = resStream.Read(buf, 0, buf.Length);
        if (count != 0)
        {
            string tempString = Encoding.ASCII.GetString(buf, 0, count);

            sb.Append(tempString);
        }
    }
    while (count > 0);
    Response.Write(sb.ToString());

    //createTextBox.Text = sb.ToString();

    resStream.Dispose();
    response.Dispose();
}

ASPX Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Backend.aspx.cs" Inherits="APIS.Backend" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:Button ID="buttonGeneratePdf" runat="server" OnClick="buttonGeneratePdf_Click" Text="Generate PDF" />
        <br />
        <asp:Button ID="buttonGenerateQrCode" runat="server" OnClick="buttonGenerateQrCode_Click" Text="Generate QR" />
        <br />
    </div>
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server" Width="400px" Height="16px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 
OnTextChanged="DropDownList1_TextChanged">
    </asp:DropDownList>
    </div>
</form>
</body>
</html>

EDIT:

I have two "backends" that communicate between each other. First create REST requests and second accept this request and based on query string get desired value from database or in this case, generate QR code.

When I press button it create request. For example:

protected void buttonGenerateQrCode_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        byte[] buf = new byte[8192];

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://localhost:44333/Backend2.aspx?generateqrcode=true&id=" + DropDownList1.SelectedItem.Text);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Stream resStream = response.GetResponseStream();
        int count;
        do
        {
            count = resStream.Read(buf, 0, buf.Length);
            if (count != 0)
            {
                string tempString = Encoding.ASCII.GetString(buf, 0, count);

                sb.Append(tempString);
            }
        }
        while (count > 0);
        Response.Write(sb.ToString());

        //createTextBox.Text = sb.ToString();

        resStream.Dispose();
        response.Dispose();
    }

And in backend which communicate with database is this request accepted:

    protected void Page_Load(object sender, EventArgs e)
    {
        string generateQrCode = Request.QueryString["generateqrcode"];
        if (generateQrCode != null && generateQrCode == "true") GenerateQrCode();
    }

Which calls function:

private void GenerateQrCode()
    {
        string id = Request.QueryString["id"];

        QRCodeGenerator qr = new QRCodeGenerator();
        QRCodeData data = qr.CreateQrCode(id, QRCodeGenerator.ECCLevel.Q);
        QRCode code = new QRCode(data);
        Bitmap QRbmp = code.GetGraphic(5);
        QRbmp.Save("pathtofile..\\QRcode.jpg");
    }

It appears that the Javascript is failing to execute because the elements __EVENTTARGET and __EVENTARGUMENT do not exist on your form. On the generated HTML, the form should contain the following hidden elements:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

You should try to confirm if these elements are being generated, and if not try to track down why they're not there. You may need to use an <asp:Form> tag rather than <form runat="server"> but that's really just a guess on my part at this point.

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