4

我试图从一个简单的 asp.net 表单中保存价值。

我在页面上几乎没有控件,例如下拉和文本框

我使用 Language & trigger post-bask 填充页面加载的第一个下拉列表,以填充第二个下拉列表ddCategoryType,它根据所选语言填充正确的值,但问题是当我尝试获取按钮上的值时ddCategoryType.SelectedItem.Value 的单击事件值由于某种原因总是返回 0,我现在无法弄清楚

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
    {
        //bool result;
        //result = objBLAddArticleCategory.CreateNewArticleCategory(objArtCat);
        //if (result == true)
        //{
        //    Response.Redirect("PageMessage.aspx?msg='Category has been Create Successfully'", true);
        //}
        //else
        //{
        //}
    }
    catch (Exception)
    { }
}

示例 .ASPX 代码

   <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>

回发后 ddCategoryType 的输出

<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>
4

2 回答 2

8

在您Page_Load中,您正在清除 的内容,ddCategoryType因此将SelectedIndex其重置为 0。

请记住,始终调用 Page_Load,并且它发生在 Button_Click 事件之前。

有几种方法可以处理这个问题,例如:不要执行 Page_Load 的else子句中的所有代码,如果SelectedIndex >= 0

于 2012-11-11T12:36:12.660 回答
3

由于您要基于从 ddSelectLanguage 中选择的值填充 ddCategoryType,因此基于该交互而不是在 Page_Load 中填充 DDL。

protected void Page_Load(object sender, EventArgs e)
{
    ddSelectLanguage.SelectedIndexChanged += new
        EventHandler(ddSelectLanguage_SelectedIndexChanged);
    if (!IsPostBack)
    {
        ddSelectLanguage.Items.Clear();
        ddSelectLanguage.DataSource = DataProvider.GetLanguages();
        ddSelectLanguage.DataBind();
        ddSelectLanguage.Items.Insert(0, new ListItem("Select Language", "0"));
    }
}

然后

 void ddSelectLanguage_SelectedIndexChanged(object sender, EventArgs e)
 {
        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"));
 }

另外,让您的查询带参数。

于 2012-11-11T15:39:53.193 回答