困扰很久的下拉列表框问题
HTML代码:
<asp:datagrid id= "DataGrid1 " runat= "server " Width= "100% " BorderWidth= "1px " BorderColor= "LightBlue "
AutoGenerateColumns= "False " AllowPaging= "True " GridLines= "Horizontal ">
<SelectedItemStyle Wrap= "False "> </SelectedItemStyle>
<EditItemStyle Wrap= "False "> </EditItemStyle>
<AlternatingItemStyle Wrap= "False " BackColor= "#EBEBEB "> </AlternatingItemStyle>
<ItemStyle Wrap= "False "> </ItemStyle>
<Columns>
<asp:BoundColumn DataField= "ID " HeaderText= "ID ">
<HeaderStyle Width= "80px "> </HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText= "操作 ">
<ItemTemplate>
<FONT face= "宋体 ">
<asp:DropDownList id= "DropDownList1 " runat= "server " OnSelectedIndexChanged= "DropDownList1_SelectedIndexChanged "
AutoPostBack= "True ">
<asp:ListItem Value= "操作 "> 操作 </asp:ListItem>
<asp:ListItem Value= "0 "> 详细信息 </asp:ListItem>
<asp:ListItem Value= "1 "> 销售报告 </asp:ListItem>
<asp:ListItem Value= "2 "> 合同信息 </asp:ListItem>
<asp:ListItem Value= "3 "> 填写销售报告 </asp:ListItem>
</asp:DropDownList> </FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
事件代码:
Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dg As DataGridItem
For Each dg In DataGrid1.Items
Dim mydd As DropDownList = dg.FindControl( "DropDownList1 ")
Select Case mydd.SelectedValue
Case 0
Response.Redirect( "inf.aspx?id= " + ID)
Case 1
Response.Redirect( "inf1.aspx?id= " + ID)
Case 2
Response.Redirect( "inf2.aspx?id= " + ID)
End Select
Next
End Sub
一个datagrid中有一个下拉列表框,在选择不同的信息时转向不同的页面,并传递出datagrid中ID列的相应值.这个如何实现?急急急!!!
[解决办法]
ID=dg.Cells[0].ToString();
0是你datagrid中ID列的index值
[解决办法]
可以用委托实现,在datagrid的DataGrid1_ItemCreated事件中加如下代码:
大致写了一下:
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList drop = (DropDownList)((DataGrid)sender).Items[0].FindControl( "DropDownList1 ");
drop.SelectedIndexChanged+=new EventHandler(drop_SelectedIndexChanged);
}
之后在Sub DropDownList1_SelectedIndexChanged事件中加你要的代码。
[解决办法]
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item)
{
DropDownList dl=e.Item.FindControl( "DropDownList1 ") as DropDownList;
dl.Attributes[ "onchange "]= "var a=document.getElementById( ' "+dl.ClientID+ " ');var b=a.selectedIndex;if(b== '0 '){window.location.href= 'addcategory.aspx?id= "+DataList1.DataKeys[e.Item.ItemIndex].ToString()+ " '} ";
}
}