在数据列表中选中/取消选中复选框时显示/隐藏文本框的最佳方法

时间:2013-11-24 14:39:57

标签: c# asp.net ajax checkbox textbox

我是aspnet编程的新手,需要一些帮助。我一直在搜索互联网,无法找到解决问题的方法。我基本上需要一种只有在选中相应的复选框时才能显示文本框的方法。文本框和复选框是内部数据列表的一部分;控件必须是数据绑定。我已经尝试过JQuery并且无法让它工作,所以现在我正在使用UpdatePanel尝试AJAX - 仍然无法正常工作。下面是我当前的代码,它位于用户控件中。我基本上包括了与复选框/文本框有关的代码。脚本管理器在母版页中引用。任何帮助将不胜感激。

提前致谢!

HTML ----->

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DataList ID="outerDataList" runat="server" OnItemDataBound="outerRep_ItemDataBound">  
<ItemTemplate>
<div id="div1" runat="server" visible='<%# ((string)Eval("Code")) != "BABRB" %>'>
  <asp:Label ID="lblBABProdType" Font-Size="Medium" Font-Bold="true" runat="server" Text='<%# Eval("Name") %>' style="padding-left:20px" />
    <asp:DataList ID="innerDataList" runat="server" RepeatColumns="3" DataKeyField="ProductID" >
      <ItemTemplate>
        <div id="div4" runat="server" visible='<%# ((string)Eval("ProductCode")) == "BABBE" %>'>
          <table ID="table4" runat="server" align="left" width="80%" style="margin-left:30px; font-size:smaller">
              <tr>
                <td width="3%">
                    <asp:CheckBox ID="bevCheckBox" runat="server" AutoPostBack="true" 
                        OnCheckedChanged="chkBox_CheckedChanged"/>
                </td>
                <td align="left" width="7%">
                    <asp:ImageButton ID="bevImgBtn" runat="server" width="40" height="40" align="middle" 
                        ImageUrl='<%# "~/ProductImages/"+Eval("Image1FileName").ToString() %>'
                        OnClick="ImgBtn_Click" CommandArgument='<%# Eval("ProductID") %>'/>
                </td>
                <td valign="middle" width="70%">
                    <span class="ProductName">            
                    <asp:LinkButton ID="bevLnkBtn" runat="server" OnClick="LnkBtn_Click" CssClass="BABProductName" 
                        CommandArgument='<%# Eval("ProductID") %>'>
                        <%# DataBinder.Eval(Container.DataItem, "Name")%>
                        <br /></asp:LinkButton>
                    </span>
                    <span class="ProductPrice">
                        <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")%>
                    </span
                    <asp:TextBox ID="bevQtyTxtBox" runat="server" Text="Qty: " Font-Size="XX-Small" 
                        Width="40px" ViewStateMode="Disabled"/>
                </td>
              </tr>
          </table>
        </div>
        <div id="div5" runat="server" visible='<%# ((string)Eval("ProductCode")) == "BABFO" %>' >
          <table ID="table5" runat="server" align="left" width="80%" style="margin-left:30px; font-size:smaller">
              <tr>
                <td width="3%">
                    <asp:CheckBox ID="foodCheckBox" runat="server" AutoPostBack="true" 
                        OnCheckedChanged="chkBox_CheckedChanged"/>
                </td>
                <td align="left" width="7%">
                    <asp:ImageButton ID="foodImgBtn" runat="server" width="40" height="40" align="middle" 
                        ImageUrl='<%# "~/ProductImages/"+Eval("Image1FileName").ToString() %>'   
                        OnClick="ImgBtn_Click" CommandArgument='<%# Eval("ProductID") %>'/>
                </td>
                <td valign="middle" width="70%">
                    <span class="ProductName">            
                    <asp:LinkButton ID="foodLnkBtn" runat="server" OnClick="LnkBtn_Click" CssClass="BABProductName" 
                        CommandArgument='<%# Eval("ProductID") %>'>
                        <%# DataBinder.Eval(Container.DataItem, "Name")%>
                        <br /></asp:LinkButton> 
                    </span>
                    <span class="ProductPrice">
                        <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")%>
                    </span>
                    <asp:TextBox ID="foodQtyTxtBox" runat="server" Text="Qty: " Font-Size="XX-Small" 
                        Width="40px" ViewStateMode="Disabled"/>
                </td>
              </tr>
          </table>
        </div>
      </ItemTemplate>
    </asp:DataList>
  </div>
  <br />
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>

代码背后-----&gt;

protected void Page_Load(object sender, EventArgs e)
{
    PopulateData();
}

private void PopulateData()
{
    // Retrieve list of products
    string categoryID = Request.QueryString["BABCategoryID"];
    // set the stored procedure name  
    SqlConnection sqlConn = new SqlConnection(ChocolateExpConfiguration.DbConnectionString);
    SqlCommand sqlComm = new SqlCommand("GetBABProductsInCategory", sqlConn);
    sqlComm.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adptr = new SqlDataAdapter(sqlComm);
    sqlComm.Parameters.AddWithValue("@BABCategoryID", categoryID);
    DataSet ds = new DataSet();
    adptr.Fill(ds);
    ds.Relations.Add(new DataRelation("CodeRelation", ds.Tables[0].Columns["Code"], ds.Tables[1].Columns["ProductCode"]));
    outerDataList.DataSource = ds.Tables[0];
    outerDataList.DataBind();
}

protected void outerRep_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        DataRowView drv = e.Item.DataItem as DataRowView;
        DataList innerDataList = (DataList)e.Item.FindControl("innerDataList");
        innerDataList.DataSource = drv.CreateChildView("CodeRelation");
        innerDataList.DataBind();
    }

}

protected void chkBox_CheckedChanged(object sender, EventArgs e)
{
    foreach (DataListItem parentItem in outerDataList.Items)
    {
        //Find nested items(DataList) of parent Datalist
        DataList innerDataList = (DataList)parentItem.FindControl("innerDataList");
        foreach (DataListItem childItem in innerDataList.Items)
        {
            CheckBox bevCheckBox = (CheckBox)childItem.FindControl("bevCheckBox");
            CheckBox foodCheckBox = (CheckBox)childItem.FindControl("foodCheckBox");
            TextBox bevQtyTxtBox = (TextBox)childItem.FindControl("bevQtyTxtBox");
            TextBox foodQtyTxtBox = (TextBox)childItem.FindControl("foodQtyTxtBox");
            if ((bevCheckBox.Checked == true) || (foodCheckBox.Checked == true))
            {
                bevQtyTxtBox.Visible = true;
                foodQtyTxtBox.Visible = true;
            }
            else
            {
                bevQtyTxtBox.Visible = false;
                foodQtyTxtBox.Visible = false;
            }
            UpdatePanel1.Update();
        }
    }
}*

1 个答案:

答案 0 :(得分:-2)

protected void EnableTextBox()
{
    int count = int.Parse(GridView1.Rows.Count.ToString());

    for (int i = 0; i < count; i++)
    {
        CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
        CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
        CheckBox cb2 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox3");
        TextBox tb = (TextBox)GridView1.Rows[i].Cells[4].FindControl("txtration");
        TextBox tb1 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txtjob");
        TextBox tb2 = (TextBox)GridView1.Rows[i].Cells[6].FindControl("txtaadhar");

        if (cb.Checked == true)
        {
            tb.Visible = true;               
        }
        else
        {
            tb.Visible = false;
        }
        if (cb1.Checked == true)
        {
            tb1.Visible = true;                
        }
        else
        {
            tb1.Visible = false;              
        }
        if (cb2.Checked == true)
        {
            tb2.Visible = true;               
        }
        else
        {
            tb2.Visible = false;
        }
    }
}
事件中的

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    EnableTextBox();
}