简体   繁体   中英

Best way to show/hide a textbox when checkbox is checked/unchecked in a datalist

I'm new to aspnet programming and need some help. I've been scouring the internet and can't find a solution to my problem. I basically need a way to have a textbox show only when it's corresponding checkbox is checked. The textboxes and checkboxes are part of an inner datalist; the controls have to be databound. I've tried JQuery and couldn't get it to work, so now I'm trying AJAX with the UpdatePanel--still not working right. Below is my current code which resides in a user control. I basically included code that pertains to the checkboxes/textboxes. The script manager is referenced in the master page. Any help would be appreciated.

Thanks in advance!

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>

CODE BEHIND ----->

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();
        }
    }
}*
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;
        }
    }
}

in checkedchanged event

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM