在gridview下拉列表中选择特定值时禁用文本框

时间:2014-08-06 13:06:26

标签: c# asp.net

我有一个gridview,每行都有下拉列表,包含VALID(1),INVALID(0)

我想如果用户从下拉菜单中选择无效,那么它应该禁用表单中给出的文本框txtComplainant。

我试过把TxtboxComplainant.Visible = false;在SelectedIndexChange事件但不起作用。

代码:

protected void ddlIsComplaint_SelectedIndexChanged(object sender, EventArgs e) 
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        int RowIndex = Convert.ToInt32(row.RowIndex);
        //lblSelectionMessage.InnerText = RowIndex.ToString();

        foreach(GridViewRow rw in GridViewSmsComplaints.Rows)
        {
            if(rw.RowIndex != RowIndex)
            {
                rw.Enabled = false;
            }
        }
            if (ddl.SelectedValue == "0") 
            {
                txtComplainant.Visible = false;
            }
     }

.aspx的:

<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="5" runat="server" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333"  Width="650px" OnRowDataBound="GridViewSmsComplaints_RowDataBound" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
               <Columns>
                    <asp:BoundField HeaderText="Sms ID" DataField="ID" /> 
                    <asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />  
                    <%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" />  --%>
                    <asp:BoundField HeaderText="Incoming No" DataField="FromMobileNo" /> 
                    <asp:BoundField HeaderText="Complaint" DataField="Message" >   
                     <ItemStyle Wrap="True" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Is Valid?">
                     <ItemTemplate>
                       <%--<asp:CheckBox ID="ckboxIsComplaint"  runat="server" />--%>
                       <asp:DropDownList ID="ddlIsComplaint" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlIsComplaint_SelectedIndexChanged">
                        <asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
                        <asp:ListItem Text="Invalid" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Valid" Value="1"></asp:ListItem>
                       </asp:DropDownList>

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

<asp:TextBox ID="txtComplainant" runat="server" CssClass="textField_width"></asp:TextBox>

1 个答案:

答案 0 :(得分:1)

看起来它没有找到控件..

这部分

if (ddl.SelectedValue == "0") 
{
   txtComplainant.Visible = false;
}

应该是我想的那样......就像这样..

foreach(GridViewRow rw in GridViewSmsComplaints.Rows)
{
   TextBox txtComplainant = ((TextBox)rw.FindControl("txtComplainant"));//added this line to find the control.

    if(rw.RowIndex != RowIndex)
    {
       rw.Enabled = false;
    }

    if (ddl.SelectedValue == "0") 
    {
       txtComplainant.Visible = false;
    }
}

如果这不能提供您想要的结果,您也可以试试这个......

GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
TextBox txtComplainant = ((TextBox)rw.FindControl("txtComplainant"));

if (ddl.SelectedValue == "0") 
{
  txtComplainant.Visible = false;
}

您需要将它放在foreach循环下面。这应该禁用更改下拉列的行中的控件。

编辑:我没有意识到它在网格之外。我已经测试了你的问题,它似乎有效。

这是我测试它的方式。

    protected void ddlTestDrop_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;

        if (ddl.SelectedValue == "1")
        {
            tbTestBox.Visible = true;
        }
        else if (ddl.SelectedValue == "0")
        {
            tbTestBox.Visible = false;
        }
    }

我能够在1到0之间来回点击以使其可见,然后不可见。我建议删除以下内容..

int RowIndex = Convert.ToInt32(row.RowIndex);
//lblSelectionMessage.InnerText = RowIndex.ToString();

foreach(GridViewRow rw in GridViewSmsComplaints.Rows)
{
   if(rw.RowIndex != RowIndex)
   {
      rw.Enabled = false;
   }
}

并查看是否可以解决您的问题。