I am loading a list of value/text into a asp:dropdownlist by calling a stored procedure. I populate the dropdownlist on the PageLoad method inside a !Page.IsPostBack block like so:
if (!Page.IsPostBack)
{
GetDropDownLists();
DataBind();
}
And this is my code implementation for the backend:
protected void GetDropDownLists()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("get_articletype", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
xArticleTypeList.Items.Clear();
xArticleTypeList.Items.Insert(0, new ListItem("- Select.. -", "0"));
xArticleTypeList.SelectedIndex = 0;
xArticleTypeList.DataSource = dt;
xArticleTypeList.DataValueField = "TypeValue";
xArticleTypeList.DataTextField = "TypeName";
xArticleTypeList.DataBind();
}
If my code isn't inside a !Page.IsPostBack block, after I click my save button the default value will always be the first item of the dropdownlist. But once I put my code inside the !Page.IsPostBack block my dropdownlist is empty. For reference here is the frontend implementation of my asp:dropdownlist.
<asp:DropDownList ID="xArticleTypeList" EnableViewState="true" AutoPostBack="true" CssClass="form-control" runat="server" />
I understand that there are other topics that cover this question, but none of the proposed solutions worked for me. Thanks you in advance for your inputs.
CodePudding user response:
Your problem is that you inserting the blank please select row BEFORE you bind. As a result, when you bind, you are blowing out the drop down.
Try it this way:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmd = new SqlCommand("get_articletype", conn))
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
dt.Load(cmd.ExecuteReader());
xArticleTypeList.DataSource = dt;
xArticleTypeList.DataValueField = "TypeValue";
xArticleTypeList.DataTextField = "TypeName";
xArticleTypeList.DataBind();
xArticleTypeList.Items.Insert(0, new ListItem("- Select.. -", "0"));
}
}
}
And GET RID of your extra call re-bind You have this:
GetDropDownLists();
DataBind(); <--- remove unless you need, or execute BEFORE the GetDropDownList()
So move your add/insert blank row to the drop down to AFTER the drop down data bind.
And REMOVE your DataBind() - you don't need it, and if for some reason you do, then move the DataBind() to before your load of the GetDropDownLists() - but my guess is that you just tossed that in the hope of making this work, and it going to cause the combo box to re-select - so remove unless you give a REALL GOOD HIGH quality outline and narrative as to why you have the DataBin() command there - (but as as noted, if you really, but really really do need to execute that DataBind() command - move it to before the load of the combo).
So, add the blank row after