After much research online, I am still shocked by this problem. I have a page that loads the names and the number of categories into a drop-down list. I only do this if !(Page.IsPostBack)
. When AutoPostBack
fired SelectedIndex = 0
. I tried a few different things. Here is my code:
Page
<form id="AddAssignmentForm" runat="server"> <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" /> <asp:UpdatePanel ID="CommentUpdate" runat="server"> <ContentTemplate> Add Comment <asp:DropDownList ID="ddlCategory" runat="server" Width="206" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" AutoPostBack="true" /> <asp:TextBox ID="txtName" runat="server" Width="200" /> <asp:TextBox ID="txtAbbrv" runat="server" Width="200" /> <asp:TextBox ID="txtDescription" runat="server" Width="200" Height="90" TextMode="MultiLine" /> </ContentTemplate> </asp:UpdatePanel> </form>
Here is the code end code.
private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { GetCategories(); } } public void GetCategories() { String strSql = @"SELECT Name, Total FROM MyTable"; if (con.State == ConnectionState.Closed) con.Open(); OleDbCommand cmdsql = new OleDbCommand(strSql, con); OleDbDataReader cmdReader = cmdsql.ExecuteReader(); if (cmdReader.HasRows) { while (cmdReader.Read()) { ddlCategory.Items.Add(new ListItem(cmdReader["Category_Name"].ToString(), cmdReader["Total"].ToString())); } ddlCategory.SelectedIndex = -1; } cmdReader.Close(); con.Close(); } public void FillForm(int index) { ListItem item = ddlCategory.Items[index]; txtName.Text = item.Text + " " + (Convert.ToInt32(item.Value) + 1).ToString(); txtAbbrv.Text = item.Text.Substring(0, 1) + (Convert.ToInt32(item.Value) + 1).ToString(); } public void ddlCategory_SelectedIndexChanged(Object sender, EventArgs e) {
I just want to fill out a form based on the selected index, but I cannot get the correct answer. Any help is appreciated.
source share