我有 DropDownList ,我必须在与另一个控件的单击事件关联的事件中填充它。数据已填充并存在,但是当我选择一个值并回发时,这些值为空。这意味着视图状态不起作用。解决方案说在 Init() 中填充 DropDown 但我不能,因为要求不允许这样做。我必须在点击事件上填充它.. Viewstates 已启用.. 以下代码填充了 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();
}
以下是从下拉列表中检索值的回发代码。下拉列表位于 Gridview 行内。
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 是有麻烦的下拉菜单。