I have tried to look through many questions about the dropdownlist and still yet to resolve my issue.
I have a Dropdownlist and a Submit button. When Submit button is clicked after value selected, it should update the Label text with the selected value of the Dropdownlist. However, what happened was each time value is selected from dropdown, it refreshes the page and the value captured was the first item in dropdownlist. How do I capture the selected value correctly before it refreshes?
Am at wits end, not sure where went wrong.
*All items in dropdownlist are unique. No duplication.
My codes:
ASP.NET
<form id="form1" runat="server">
<asp:DropDownList ID="ddlCode" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCode_SelectedIndexChanged"/>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
<asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<ProgressTemplate>
<img alt="progress" src="img/loading.gif"/><br /><h2>Loading...</h2>
</ProgressTemplate>
</asp:UpdateProgress>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Button ID="submitBtn" runat="server" cssclass="btn btn-success" OnClick="submitBtn_Click" Text="Submit"/>
<asp:Label ID="lblCode" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCode" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
</form>
Code behind
string ddlSelectedIndex = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//This is to load code into dropdownlist which works fine.
LoadCode();
}
}
protected void ddlCode_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSelectedIndex =(ddlCode.Items[ddlCode.SelectedIndex].Text).Substring(0, 4);
}
protected void submitBtn_Click(object sender, EventArgs e)
{
lblCode.Text = ddlSelectedIndex;
}