Emre Aslan Emre Aslan - 13 days ago 6
ASP.NET (C#) Question

Dropdownlist not updating inside UpdatePanel?

I have a asp web site. I want to update textboxes while change dropdownlist. But I don't use autopostback. Dropdownlist getting values in database and I'm writing into textbox this values.How can I do inside updatePanel?

index.aspx

<div class="col-sm-8">
<asp:DropDownList ID="drp_MalAd" Width="130px" CssClass="form-control center-block" runat="server" AutoPostBack="true" DataSourceID="SqlDataSourceMalAd" DataTextField="MALAD" DataValueField="MALAD" OnSelectedIndexChanged="drp_MalAd_SelectedIndexChanged"></asp:DropDownList>
<asp:SqlDataSource runat="server" ID="SqlDataSourceMalAd" ConnectionString='<%$ ConnectionStrings:... %>' SelectCommand="SELECT ..."></asp:SqlDataSource>
</div>


And My index.aspx.cs code:

protected void drp_MalAd_SelectedIndexChanged(object sender, EventArgs e)
{
string malAdi = drp_MalAd.SelectedItem.Text.ToString();
SqlDataReader reader;

SqlCommand cmd = new SqlCommand("SELECT .. WHERE MALAD='" + malAdi + "'", con);
SqlCommand cmd1 = new SqlCommand("SELECT ...WHERE MALAD='" + malAdi + "'", con);
SqlCommand cmd2 = new SqlCommand("SELECT ... WHERE MALAD='" + malAdi + "'", con);
SqlCommand cmd3 = new SqlCommand("SELECT ... WHERE MALAD='" + malAdi + "'", con);

try
{
con.Open();

txt_AnaBirim.Text = cmd.ExecuteScalar().ToString();
txt_malKodu.Text = cmd1.ExecuteScalar().ToString();
txt_KDVOranim.Text = cmd2.ExecuteScalar().ToString();
con.Close();
}
catch (Exception)
{
}

try
{
con.Open();
reader = cmd3.ExecuteReader();
grd_StokDurumu.DataSource = reader;
grd_StokDurumu.DataBind();
reader.Close();
}
catch
{
}
finally
{
con.Close();
}
}

Answer

You wrap everything you want to update without a PostBack inside an UpdatePanel

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

            <asp:DropDownList ID="drp_MalAd" Width="130px" CssClass="form-control center-block" runat="server" AutoPostBack="true" DataSourceID="SqlDataSourceMalAd" DataTextField="MALAD" DataValueField="MALAD" OnSelectedIndexChanged="drp_MalAd_SelectedIndexChanged"></asp:DropDownList>
            <asp:SqlDataSource runat="server" ID="SqlDataSourceMalAd" ConnectionString='<%$ ConnectionStrings:... %>' SelectCommand="SELECT ..."></asp:SqlDataSource>

            <asp:TextBox ID="txt_AnaBirim" runat="server"></asp:TextBox>
            <asp:TextBox ID="txt_malKodu" runat="server"></asp:TextBox>
            <asp:TextBox ID="txt_KDVOranim" runat="server"></asp:TextBox>

    </ContentTemplate>
</asp:UpdatePanel>

And you need to add a ScriptManager to the page.

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