我在网格视图外面有一个Dropdown1包含三个选项选择,A,B。
内部gridview两个下拉列表Dropdown2,Dropdown3,其中Dropdown2包含三个选项,例如Select,C,D。
基于Dropdown1& Dropdown2,我在Dropdown2中的javascript中使用onchange事件填充Dropdown3。
第一行的一切正常,但当我添加新行时,Dropdown3值消失为默认“选择”
Javascript功能
function DataformatFetch(Selection) {
var status = Selection.options[Selection.selectedIndex].value;
var row = Selection.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
if (document.getElementById("<%=DropDown1.ClientID%>").value == "A") {
var Dataformatlist = row.cells[7].getElementsByTagName('SELECT')[0];
switch (status) {
case "Select":
Dataformatlist.options.length = 0;
Dataformatlist.options[0] = new Option("Select","Select");
break;
case "C":
Dataformatlist.options.length = 0;
Dataformatlist.options[0] = new Option("Select","Select");
Dataformatlist.options[1] = new Option("1","1");
Dataformatlist.options[2] = new Option("2","2");
Dataformatlist.options[3] = new Option("3","3");
break;
case "D":
Dataformatlist.options.length = 0;
Dataformatlist.options[0] = new Option("Select","Select");
Dataformatlist.options[1] = new Option("11","11");
Dataformatlist.options[2] = new Option("22","22");
Dataformatlist.options[3] = new Option("33","33");
break;
}
}
else if (document.getElementById("<%=DropDown1.ClientID%>").value == "B") {
var Dataformatlist = row.cells[7].getElementsByTagName('SELECT')[0];
switch (status) {
case "Select":
Dataformatlist.options.length = 0;
Dataformatlist.options[0] = new Option("Select");
break;
case "C":
Dataformatlist.options.length = 0;
Dataformatlist.options[0] = new Option("Select","Select");
Dataformatlist.options[1] = new Option("111","111");
Dataformatlist.options[2] = new Option("222","222");
Dataformatlist.options[3] = new Option("333","333");
break;
case "D":
Dataformatlist.options.length = 0;
Dataformatlist.options[0] = new Option("Select","Select");
Dataformatlist.options[1] = new Option("1111","1111");
Dataformatlist.options[2] = new Option("2222","2222");
Dataformatlist.options[3] = new Option("3333","3333");
break;
}
}
}
ASPX代码
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Object Type">
<ItemTemplate>
<asp:DropDownList ID="Dropdownlist2" runat="server" OnChange="DataformatFetch(this);">
<asp:ListItem Value="Select">Select</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
C#代码
private void AddNewRowToGrid()
{
try
{
if (ViewState["CurrentTable"] != null)
{
System.Data.DataTable dtCurrentTable = (System.Data.DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
//drCurrentRow["REQUESTNO"] = dtCurrentTable.Rows.Count + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
TextBox box2 = (TextBox)DataExchangeGridView.Rows[i].Cells[0].FindControl("PTNTextBox");
TextBox box3 = (TextBox)DataExchangeGridView.Rows[i].Cells[1].FindControl("PTDTextBox");
TextBox box4 = (TextBox)DataExchangeGridView.Rows[i].Cells[2].FindControl("PTRTextBox");
DropDownList box5 = (DropDownList)DataExchangeGridView.Rows[i].Cells[3].FindControl("PIPRDropdownlist");
DropDownList box6 = (DropDownList)DataExchangeGridView.Rows[i].Cells[4].FindControl("SupplierNameDroplist");
DropDownList box7 = (DropDownList)DataExchangeGridView.Rows[i].Cells[5].FindControl("ObjectTypeDropdownlist");
DropDownList box8 = (DropDownList)DataExchangeGridView.Rows[i].Cells[6].FindControl("DataformatDropdownlist");
dtCurrentTable.Rows[i]["PARTNUMBER"] = box2.Text;
dtCurrentTable.Rows[i]["PARTDESCRIPTION"] = box3.Text;
dtCurrentTable.Rows[i]["PARTREVISION"] = box4.Text;
dtCurrentTable.Rows[i]["PARTIPR"] = box5.Text;
dtCurrentTable.Rows[i]["SUPPLIERNAME"] = box6.Text;
dtCurrentTable.Rows[i]["OBJECTTYPE"] = box7.Text;
dtCurrentTable.Rows[i]["DATAFORMAT"] = box8.Text;
}
DataExchangeGridView.DataSource = dtCurrentTable;
DataExchangeGridView.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
catch (Exception rt)
{
rt.ToString();
}
}
private void SetPreviousData()
{
try
{
int rowIndex = 0;
if (this.DataExTypeDropDownList.SelectedItem.Value == "Import")
{
if (ViewState["CurrentTable"] != null)
{
System.Data.DataTable dt = (System.Data.DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Web.UI.WebControls.TextBox box2 = (System.Web.UI.WebControls.TextBox)DataExchangeGridView.Rows[rowIndex].Cells[0].FindControl("PTNTextBox");
System.Web.UI.WebControls.TextBox box3 = (System.Web.UI.WebControls.TextBox)DataExchangeGridView.Rows[rowIndex].Cells[1].FindControl("PTDTextBox");
System.Web.UI.WebControls.TextBox box4 = (System.Web.UI.WebControls.TextBox)DataExchangeGridView.Rows[rowIndex].Cells[2].FindControl("PTRTextBox");
box2.Enabled = box3.Enabled = box4.Enabled = false;
box2.Text = string.Empty;
box3.Text = string.Empty;
box4.Text = string.Empty;
System.Web.UI.WebControls.DropDownList box5 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[3].FindControl("PIPRDropdownlist");
System.Web.UI.WebControls.DropDownList box6 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[4].FindControl("SupplierNameDroplist");
System.Web.UI.WebControls.DropDownList box7 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[5].FindControl("ObjectTypeDropdownlist");
System.Web.UI.WebControls.DropDownList box8 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[6].FindControl("DataformatDropdownlist");
//FillDropDownList(box8);
if (i < dt.Rows.Count - 1)
{
box2.Text = dt.Rows[i]["PARTNUMBER"].ToString();
box3.Text = dt.Rows[i]["PARTDESCRIPTION"].ToString();
box4.Text = dt.Rows[i]["PARTREVISION"].ToString();
box5.Text = dt.Rows[i]["PARTIPR"].ToString();
box6.Text = dt.Rows[i]["SUPPLIERNAME"].ToString();
box7.Text = dt.Rows[i]["OBJECTTYPE"].ToString();
//box8.Text = dt.Rows[i]["DATAFORMAT"].ToString();
string XE = box8.Items.FindByText(dt.Rows[i]["DATAFORMAT"].ToString()).Value;
box8.Text = XE;
}
rowIndex++;
}
}
}
}
else if (this.DataExTypeDropDownList.SelectedItem.Value == "Export")
{
if (ViewState["CurrentTable"] != null)
{
System.Data.DataTable dt = (System.Data.DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Web.UI.WebControls.TextBox box2 = (System.Web.UI.WebControls.TextBox)DataExchangeGridView.Rows[rowIndex].Cells[0].FindControl("PTNTextBox");
System.Web.UI.WebControls.TextBox box3 = (System.Web.UI.WebControls.TextBox)DataExchangeGridView.Rows[rowIndex].Cells[1].FindControl("PTDTextBox");
System.Web.UI.WebControls.TextBox box4 = (System.Web.UI.WebControls.TextBox)DataExchangeGridView.Rows[rowIndex].Cells[2].FindControl("PTRTextBox");
System.Web.UI.WebControls.DropDownList box5 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[3].FindControl("PIPRDropdownlist");
System.Web.UI.WebControls.DropDownList box6 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[4].FindControl("SupplierNameDroplist");
System.Web.UI.WebControls.DropDownList box7 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[5].FindControl("ObjectTypeDropdownlist");
System.Web.UI.WebControls.DropDownList box8 = (System.Web.UI.WebControls.DropDownList)DataExchangeGridView.Rows[rowIndex].Cells[6].FindControl("DataformatDropdownlist");
if (i < dt.Rows.Count - 1)
{
box2.Text = dt.Rows[i]["PARTNUMBER"].ToString();
box3.Text = dt.Rows[i]["PARTDESCRIPTION"].ToString();
box4.Text = dt.Rows[i]["PARTREVISION"].ToString();
box5.Text = dt.Rows[i]["PARTIPR"].ToString();
box6.Text = dt.Rows[i]["SUPPLIERNAME"].ToString();
box7.Text = dt.Rows[i]["OBJECTTYPE"].ToString();
box8.Text = dt.Rows[i]["DATAFORMAT"].ToString();
}
rowIndex++;
}
}
}
}
}
catch (Exception er)
{
er.ToString();
}
}
答案 0 :(得分:0)
首先,数据格式由于生成客户端而未被传回,因此我们可以添加隐藏字段来保存该值。
<asp:HiddenField ID="selectedValue" runat="server" Value='<%# Eval("Dataformat") %>' />
我们如何填充该字段,将OnChange添加到数据格式下拉列表中。
<asp:DropDownList ID="Dropdownlist7" runat="server" OnChange="DataformatSelect(this);">
function DataformatSelect(Selection) {
var selectedValue = Selection.options[Selection.selectedIndex].value;
Selection.parentNode.getElementsByTagName('INPUT')[0].value = selectedValue;
}
在服务器端,我们必须在回发时(以及初始加载时)返回所有数据。
private DataTable GetEmptyTable()
{
var table = new DataTable();
table.Columns.Add("ObjectType", typeof(string));
table.Columns.Add("Dataformat", typeof(string));
return table;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var table = GetEmptyTable();
table.Rows.Add("Select", "Select");
GridView1.DataSource = table;
GridView1.DataBind();
}
}
protected void btnAddRow_Click(object sender, EventArgs e)
{
var table = GetEmptyTable();
DataRow dr;
foreach (GridViewRow gvr in GridView1.Rows)
{
dr = table.NewRow();
DropDownList ddlObjectType = gvr.FindControl("Dropdownlist2") as DropDownList;
HiddenField hdnDataFormat = gvr.FindControl("selectedValue") as HiddenField;
dr[0] = ddlObjectType.SelectedValue;
dr[1] = hdnDataFormat.Value;
table.Rows.Add(dr);
}
table.Rows.Add("Select", "Select");
GridView1.DataSource = table;
GridView1.DataBind();
}
确保使用Bind填充对象类型。
<asp:DropDownList ID="Dropdownlist2" runat="server" OnChange="DataformatFetch(this);" SelectedValue='<%# Bind("ObjectType") %>'>
最后,在页面加载时,我们需要同步客户端生成的下拉列表。
<body onload="pageLoad();">
function pageLoad() {
[].forEach.call(document.getElementsByTagName('tr'), function (row, index) {
if (index == 0) {
return;
}
DataformatFetch(row.cells[0].getElementsByTagName('select')[0]);
var selectedValue = row.getElementsByTagName('input')[0].value;
row.cells[1].getElementsByTagName('select')[0].value = selectedValue;
});
}
希望能填补你已经存在的任何缺失。