Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
I'm developing an project in that i have Datagrid inside the data grid i have 2 controls named checkbox and textbox.

My Requirement:

What i need is initially all the textbox inside the datagrid is disabled.
when i click the check box then its associated textbox alone should get enable and should set focus in that and if i again uncheck it then it should disable.

What i acheived is:

I created the datagrid , checkbox and textbox. and when the check box is clicked its associated textbox is getting focus.

What i seek for is:

Really i dont know how to enable and disable the textbox based on check box click.

Here is my aspx code:
ASP.NET
<blockquote class="FQ"><div class="FQA">Quote:</div><asp:DataGrid ID="dgModuleSearch" runat="server" BorderWidth="1px" BorderColor="#FE9B00" BorderStyle="Solid" BackColor="White" AllowPaging="True" Font-Names="Verdana" Font-Size="Medium" AutoGenerateColumns="False" AllowSorting="True"OnPageIndexChanged="dgModuleSearch_PageIndexChanged" OnItemCommand="dgModuleSearch_Select" PageSize="25"  OnItemDataBound="dgModuleSearch_DataBound">
<SelectedItemStyle Font-Bold="True" ForeColor="Black" BackColor="Snow"></SelectedItemStyle>
<EditItemStyle BackColor="AntiqueWhite"></EditItemStyle>
<PagerStyle BackColor="#FDE9CB" ForeColor="#003399" HorizontalAlign="Right" Mode="NumericPages" Position="Bottom" Font-Size="Small" Font-Bold="true" />
<AlternatingItemStyle BackColor="Snow"></AlternatingItemStyle>
<ItemStyle ForeColor="#000066" BackColor="Snow"></ItemStyle>
<HeaderStyle Font-Size="XX-Small" Font-Bold="True" Height="10px" ForeColor="#000000"BackColor="#FFDBA6"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Select">
<HeaderStyle Width="5%" Font-Underline="true" />
<ItemTemplate>ID="cb1" runat="server"  Width ="265px" CommandName="SelectItem"
OnCheckedChanged="cb1_CheckedChanged" Text='<%# DataBinder.Eval(Container.DataItem, "Modules")%>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="No of Jobs/Transactions">
<HeaderStyle Width="5%" Font-Underline="true" />
<ItemTemplate>
<asp:TextBox ID="tb1" runat="server" AutoPostBack="true" Columns="5" Text="" Width ="265px" Enabled="true"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ID" HeaderText="ID" Visible="false"></asp:BoundColumn>
</Columns>
</asp:DataGrid></blockquote>



Here is my code.cs:

if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                CheckBox cb1 = (CheckBox)e.Item.FindControl("cb1");
                TextBox tb1 = (TextBox)e.Item.FindControl("tb1");
                if (cb1.Checked == true)
                {
                    tb1.Enabled = true;
                    cb1.Attributes.Add("onclick", "javascript:if(this.checked){document.getElementById('" + tb1.ClientID + "').focus()}");
                }
                else
                {
                    tb1.Enabled = false;
                }
                
            }


It would be appreciatable if some one helps me to solve this.

Thanks in Advance.
Posted

you need to add AutoPostBack="True" for your CheckBox
if you need to hide textbox in cb1_CheckedChanged event; try below
C#
public void cb1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow objItem = (GridViewRow)chk.Parent.Parent;
    CheckBox cb1 = (CheckBox)objItem.FindControl("cb1");
    TextBox tb1 = (TextBox)objItem.FindControl("tb1");
    tb1.Visible = cb1.Checked; 
}
 
Share this answer
 
v5
Comments
[no name] 9-May-14 5:38am    
Hi thanks for the quick response.
i tried your above code but i getting error near item in the following 2 lines CheckBox cb1 = (CheckBox)e.Item.FindControl("cb1");
TextBox tb1 = (TextBox)e.Item.FindControl("tb1");
DamithSL 9-May-14 5:59am    
hope you are doing this on dgModuleSearch_DataBound event and also you need to add the validation as same as do in your code. pls check my updated answer
[no name] 9-May-14 6:02am    
No in dgModuleSearch_DataBound if i use this code there is no error but when i use same code in cb1_change Event then it shows error inthe word "item" as System.Eventsargs does not cnotain the definition for item and no extension method for item and so on<br><br><br>
here is my code:<br><br><br>
 <br><br><br>
protected void cb1_CheckedChanged(object sender, EventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
CheckBox cb1 = (CheckBox)e.Item.FindControl("cb1");
TextBox tb1 = (TextBox)e.Item.FindControl("tb1");
tb1.Visible = cb1.Checked;
}
}
Hi This is How i Make my Project to work:


foreach (DataGridItem dr in dgModuleSearch.Items)
            {
                CheckBox cb1 = (CheckBox)dr.FindControl("cb1");
                TextBox tb1 = (TextBox)dr.FindControl("tb1");
               // if (dr.Cells[0].Text != null && dr.Cells[0].Text != "")
                if (cb1.Checked)
                {
                    tb1.Visible = cb1.Checked;
                    cb1.Attributes.Add("onclick", "javascript:if(this.checked){document.getElementById('" + tb1.ClientID + "').focus()}");
                }
            }


I need the code for vise versa

(i.e)Can Some one say how to set the enable of the textbox in the datagrid to false when check box in the datagrid is is unchecked.

thanks in Advance.
 
Share this answer
 
Comments
[no name] 9-May-14 6:51am    
hi the following code helps to hide the textbox

if (cb1.Checked)
{
tb1.Visible = cb1.Checked;
cb1.Attributes.Add("onclick", "javascript:if(this.checked){document.getElementById('" + tb1.ClientID + "').focus()}");
}
else
{
tb1.Visible = false;
}
[no name] 27-May-14 9:29am    
Hi may i know what is the reason for downvoting

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900