我正试图从一个简单的asp.net表单中保存价值。
我在网页上几乎没有像下拉菜单那样的控件。文本框的
我在页面加载时填写第一个下拉菜单&Language;触发后晒,以填充第二个下拉列表ddCategoryType
,它根据所选语言填充正确的值,但问题是当我尝试获取ddCategoryType.SelectedItem的按钮点击事件值的值。由于某些我现在无法弄清楚的原因,值总是返回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)
{ }
}
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>
回发后的ddCategoryType的OutPut
<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>
答案 0 :(得分:8)
在Page_Load
中,您正在清除ddCategoryType
的内容,因此SelectedIndex
将重置为0.
请记住,总是调用Page_Load,它会在Button_Click事件之前发生。
有几种方法可以解决这个问题,例如:如果SelectedIndex >= 0
答案 1 :(得分:3)
由于您希望根据ddSelectLanguage中的选定值填充ddCategoryType,然后根据该交互填充DDL,而不是在Page_Load中填充。
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"));
}
另外,让您的查询获取参数。