gpt4 book ai didi

asp.net - 将单选按钮列表绑定(bind)到 gridview 中的列

转载 作者:行者123 更新时间:2023-12-02 17:06:04 25 4
gpt4 key购买 nike

我在 GridView 中有单选按钮列表,需要绑定(bind)到列。如果列中的值为 0,则选择第一个单选按钮,如果为 1,则选择另一个单选按钮。

这是代码,其中一些代码因为不需要而被部分删除

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<>"
SelectCommand="" SelectCommandType="StoredProcedure" UpdateCommand="">
<SelectParameters></SelectParameters>
<UpdateParameters></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvBlockDetail" runat="server" AutoGenerateColumns="False"
DataKeyNames="curriculumyear,electiveid,blockid" DataSourceID="SqlDataSource1"
HorizontalAlign="Left" CellPadding="1" CssClass="news" GridLines="None"
BorderColor="#ebe9e2" BorderStyle="Solid" BorderWidth="1" >
<AlternatingRowStyle BackColor="#ebe9e2" />
<HeaderStyle BackColor="#660000" ForeColor="White" Font-Size="Small" />
<RowStyle Font-Size="9pt" Wrap="false" ForeColor="#660000" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="Add/Remove">
<HeaderStyle Width="15%" />
<ItemStyle Wrap="false" Width="80px" />
<ItemTemplate>
<asp:RadioButtonList ID="rblAddRemove" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Add" Value="0"></asp:ListItem>
<asp:ListItem Text="Remove" Value="1"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Block">
<HeaderStyle Width="15%" />
<ItemStyle Wrap="false" Width="50px" />
<ItemTemplate>
<asp:Label ID="lblBlock" runat="server" Text='<%# Bind("Block") %>'></asp:Label>
<asp:Label ID="lblSection" runat="server" Text='<%# Bind("Section") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="daterange" HeaderText="Dates" ReadOnly="True" SortExpression="daterange" />
<asp:BoundField DataField="credithours" HeaderText="Credit Hrs"
SortExpression="credithours" HeaderStyle-Width="10%" ItemStyle-Width="10%" />
<asp:TemplateField HeaderText="Students&lt;br&gt;Per Block" HeaderStyle-Width="15%" SortExpression="studentsperblock">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("studentsperblock") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtStudentsPerBlock" runat="server" MaxLength="3" Width="40px" Text='<%# Bind("studentsperblock") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="enrolled" HeaderText="Enrolled" ReadOnly="True"
SortExpression="enrolled" ItemStyle-Width="200px" />
<asp:BoundField DataField="blockid" HeaderText="blockid" ReadOnly="True"
SortExpression="blockid" Visible="false" />
</Columns>
</asp:GridView>

隐藏代码:

Protected Sub gvBlockDetail_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBlockDetail.RowDataBound

End Sub

最佳答案

您可以尝试内联绑定(bind):

<asp:RadioButtonList ID="rblAddRemove" runat="server" RepeatDirection="Horizontal" SelectedValue='<%# Bind("YOURCOLUMN") %>'>
<asp:ListItem Text="Add" Value="0"></asp:ListItem>
<asp:ListItem Text="Remove" Value="1"></asp:ListItem>
</asp:RadioButtonList>

其中 yourcolumn 是您描述的 int 列。

或者通过 RowDataBound 事件。 (伪代码,属性可能有不同的名称,我使用的是 C#)

Protected Sub gvBlockDetail_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBlockDetail.RowDataBound
if(e.Row.RowType == RowType.DataRow)
{
RadioButtonList rbl = e.Row.FindControl("rblAddRemove") as RadioButtonList;
if(rbl != null)
{
rbl.SelectedValue = ((YOURDATAITEM)(e.Row.DataItem).YourProperty.ToString();
}
}
End Sub

编辑:我发现您没有使用自定义类。您需要使用 YOURDATAITEM 调整该行。使用快速观察来了解如何转换对象以获得所需的属性。

关于asp.net - 将单选按钮列表绑定(bind)到 gridview 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3773911/

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