Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 using javascript
Posted
Comments
What have you tried?
priyanshbhaliya 11-Nov-13 0:46am    
i tried to visible textbox when checkbox is checked in grid view's any row
textbox and checkbox is in same column...
and using javascript i tried to do this and also i done using asp.net code but it server side
so page is posted back every checkbox checked so i dont need this to post every check
So, what is the problem with JavaScript code?
priyanshbhaliya 11-Nov-13 1:24am    
how to do it ? i dont knw??

i refer this codeproject questions answer:
http://www.codeproject.com/Answers/315153/if-checkbox-in-gridview-is-checked-then-textbox-wi#answer2

but javascript not work
Sergey Alexandrovich Kryukov 11-Nov-13 1:39am    
"JavaScript don't work" is not informative. Will you show your code?
—SA

1 solution

Hi
Below is rough code that implements your functionality

JavaScript
function ToggleTextBox(checkid,textid) {
            var checkBox = document.getElementById(checkid);
            if (checkBox.checked)
            {
                document.getElementById(textid).style.visibility = 'visible';
            }
            else
            {
                document.getElementById(textid).style.visibility = 'hidden';
            }
        }


Grid View Definition

C#
<asp:gridview runat="server" id="grid" onrowdatabound="RowDataBoundEvent" xmlns:asp="#unknown">
    <columns>
    <asp:templatefield headertext="Check">
    <itemtemplate>
        <asp:checkbox runat="server" id="cbSelect" checked="false" />
    </itemtemplate>
    </asp:templatefield>
    <asp:templatefield headertext="B1">
    <itemtemplate>
        <asp:textbox id="textBox" runat="server"></asp:textbox>
    </itemtemplate></asp:templatefield>
    </columns>
    </asp:gridview>


Code Behind File
C#
protected void Page_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("Name");
           dt.Rows.Add("G");
           dt.Rows.Add("K");
           grid.DataSource = dt;
           grid.DataBind();
       }

       protected void RowDataBoundEvent(object sender, GridViewRowEventArgs e)
       {

           int rowIndex = e.Row.RowIndex;
           if (rowIndex >= 0)
           {
               ((CheckBox)e.Row.FindControl("cbSelect")).Attributes.Add("onclick", "javascript:ToggleTextBox('" +
                       ((CheckBox)e.Row.FindControl("cbSelect")).ClientID + "','"+
                       ((TextBox)e.Row.FindControl("textBox")).ClientID + "')");
           }
       }
 
Share this answer
 

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