我有 gridview ,它在每一行中都包含下拉列表。下拉列表已选择索引更改事件。现在,当我在网格的任何行中更改下拉列表中的值时,它会为 gridview 中的所有行触发选定的索引更改事件。如何解决这个问题?
下面是 HTML 标记
<asp:GridView ID="gvEditFields" runat="server"
AllowSorting="false" AllowPaging="false" OnRowDataBound="gvEditFields_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Project">
<ItemTemplate>
<asp:DropDownList ID="ddlProjectList" runat="server" Width="130px" AutoPostBack="true" OnSelectedIndexChanged="ddlProjectList_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvProjectList" runat="server" ErrorMessage="* Required"
Display="Dynamic" ControlToValidate="ddlProjectList" SetFocusOnError="True" ValidationGroup="EditGridSave"
Text="Required" ></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:DropDownList ID="ddlTaskList" runat="server" AppendDataBoundItems="false" Width="150px" OnSelectedIndexChanged="ddlTaskList_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvTaskList" runat="server" ErrorMessage="* Required"
Display="Dynamic" ControlToValidate="ddlTaskList" SetFocusOnError="True" ValidationGroup="EditGridSave"
Text="Required" ></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Status">
<ItemTemplate>
<asp:DropDownList ID="ddlTaskStatus" runat="server" Width="100px"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvTaskStatus" runat="server" ErrorMessage="* Required"
Display="Dynamic" ControlToValidate="ddlTaskStatus" SetFocusOnError="True" ValidationGroup="EditGridSave"
Text="Required" ></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Rows="3" Columns="30" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是行数据绑定和选定索引更改事件的代码。
protected void gvEditFields_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SetEditFieldsGridRecords(e.Row);
//DropDownList ddlProjectList = (DropDownList)e.Row.FindControl("ddlProjectList");
}
}
protected void ddlProjectList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlProjectList = (DropDownList)sender;
// GridViewRow Row = (GridViewRow)ddlProjectList.NamingContainer;
GridViewRow selectedRow = (GridViewRow)ddlProjectList.Parent.Parent;
int i = selectedRow.RowIndex;
DropDownList ddlTaskList = (DropDownList)selectedRow.Cells[1].FindControl("ddlTaskList");
DropDownList ddlTaskStatus = (DropDownList)selectedRow.Cells[2].FindControl("ddlTaskStatus");
BindTasks(clsCheckDBNull.ToStr(ddlProjectList.SelectedValue), -1, ddlTaskList);
BindStatus(clsCheckDBNull.ToInt(ddlTaskList.SelectedValue), "", ddlTaskStatus);
}
下面是使用的方法的代码
private void BindStatus(int intTaskId, string TaskStatus, DropDownList drpStatus)
{
DataTable dt = clsProjectTaskStatuses.SelectAll(BTMSession.AccountID);
DataView dv = new DataView(dt);
drpStatus.DataTextField = "str_PROJECT_TASK_STATUS_NAME";
drpStatus.DataValueField = "int_PROJECT_TASK_STATUS_ID";
drpStatus.DataSource = dv;
drpStatus.DataBind();
SiteUtility.BindTooltip(drpStatus);
if (drpStatus.Items.Count >= 2 && TaskStatus.Trim() != "")
{
if (drpStatus.Items.FindByText(TaskStatus.ToString()) != null)
drpStatus.SelectedIndex = drpStatus.Items.IndexOf(drpStatus.Items.FindByText(TaskStatus.ToString()));
}
}
private void BindTasks(string ProjectNo, int intTaskid, DropDownList drpTasks)
{
if (drpTasks.Items.Count >= 1)
drpTasks.Items.Clear();
DataSet ds = new DataSet();
clsImportFileTypeDAL objImportFileTypeDAL = new clsImportFileTypeDAL();
ds = objImportFileTypeDAL.SelectTaskListByProjectNoUserIDForTimeImport(ProjectNo, clsCheckDBNull.ToInt(BTMSession.UserId));
CommonHelper.DisposeOf(objImportFileTypeDAL);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
drpTasks.DataTextField = "str_TASK_SUMMERY";
drpTasks.DataValueField = "int_task_id";
drpTasks.DataSource = ds;
drpTasks.DataBind();
}
drpTasks.Items.Insert(0, new ListItem("Select Task", "-1"));
SiteUtility.BindTooltip(drpTasks);
drpTasks.SelectedValue = "-1";
if (drpTasks.Items.Count >= 2)
{
if (drpTasks.Items.FindByValue(intTaskid.ToString()) != null)
drpTasks.SelectedIndex = drpTasks.Items.IndexOf(drpTasks.Items.FindByValue(intTaskid.ToString()));
}
}
私人无效 SetEditFieldsGridRecords(GridViewRow 行) { DataRowView drv = (DataRowView)row.DataItem;
DropDownList ddlProjectList = (DropDownList)row.FindControl("ddlProjectList");
BindProjects(clsCheckDBNull.ToStr(drv["str_PROJECT_NO"]), ddlProjectList);
DropDownList ddlTaskList = (DropDownList)row.FindControl("ddlTaskList");
BindTasks(ddlProjectList.SelectedValue, clsCheckDBNull.ToInt(drv["int_TASK_NO"]), ddlTaskList);
DropDownList ddlTaskStatus = (DropDownList)row.FindControl("ddlTaskStatus");
BindStatus(clsCheckDBNull.ToInt(ddlTaskList.SelectedValue), clsCheckDBNull.ToStr(drv["TaskStatus"]), ddlTaskStatus);
BTMWeb.Calender.Calender txtWorkDate = (BTMWeb.Calender.Calender)row.FindControl("txtWorkDate");
DropDownList drpStartHours = (DropDownList)row.FindControl("drpStartHours");
DropDownList drpStartMinutes = (DropDownList)row.FindControl("drpStartMinutes");
DropDownList drpEndHours = (DropDownList)row.FindControl("drpEndHours");
DropDownList drpEndMinutes = (DropDownList)row.FindControl("drpEndMinutes");
TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
int StartHours = clsCheckDBNull.ToDate(drv["StartTime"]).Hour;
int StartMinutes = clsCheckDBNull.ToDate(drv["StartTime"]).Minute;
int EndHours = clsCheckDBNull.ToDate(drv["EndTime"]).Hour;
int EndMinutes = clsCheckDBNull.ToDate(drv["EndTime"]).Minute;
txtWorkDate.DateValue = clsCheckDBNull.ToDate(drv["dt_WORKDATE"]);
drpStartHours.SelectedValue = StartHours.ToString();
drpStartMinutes.SelectedValue = StartMinutes.ToString();
drpEndHours.SelectedValue = EndHours.ToString();
drpEndMinutes.SelectedValue = EndMinutes.ToString();
txtDescription.Text = clsCheckDBNull.ToStr(drv["str_Description"]);
}
例如。如果我有行并且如果我更改第一行中的下拉值,则所选索引更改会为两行触发。