Its my first time to encounter this problem so please anyone help me.
Here’s my problem I have 2 dropdownlist , the second one would be populated from the selected item in the first dropdownlist, and in the second dropdownlist it would populate the gridview depends on the chosen item. now the problem is, everytime I select any item from the 2nd ddl it posts back and the first item would be selected instead the item that I select.
heres my code: (aspx)
<asp:TableRow> <%--Branch--%>
<asp:TableCell HorizontalAlign="Left">
<asp:Label ID="lblBranch" runat="server" Font-Bold="true">Branch:</asp:Label>
</asp:TableCell>
<asp:TableCell HorizontalAlign="Left">
<asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_SelectedIndexChanged" AppendDataBoundItems="true" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow> <%--Recepient--%>
<asp:TableCell HorizontalAlign="Left">
<asp:Label ID="lblRecepient" runat="server" Font-Bold="true">Recepient:</asp:Label>
</asp:TableCell>
<asp:TableCell HorizontalAlign="left">
<asp:DropDownList ID="ddlRecepientDepartment" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlRecepientDepartment_SelectedIndexChanged" AppendDataBoundItems="true" EnableViewState="true"></asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow> <%--Gridview Recepient--%>
<asp:TableCell></asp:TableCell>
<asp:TableCell>
<asp:GridView ID="gvRecepientPosition" runat="server" AllowPaging="True" AutoGenerateColumns="False"
EmptyDataText="No Record found!" ShowFooter="false" ShowHeader="true" Width="99%" HorizontalAlign="Center" PageSize="5" OnPageIndexChanging="gvRecepientPosition_PageIndexChanging">
<Columns>
<%--<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("RECID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Recepient" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="cbRecepientPosition" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Position" HeaderText="Position" ItemStyle-HorizontalAlign="Left" />
</Columns>
</asp:GridView>
</asp:TableCell>
</asp:TableRow>
CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlBranch.Items.Clear();
GetBranch();
}
}
private void GetBranch()
{
var objManager = new EmailNotificationInfoManager();
// ddlBranch.Items.Clear();
ddlBranch.DataSource = objManager.EmailNotificationInfoBranch();
ddlBranch.DataTextField = "Branch";
ddlBranch.DataValueField = "RECID";
ddlBranch.DataBind();
ddlBranch.Items.Insert(0,"<--Select-->");
}
protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
{
var objManager = new EmailNotificationInfoManager();
ddlRecepientDepartment.Items.Clear();
ddlRecepientDepartment.DataSource = objManager.EmailNotificationInfoDepartment(Convert.ToInt64(ddlBranch.SelectedValue));
ddlRecepientDepartment.DataTextField = "Department";
ddlRecepientDepartment.DataValueField = "branchID";
ddlRecepientDepartment.DataBind();
ddlRecepientDepartment.Items.Insert(0,"<--Select-->");
}
protected void ddlRecepientDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
var objManager = new EmailNotificationInfoManager();
gvRecepientPosition.DataSource = objManager.GetPositionByDepartment(ddlRecepientDepartment.SelectedItem.Text);
gvRecepientPosition.DataBind();
Session["PositionDepartment"] = objManager.GetPositionByDepartment(ddlRecepientDepartment.SelectedItem.Text);
ddlRecepientDepartment.Attributes.Add("onChange", "this.currentvalue = this.value;");
//Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
}
PLEASE HELP ME WITH THIS ONE. THANK YOU!
If you mean the first dropdown is getting the first item selected then check with the
EnableViewState="true"property ofddlBranchAnd why this?