如何设置datagrid中的DropDownList显示的是绑定记录的当前数据?

gqxm 2003-08-25 11:11:16
datagrid中包含一个EditItemTemplate是DropDownList的列,现我想在点击编辑时,DropDownList显示的是绑定记录的当前数据,而不是DropDownList的第一条数据,请问语句应如何写?

datagrid代码:
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" CssClass="f9black" BackColor="White" ForeColor="Black" Width="200px" Height="256px" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Vertical" AutoGenerateColumns="False" DataKeyField="id">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="确定" CancelText="取消" EditText="编辑">
<HeaderStyle Width="20px"></HeaderStyle>
<ItemStyle Font-Size="Small"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="id" HeaderText="序号"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="排序序号">
<HeaderStyle Width="138px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label8 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.序号") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>

另外,如果我想把这个DropDownList1禁用,为什么以下语句不可以实现,DropDownList0是nothing。

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = CInt(e.Item.ItemIndex)
Dim DropDownList0 As DropDownList = CType(e.Item.FindControl("DropDownList1"), DropDownList)
DropDownList0.Enabled = False
bindgrid()
End Sub
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruyedian 2003-08-25
  • 打赏
  • 举报
回复
放在绑定事件里面
private void DataGrid1_ItemDataBound(sender as object, e as DataGridItemEventArgs)
if e.item.itemindex<>-1 then
Dim DropDownList0 As DropDownList = CType(e.Item.FindControl("DropDownList1"), DropDownList)
DropDownList0.Enabled = False
end if
end sub
gqxm 2003-08-25
  • 打赏
  • 举报
回复
chnking(kent):
等我试试。

to acewang(**^o^**):

Dim DropDownList0 As DropDownList = CType(e.Item.FindControl("DropDownList1"), DropDownList)
DropDownList0.Enabled = False

这段代码应放在哪里?才可以实现我的目的?
chnking 2003-08-25
  • 打赏
  • 举报
回复
上面的代码有点错误,下面正确:

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DropDownList1");
string str = ((DataRowView)e.Item.DataItem)["id"].ToString();
myDropDownList.Items.FindByText(str).Selected = true;
}
}
acewang 2003-08-25
  • 打赏
  • 举报
回复
点击编辑的时候dropdownlist1还没生成呢,当然是nothing
chnking 2003-08-25
  • 打赏
  • 举报
回复
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DropDownList1");
string str = ((DataRowView)e.Item.DataItem)["id"].ToString();
myDropDownList.SelectedItem = myDropDownList.Items.FindByText(str);
}
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧