选择GridView中Radiolist的特定Radios时显示TextBox

时间:2014-09-07 09:53:25

标签: asp.net gridview

我在GridView模板字段中有一个RadioList和一个TextBox。我想只在选择一些特定的单选按钮时显示TextBox,并隐藏休息。例如如果列表中有五个无线电,如果用户选择无线电1,无线电4和无线电5,则只应显示文本框,隐藏其他选项。

这是设计师脚本:

<asp:GridView ID="gvJobTasks" AutoGenerateColumns="false" runat="server" CellPadding="3"
       PageSize="20" GridLines="None" BorderStyle="None" Width="80%" 
       CssClass="gview grayTable1" AllowPaging="true" OnRowDataBound="gvJobTasks_RowDataBound">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle CssClass="pager" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle CssClass="hview" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="whitesmoke" ForeColor="#284775" />            
    <Columns>
        <asp:BoundField DataField="JobTaskName" ItemStyle-Width="60%" HeaderText="Job Task">                
            <ItemStyle Width="60%"></ItemStyle>
        </asp:BoundField>

        <asp:TemplateField HeaderText="Ratings" ItemStyle-Width="40%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:RadioButtonList ID="radioJobTaskRatings" Width="100%" RepeatDirection="Horizontal" runat="server">
                    <asp:ListItem Text="1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="2" Value="2"></asp:ListItem>
                    <asp:ListItem Text="3" Value="3"></asp:ListItem>
                    <asp:ListItem Text="4" Value="4"></asp:ListItem>
                    <asp:ListItem Text="5" Value="5"></asp:ListItem>
                    <asp:ListItem Text="NA" Value="0"></asp:ListItem>
                </asp:RadioButtonList>  

                <asp:TextBox ID="txtJobTasksJustification" Visible="false"  Text='<%#Eval("Justification") %>' TextMode="MultiLine" runat="server"></asp:TextBox>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>

1 个答案:

答案 0 :(得分:2)

尝试这样:

<asp:GridView ID="gvJobTasks" AutoGenerateColumns="false" runat="server" CellPadding="3"
       PageSize="20" GridLines="None" BorderStyle="None" Width="80%" 
       CssClass="gview grayTable1" AllowPaging="true" OnRowDataBound="gvJobTasks_RowDataBound">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle CssClass="pager" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle CssClass="hview" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="whitesmoke" ForeColor="#284775" />            
    <Columns>
        <asp:BoundField DataField="JobTaskName" ItemStyle-Width="60%" HeaderText="Job Task">                
            <ItemStyle Width="60%"></ItemStyle>
        </asp:BoundField>

        <asp:TemplateField HeaderText="Ratings" ItemStyle-Width="40%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:RadioButtonList ID="radioJobTaskRatings" AutoPostBack="true" OnSelectedIndexChanged="radioJobTaskRatings_OnSelectedIndexChanged"
                 Width="100%" RepeatDirection="Horizontal" runat="server">
                    <asp:ListItem Text="1"  Value="1"></asp:ListItem>
                    <asp:ListItem Text="2" Value="2"></asp:ListItem>
                    <asp:ListItem Text="3" Value="3"></asp:ListItem>
                    <asp:ListItem Text="4" Value="4"></asp:ListItem>
                    <asp:ListItem Text="5" Value="5"></asp:ListItem>
                    <asp:ListItem Text="NA" Value="0"></asp:ListItem>
                </asp:RadioButtonList>  

                <asp:TextBox ID="txtJobTasksJustification" Visible="false"  Text='<%#Eval("Justification") %>' TextMode="MultiLine" runat="server"></asp:TextBox>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>

代码:

protected void gvJobTasks_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void radioJobTaskRatings_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = (GridViewRow)(((RadioButtonList)sender).Parent).Parent;
        RadioButtonList rb=(RadioButtonList)row.FindControl("radioJobTaskRatings");
        TextBox txtJobTasksJustification=(TextBox)row.FindControl("txtJobTasksJustification");
        if (rb.SelectedValue == "1" || rb.SelectedValue == "4" || rb.SelectedValue == "5")
        {
            txtJobTasksJustification.Visible = true;
        }
        else
        {
            txtJobTasksJustification.Visible = false;
        }


    }