javascript

Last post 05-10-2008 1:26 AM by krishnav. 5 replies.

Sort Posts:

  • javascript

    05-07-2008, 12:03 PM
    • Loading...
    • renjithgk
    • Joined on 09-04-2007, 3:55 AM
    • Posts 15

     

    Hi all,

     I am having a datagrid and on that datagrid i have a checkbox and a textbox for entering comments.when the page loads stored procedure executes to bind the data to datagrid.so i need to make a new change.when i click on the checkbox textbox should be displayed in the datagrid.

    i have tried this pls find the code below:

    function show() { { if(dggoals.getElementById('ChkGoals').checked) { document.getElementById('txtGHComments').style.visibility ='visible'; } else { document.getElementById('txtGHComments').style.visibility ='hidden'; } } }

    in checkbox event i entered as onclick or oncheckchanged =show()

     

    i m getting all the textbox as visible..guide me

     

     

  • Re: javascript

    05-07-2008, 12:30 PM
    Answer
    • Loading...
    • rmaiya
    • Joined on 06-25-2007, 11:08 PM
    • Olympia, WA
    • Posts 1,226

    You can not access controls inside the grid like above you mentioned. You need to get the client ID.  And also oncheckchanged is a  server side event, I feel you must be getting a java script error also. Anyway here is the deal.

    modify your function

    function show(chk, txt ) 

    {

     if(chk.checked )

    {

           txt.style.display ='';

    }

    else {

    txt.style.display ='none';

    }

    On the server side

            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                CheckBox chk = e.Row.FindControl("CheckBox1") as CheckBox;
                TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
                chk.Attributes.Add("onchange", "javascript:show('" + chk.ClientID + "','" + txt.ClientID + "')");
            }

     

     

     

    Raghu
    (MCSD.NET, MCAD.NET, MCDBA)
    [Don't forget to click on Mark as answer on the post that helped you ]
  • Re: javascript

    05-07-2008, 1:11 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 6,867

    If you're using a DataGrid, try this:

    In the aspx file:

    <asp:datagrid id="DataGrid1" OnItemDataBound="DataGrid1_ItemDataBound" ...
    ...

    <script type="text/javascript">
    <!--
    function checkBoxOnClick(elementRef, textBoxId)
    {
     var textBoxRef = document.getElementById(textBoxId);
     textBoxRef.style.display = (elementRef.checked == true) ? 'inline' : 'none';
    }
    // -->
    </script>

    CodeBehind file:

    protected void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {   
     if ( e.Item.ItemIndex >= 0 )
     {
      CheckBox checkBox = (CheckBox)e.Item.FindControl("CheckBox1");
      TextBox textBox = (TextBox)e.Item.FindControl("TextBox1");
      if ( (checkBox != null) && (textBox != null) )
      {
       checkBox.Attributes.Add("onclick", "show('" + checkBox.ClientID + "','" + textBox.ClientID + "');");
      }
     }
    }

    NC...

  • Re: javascript

    05-09-2008, 2:50 AM

    This is perfect i checked out Thanks for Help

    Warm Regards

    Chethan M
    MCTS
    Bangalore India
  • Re: javascript

    05-09-2008, 9:42 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 6,867

    Glad you got it working!

    NC...

     

  • Re: javascript

    05-10-2008, 1:26 AM
    • Loading...
    • krishnav
    • Joined on 05-01-2008, 9:45 AM
    • Vijayawada
    • Posts 49

    function checkBoxOnClick(idofcheckbox, textorgridorany)
    {
     var target= document.getElementById(" textorgridorany");
     target.style.display = (idofcheckbox.checked == true) ? 'inline' : 'none';
    }

     

    In code behind

    ----------------

    checkBox.Attributes.Add("onclick", "show('id of chekbox','id of grid or textbox or any');");

Page 1 of 1 (6 items)