我有一个数据列表,我有两个下拉列表:第一个选择一个表,第二个选择列。到目前为止一切都很好。
当我转到datalist中的下一个项目并在Dropdownlist“ddlTable”上进行选择时,问题就出现了。执行此操作后,先前的第二个下拉列表将其值更改为第一个可能的项。你能帮我吗?我怎么能阻止它这样做?
以下是代码:
protected void Page_Load(object sender, EventArgs e)
{
try
{
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (!scriptManager.IsInAsyncPostBack && !IsPostBack)
{
DataList_Load(DataListItems);
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void DataList_Load(int itens)
{
try
{
List<int> myList = new List<int>();
for (int i = 0; i < itens; i++)
myList.Add(i);
dlQuery.DataSource = myList;
dlQuery.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
protected void dlQuery_ItemDataBound(object sender, DataListItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var ddl = e.Item.FindControl("ddlTable") as DropDownList;
ddlTable_Load(ddl);
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void ddlTable_Load(DropDownList ddl)
{
try
{
Connection cn = new Connection();
SqlConnection SQLConn = cn.SqlLocalConn;
SqlCommand SQLcmd = new SqlCommand("stp_Portal_GlobalMgmtQueryItens", SQLConn);
SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
ddl.DataSource = cn.ExecuteSqlCmd(SQLConn, SQLcmd);
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.DataBind();
ddl.Items.Insert(0, "Select one");
}
catch (Exception ex)
{
throw ex;
}
}
protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList ddl1 = sender as DropDownList;
if (ddl1.SelectedIndex > 0)
{
//string aux = ddl1.UniqueID;
DropDownList ddl2 = (DropDownList)ddl1.Parent.FindControl("ddlColumn");
//string aux2 = ddl2.UniqueID;
ddlColumn_Load(ddl2, ddl1.SelectedValue);
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void ddlColumn_Load(DropDownList ddl, string val)
{
try
{
Connection cn = new Connection();
SqlConnection SQLConn = cn.SqlLocalConn;
SqlCommand SQLcmd = new SqlCommand("stp_Portal_GlobalMgmtQueryItemColumns", SQLConn);
SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
SQLcmd.Parameters.Add("@intTableId", SqlDbType.Int).Value = val;
ddl.DataSource = cn.ExecuteSqlCmd(SQLConn, SQLcmd);
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.DataBind();
ddl.Items.Insert(0, "Select one");
}
catch (Exception ex)
{
throw ex;
}
}
protected void ddlColumn_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList ddl = sender as DropDownList;
if (ddl.SelectedIndex > 0)
{
//just for test
}
}
catch (Exception ex)
{
throw ex;
}
}
aspx
<asp:DataList ID="dlQuery" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
EnableViewState="true" OnItemDataBound="dlQuery_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td align="right" style="height: 20px">
<asp:Label ID="lblTable" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
Text="Table" ForeColor="#005780"></asp:Label>
</td>
<td align="left">
<asp:DropDownList ID="ddlTable" runat="server" AutoPostBack="True" Font-Names="Verdana"
Font-Size="7pt" OnSelectedIndexChanged="ddlTable_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right" style="height: 20px">
<asp:Label ID="lblColumn" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
Text="Column" ForeColor="#005780"></asp:Label>
</td>
<td align="left">
<asp:DropDownList ID="ddlColumn" runat="server" Font-Names="Verdana" Font-Size="7pt"
AutoPostBack="True" OnSelectedIndexChanged="ddlColumn_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right" style="height: 20px">
<asp:Label ID="lblToShow" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
Text="To Show" ForeColor="#005780"></asp:Label>
</td>
<td align="left">
<asp:CheckBox ID="cbToShow" runat="server" Font-Names="Verdana" Font-Size="7pt" />
</td>
<tr>
<td align="right" style="height: 20px">
<asp:Label ID="lblCrit" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
ForeColor="#005780" Text="Criteria"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtCrit" runat="server" Font-Names="Verdana" Font-Size="7pt" Width="95%"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" style="height: 20px">
<asp:Label ID="lblOr" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
ForeColor="#005780" Text="Or"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtOr" runat="server" Font-Names="Verdana" Font-Size="7pt" Width="95%"></asp:TextBox>
</td>
</tr>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<table>
<tr>
<td align="right" style="width: 20px">
</td>
</tr>
<tr>
<td align="right" style="width: 20px">
</td>
</tr>
<tr>
<td align="right" style="width: 20px">
</td>
</tr>
<tr>
<td align="right" style="width: 20px">
</td>
</tr>
<tr>
<td align="right" style="width: 20px">
</td>
</tr>
</table>
</SeparatorTemplate>
</asp:DataList>