I am trying to save a value from a simple asp.net form.
I have several controls on the page, such as a drop-down list and a text box
I populate the first drop-down list on the language page and run post-bask on the same to populate the second ddCategoryType drop-down list, which it populates with the correct values based on the selected language, but the problem is when I try to get the value at the event value button clicks for ddCategoryType.SelectedItem.Value always returns 0 for some reason that I cannot figure out right now
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddSelectLanguage.Items.Clear(); ddSelectLanguage.DataSource = DataProvider.GetLanguages(); ddSelectLanguage.DataBind(); ddSelectLanguage.Items.Insert(0, new ListItem("Select Language", "0")); } else { ddCategoryType.Items.Clear(); String strSql = "SELECT TypeName, TypeID FROM CategoryType WHERE LangID =" + ddSelectLanguage.SelectedItem.Value.ToString(); DataSet ds = new DataSet(); ds = DataProvider.Connect_Select(strSql); ddCategoryType.DataSource = ds; ddCategoryType.DataBind(); ddCategoryType.Items.Insert(0, new ListItem("Select Type", "0")); } } protected void btnSaveCategory_Click(object sender, EventArgs e) { objArtCat.LanguageID = int.Parse(ddSelectLanguage.SelectedItem.Value.ToString()); objArtCat.CategoryName = txtCategoryName.Text; objArtCat.CategoryType = int.Parse(ddCategoryType.SelectedItem.Value.ToString()); objArtCat.CategoryActive = bool.Parse(ddCategoryActive.SelectedItem.Value.ToString()); try {
SAMPLE.ASPX CODE
<div class="row"></div> <div class="row"> <asp:Label ID="lblSelectLang" CssClass="txtLabel" Text="Select Language :" runat="server" ></asp:Label> <asp:DropDownList ID="ddSelectLanguage" runat="server" CssClass="ddGeneral" DataTextField="LangName" DataValueField="LangID" CausesValidation="True" AutoPostBack="True" > </asp:DropDownList> <asp:RequiredFieldValidator ID="rfvddLanguage" runat="server" ErrorMessage="Please Select Language" ControlToValidate="ddSelectLanguage" InitialValue="Select Language" ValidationGroup="atpAddNewArticle" ></asp:RequiredFieldValidator> </div> <div class="row"> <asp:Label ID="lblCategoryName" CssClass="txtLabel" runat="server" Text="Category Name :"></asp:Label> <asp:TextBox ID="txtCategoryName" runat="server" CssClass="txtbox300"></asp:TextBox> <asp:RequiredFieldValidator ID="RFVtxtAuthorName" runat="server" ErrorMessage="*" ControlToValidate="txtCategoryName" ValidationGroup="atpAddNewArticle" CssClass="validation"></asp:RequiredFieldValidator> </div> <div class="row"> <asp:Label ID="lblCategoryType" CssClass="txtLabel" runat="server" Text="Category Type :"></asp:Label> <asp:DropDownList ID="ddCategoryType" runat="server" CssClass="ddGeneral" DataTextField="TypeName" DataValueField="TypeID" > </asp:DropDownList> </div> <div class="row"> <asp:Label ID="lblCategoryActive" CssClass="txtLabel" runat="server" Text="Category Active :"></asp:Label> <asp:DropDownList ID="ddCategoryActive" runat="server" CssClass="ddGeneral" > <asp:ListItem Value="False" Selected="True">NO</asp:ListItem> <asp:ListItem Value="True">YES</asp:ListItem> </asp:DropDownList> </div> <div class="rowButton"> </br> <asp:Button ID="btnUpdateArticle" runat="server" Text="Save Category" CssClass="btn" ValidationGroup="atpAddNewArticle" onclick="btnSaveCategory_Click" /> <input id="Reset" type="reset" class="btn" value="Reset" /> </div>
OutPut for ddCategoryType after postback
<div class="row"> <span class="txtLabel" id="MainContent_lblCategoryType">Category Type :</span> <select class="ddGeneral" id="MainContent_ddCategoryType" name="ctl00$MainContent$ddCategoryType"> <option value="0" selected="selected">Select Type</option> <option value="1">Article</option> <option value="2">News</option> <option value="3">Sports</option> <option value="4">People</option> <option value="5">Message</option> </select> </div>
source share