gpt4 book ai didi

asp.net - 如何在 GridView 内添加级联下拉列表以进行编辑?

转载 作者:行者123 更新时间:2023-12-04 07:08:25 24 4
gpt4 key购买 nike

我有一个相当标准的 ASP.NET GridView,它显示 2 个具有父子关系的列。虽然数据库中存在A列和B列之间的关系,但GridView并没有实现它。

我想做以下事情:
当用户选择编辑该行时,2 个下拉菜单变为可见。 DropDownB 应根据 DropDownA 的值自动填充可用选项。当 DropDownA 更改时,需要更新 DropDownB 以反射(reflect)当前选项。

当前可用于网格的列:

 - ColumnAID
- ColumnADescription
- ColumnBID
- ColumnBDescription

我当然可以在网格之外完成同样的功能,甚至可以让选定的行事件显示一个模式弹出窗口,允许我相应地编辑字段,但希望将其保留在网格中。

最佳答案

下面的示例显示了 2 个下拉列表假日组和假日类型。
根据假日组的选择,将加载假日类型下拉菜单。
这里的问题是将自动回发设置为“true”并定义第一个下拉列表的 selectindex 更改事件 OnSelectedIndexChanged="CboHolidayGroup_SelectedIndexChanged"将“(”和“)”分别替换为“<”和“>”。
以下代码可用于 GridView 内的级联下拉菜单:

设计页面

(asp:TemplateField HeaderText="(*) Holiday Group")
(ItemStyle CssClass="TEXTBOX_MEDIUM" /)
(EditItemTemplate)
(asp:DropDownList ID="CboHolidayGroup" CssClass="DROPDOWN_SMALL" runat="server"
DataTextField="holidaygroup" DataValueField="holiday_group_code_id" AutoPostBack="true"
OnSelectedIndexChanged="CboHolidayGroup_SelectedIndexChanged")
(/asp:DropDownList)
(asp:RangeValidator ID="RvHolidayGroup" runat="server" ControlToValidate="CboHolidayGroup"
Display="None" ErrorMessage="Please Select Holiday Group" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpUpdate")
(/asp:RangeValidator)
(/EditItemTemplate)
(ItemTemplate)
(asp:Label ID="LblHolidayGroup" CssClass="DROPDOWN_SMALL" runat="server" Text='<%# Eval("holidaygroup") %>')(/asp:Label)
(/ItemTemplate)
(FooterTemplate)
(asp:DropDownList ID="CboNewHolidayGroup" CssClass="DROPDOWN_SMALL" runat="server"
DataTextField="holidaygroup" DataValueField="holiday_group_code_id" Enabled="false"
AutoPostBack="true" OnSelectedIndexChanged="CboHolidayGroup_SelectedIndexChanged")
(/asp:DropDownList)
(asp:RangeValidator ID="RvNewHolidayGroup" runat="server" ControlToValidate="CboNewHolidayGroup"
Display="None" ErrorMessage="Please Select Holiday Group" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpInsert")
(/asp:RangeValidator)
(/FooterTemplate)
(/asp:TemplateField)
(asp:TemplateField HeaderText="(*) Holiday Type")
(ItemStyle CssClass="DROPDOWN_XLARGE" /)
(EditItemTemplate)
(asp:DropDownList ID="CboHolidayType" CssClass="DROPDOWN_XLARGE" runat="server"
DataTextField="holidaytype" DataValueField="holiday_type_code_id")
(/asp:DropDownList)
(asp:RangeValidator ID="RvHolidayType" runat="server" ControlToValidate="CboHolidayType"
Display="None" ErrorMessage="Please Select Holiday Type" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpUpdate")
(/asp:RangeValidator)
(/EditItemTemplate)
(ItemTemplate)
(asp:Label ID="LblHolidayType" CssClass="DROPDOWN_XLARGE" runat="server" Text='<%# Eval("holidaytype") %>')(/asp:Label)
(/ItemTemplate)
(FooterTemplate)
(asp:DropDownList ID="CboNewHolidayType" CssClass="DROPDOWN_XLARGE" runat="server"
DataTextField="holidaytype" DataValueField="holiday_type_code_id" Enabled="false")
(/asp:DropDownList)
(asp:RangeValidator ID="RvNewHolidayType" runat="server" ControlToValidate="CboNewHolidayType"
Display="None" ErrorMessage="Please Select Holiday Type" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpInsert")
(/asp:RangeValidator)
(/FooterTemplate)
(/asp:TemplateField)

代码背后和设计
<asp:TemplateField HeaderText="(*) Holiday Group">
<ItemStyle CssClass="TEXTBOX_MEDIUM" />
<EditItemTemplate>
<asp:DropDownList ID="CboHolidayGroup" CssClass="DROPDOWN_SMALL" runat="server"
DataTextField="holidaygroup" DataValueField="holiday_group_code_id" AutoPostBack="true"
OnSelectedIndexChanged="CboHolidayGroup_SelectedIndexChanged">
</asp:DropDownList>
<asp:RangeValidator ID="RvHolidayGroup" runat="server" ControlToValidate="CboHolidayGroup"
Display="None" ErrorMessage="Please Select Holiday Group" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpUpdate">
</asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblHolidayGroup" CssClass="DROPDOWN_SMALL" runat="server" Text='<%# Eval("holidaygroup") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="CboNewHolidayGroup" CssClass="DROPDOWN_SMALL" runat="server"
DataTextField="holidaygroup" DataValueField="holiday_group_code_id" Enabled="false"
AutoPostBack="true" OnSelectedIndexChanged="CboHolidayGroup_SelectedIndexChanged">
</asp:DropDownList>
<asp:RangeValidator ID="RvNewHolidayGroup" runat="server" ControlToValidate="CboNewHolidayGroup"
Display="None" ErrorMessage="Please Select Holiday Group" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpInsert">
</asp:RangeValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="(*) Holiday Type">
<ItemStyle CssClass="DROPDOWN_XLARGE" />
<EditItemTemplate>
<asp:DropDownList ID="CboHolidayType" CssClass="DROPDOWN_XLARGE" runat="server" DataTextField="holidaytype"
DataValueField="holiday_type_code_id">
</asp:DropDownList>
<asp:RangeValidator ID="RvHolidayType" runat="server" ControlToValidate="CboHolidayType"
Display="None" ErrorMessage="Please Select Holiday Type" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpUpdate">
</asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblHolidayType" CssClass="DROPDOWN_XLARGE" runat="server" Text='<%# Eval("holidaytype") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="CboNewHolidayType" CssClass="DROPDOWN_XLARGE" runat="server"
DataTextField="holidaytype" DataValueField="holiday_type_code_id" Enabled="false">
</asp:DropDownList>
<asp:RangeValidator ID="RvNewHolidayType" runat="server" ControlToValidate="CboNewHolidayType"
Display="None" ErrorMessage="Please Select Holiday Type" MaximumValue="1000"
MinimumValue="1" SetFocusOnError="true" Type="Integer" ValidationGroup="VgrpInsert">
</asp:RangeValidator>
</FooterTemplate>
</asp:TemplateField>

.
#Region "CboHolidayGroup_SelectedIndexChanged"
Protected Sub CboHolidayGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim sGroup As String = String.Empty
Dim intGroup As Integer = 0
Dim dtNewHolidayType As DataTable
Dim dtHolidayType As DataTable
Dim objAims As iSymbol = Factory.Factory.CreateSymbolObject()

If DirectCast(sender, DropDownList).SelectedIndex > 0 Then
If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("CboHolidayGroup")) Then
sGroup = DirectCast(sender, DropDownList).SelectedItem.Text
intGroup = DirectCast(sender, DropDownList).SelectedValue
If sGroup = "Bank" Then
If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("ChkBankHoliday")) Then
DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("ChkBankHoliday"), CheckBox).Checked = True
End If

If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday")) Then
DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday"), CheckBox).Checked = True
End If
Else
If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("ChkBankHoliday")) Then
DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("ChkBankHoliday"), CheckBox).Checked = False
End If

If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday")) Then
DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday"), CheckBox).Checked = False
End If
End If

If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("CboHolidayType")) Then
Dim CboHolidayType As DropDownList = DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("CboHolidayType"), DropDownList)
dtHolidayType = objSymbol.GetSelectedHolTypes(intGroup)
If (dtHolidayType.Rows.Count > 0) Then
CboHolidayType.DataSource = dtHolidayType
CboHolidayType.DataTextField = "holidaytype"
CboHolidayType.DataValueField = "holiday_type_code_id"
CboHolidayType.DataBind()
AddFirstItem(CboHolidayType, _CON_COMBO_FIRST_SELECT)
End If
End If
End If

If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("CboNewHolidayGroup")) Then
sGroup = DirectCast(sender, DropDownList).SelectedItem.Text
intGroup = DirectCast(sender, DropDownList).SelectedValue
If sGroup = "Bank" Then
If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday")) Then
DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday"), CheckBox).Checked = True
End If
Else
If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday")) Then
DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("ChkNewBankHoliday"), CheckBox).Checked = False
End If
End If

If Not IsNothing(DirectCast(sender, DropDownList).Parent.FindControl("CboNewHolidayType")) Then
Dim CboNewHolidayType As DropDownList = DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("CboNewHolidayType"), DropDownList)
dtHolidayType = objSymbol.GetSelectedHolTypes(intGroup)
If (dtHolidayType.Rows.Count > 0) Then
CboNewHolidayType.DataSource = dtHolidayType
CboNewHolidayType.DataTextField = "holidaytype"
CboNewHolidayType.DataValueField = "holiday_type_code_id"
CboNewHolidayType.DataBind()
AddFirstItem(CboNewHolidayType, _CON_COMBO_FIRST_SELECT)
End If
End If
End If
End If
End Sub
#End Region

关于asp.net - 如何在 GridView 内添加级联下拉列表以进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/754823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com