Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear

I have gridview in which i added a templatefield checkbox column and
a templatefield textbox column.

if checkbox is checked then
tetxbox will be visible
else
textbox will be invisible

how can i do this in asp.net

Thanks in advance

Please give me replay
Posted
Comments
   
And what's the problem?
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can try this way

create an event oncheckedchange and have the visible/invisibility option when the event raises(when you check/uncheck the checkbox)

I did this in EditItemTempate in gridview and it worked. May be you can give a try in ItemTemplate.

Regards
Naina
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can use template field or any bount field and then us JS to display or hide textbox

<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
        <columns>
        <asp:templatefield>
        <itemtemplate>
        <asp:checkbox id="chk" runat="server" onclick="cknunck(this)" />
        <asp:textbox id="tbx" runat="server" style="display:none;"></asp:textbox>
        </itemtemplate>
        </asp:templatefield>
        </columns>
        </asp:gridview>


 function cknunck(ck)
        {
            var dis="none";
            if(ck.checked)
                dis="";
            ck.nextSibling.style.display=dis;

        }
// nextSibling use to access next element after current element
// if you use bound field then you can use 
// ch.parentElement.nextSibling.children[0] 
// if text box is in next cell of table


you can use style.visibility="hidden" tag also in place of style.display

if this will help you then please mark as solution
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

You Can Try This one......

<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
<columns> <ItemTemplate>
<asp:CheckBox ID ="chkAdd" AutoPostBack="true" runat="server" oncheckedchanged="chkAdd_CheckedChanged" Enabled="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="Txtgrd" Visible="false" runat ="server" " />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


0n .cs Use this....
foreach (GridViewRow row in GridView1.Rows)
{
checkBox chkadd = (CheckBox)row.FindControl("chkAdd");
TextBox txtf = (Button)row.FindControl("btngrd");
if (chkadd.Checked == true)
{txtf.visible=true;}
if (chkadd.Checked == false)
{
txtf.Visible = false;
}

if U don't want use JS then u sholud try this one..
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100