【发布时间】:2012-06-25 10:42:07
【问题描述】:
我有一个在网格视图中使用的状态下拉列表,用户可以在编辑网格视图中的行时选择他们想要的列表项。问题是当前实现没有检索选定的值,而只是检索默认/加载的值。
这是网格视图的定义:
<asp:GridView ID="applicationGrid" runat="server" Width="95%"
AutoGenerateEditButton="True"
AutoGenerateColumns="False"
ShowFooter="True"
CellSpacing="10"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left"
OnRowUpdating="applicationGrid_RowUpdating"
OnRowEditing="applicationGrid_RowEditing"
OnRowCancelingEdit="applicationGrid_RowCancelingEdit"
AutoPostBack="true" >
这是下拉列表将在编辑时出现的列的定义:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="StatusDescription" runat="server" Text='<%# Eval("STATUS_DESCRIPTION") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
这是处理更新场景的代码:
protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = applicationGrid.Rows[e.RowIndex];
applicationGrid.EditIndex = -1;
Label applicationCodeLabel = row.FindControl("AppID") as Label;
TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
string applicationName = applicationNameTextBox.Text;
string applicationURL = applicationURLTextBox.Text;
int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
//string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
//string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
application.UpdateApplication(applicationCode, applicationName, applicationURL);
PopulateApplications();
}
一切正常,但所选值不是加载的值,也不是用户选择的值。因此问题是从列表中获取选定的值。需要改变什么,为什么?
protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = applicationGrid.Rows[e.RowIndex];
applicationGrid.EditIndex = -1;
Label applicationCodeLabel = row.FindControl("AppID") as Label;
TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
string applicationName = applicationNameTextBox.Text;
string applicationURL = applicationURLTextBox.Text;
int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
//string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
//string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
application.UpdateApplication(applicationCode, applicationName, applicationURL);
PopulateApplications();
}
编辑:添加我的填充方法:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
PopulateApplications();
}
catch (Exception exception)
{
throw exception;
}
}
}
private void PopulateApplications()
{
DataTable reader = application.GetApplicationList();
applicationGrid.DataSource = reader;
applicationGrid.DataBind();
applicationGrid.AllowSorting = true;
}
protected void DropDownLoadEdit(object sender, EventArgs e)
{
DataTable statusTable = application.GetStatusList();
DropDownList dropdown = sender as DropDownList;
dropdown.DataSource = statusTable;
dropdown.DataTextField = "status_description";
dropdown.DataValueField = "application_status_code";
dropdown.DataBind()
}
更新 #2:我正在尝试在类中填充用于所选索引的静态变量。这将在按下更新时使用。但是,这仍然是获取下拉列表的原始值,而不是选定的值。
这是方法:
protected void StatusDescriptionList_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
selectedValue = Convert.ToInt32(ddl.SelectedValue.ToString());
}
【问题讨论】:
-
您是否进行过调试以查看此时选择的值(如果有): DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
-
@Gaz Winter - 是的,值始终为 1,这是默认值。当我选择应该是 2 的值时,它仍然是 1。因此,在下拉列表中没有检测到选择的变化。
-
-
@MMK - 当我将其设置为 AutoPostBack="true" 时,它会刷新页面并重新加载旧值。还有其他步骤吗?
-
protected void ddlList_SelectedIndexChanged(object sender, EventArgs e) { //在这里找到DropDownList控件 int intId = Convert.ToInt32(NameofDDLhere.SelectedValue); }