DropDownList的项上回发空 [英] DropDownList-Items are null on postback
问题描述
我的DropDownList,我在与其他控制。该数据填充和present的单击事件相关联的事件来填充,但是当我选择一个值,回传值是零。这意味着视图状态不是工作..解决方案说,填充初始化(下拉),但我不能因为要求不允许这样。我必须填充它的单击事件.. Viewstates启用。以下code填充下拉..
I have DropDownList that I have to populate in an event associated with a click event of another control.. the data is populated and present but when I select a value and postback the values are null. Which means the view state is not working.. Solutions say that populate the DropDown in Init() but I can't because the requirement does not allow this. I have to populate it on click event .. Viewstates are enabled.. The following code populates the DropDown..
if (e.CommandName == "Add Friend")
{
HtmlGenericControl divMySub = (HtmlGenericControl)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("divMySubjects");
divMySub.Style["display"] = "block";
DropDownList mySub = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
UpdatePanel mySubPanel = (UpdatePanel)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("UpdatePanelRequestAction");
DataView SubjectTableView = ProfileDataAccess.GetUserUnusedSubjectsForRequest(UserId ,RequesterId).DefaultView;
if (SubjectTableView.Count > 0)
{
mySub.DataSource = SubjectTableView;
mySub.DataTextField = "Name";
mySub.DataValueField = "Id";
mySub.DataBind();
}
else
{
divMySub.InnerText = "Requests Complete";
LinkButton buttonlink= (LinkButton)sender;
buttonlink.Enabled = false;
}
mySubPanel.Update();
}
和下面是code为检索从下拉列表中选择值回发..下拉是一个GridView行内。
and the following is the code for the postback that retrieves the value from the Dropdown list.. The DropDown is inside a Gridview Row.
protected void LinkButtonAddFriend_Command(object sender, CommandEventArgs e)
{
Guid RequestedId = new Guid(Membership.GetUser().ProviderUserKey.ToString());
Guid UserId = new Guid(HiddenFieldUserId.Value.ToString());
int UserSubjectId = Convert.ToInt32 (GridViewUserSubjects.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
DropDownList DDL = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
LinkButton RequestAction = (LinkButton)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("LinkButtonFriendAction");
int RequesterSubjectId = Convert.ToInt32(DDL.SelectedItem.Value);
if (FriendsDataAccess.InsertRequest(UserId, RequestedId, UserSubjectId, RequesterSubjectId))
{
RequestAction.Text = "Remove Request";
RequestAction.Enabled = true;
}
}
DDL是麻烦下拉。
DDL is the Dropdown with trouble.
推荐答案
当你在数据绑定的 GridView控件
这是容器中的 DropDownList的
?你有一个包裹它,如果(!的IsPostBack)
-check?当您将数据绑定 GridView控件
子控件总是重置了。
When are you databinding the GridView
which is the container of the DropDownList
? Do you have wrapped it in a if(!IsPostBack)
-check? When you databind the GridView
the child controls are always "resetted".
如果你使用像 ObjectDataSource控件
的声明性数据绑定控件,你应该避免 this.databind()
如果它没有必要
If you use a declarative databound control like ObjectDataSource
you should avoid this.databind()
if it's not necessary.
这篇关于DropDownList的项上回发空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!