locked
client side access to checkbox and dropdownlist RRS feed

  • Question

  • User82231285 posted

     i am able to access client-side dropdownlist but i want something like if the checkbox is checked then only get selected value of dropdownlist on client side

    checkbox checked should be also on client - side 

    both checkbox and dropdownlist are in datagrid .

    here is the code

    ---on itemdatabound 

           If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

                  Dim ddlSample As DropDownList = CType(e.Item.FindControl("pweighting"), DropDownList)
                  ddlSample.Attributes.Add("onchange", "ClientShowDataGrid(this);")


            End If

    and then in javascript function i am doing my task 

    but i also want to check like if the checkbox is checked in datagrid.

     hope it all make sense

    thanx heaps,

    Mrugesh.
     


     

    Tuesday, February 26, 2008 12:13 AM

Answers

  • User-2116278700 posted

    Hi,

    this is the javascript and its written on the assumption that you have a checkbox placed before dropdown and there is no other dropdown placed outside gridview. this script will fulfill your basic requirements.

     

    <script type="text/javascript">
            function GetSelectedValues()
            {
                var frm = document.forms[0];
                
                for (i=0;i<frm.elements.length;i++)
                {
                    if (frm.elements[i].type =="select-one")//"checkbox")
                    {
                        if (frm.elements[i-1].type =="checkbox" && frm.elements[i-1].checked)
                            alert('Success!!!');
                    }
                }
                return false;
            }
    </SCRIPT>< pre> 

     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Me.Button1.Attributes.Add("onclick", "javascript:return GetSelectedValues()")
            End If
    End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2008 3:29 AM

All replies

  • User312496708 posted

    Pass the checkbox's clients id to the function

      If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

                  Dim ddlSample As DropDownList = CType(e.Item.FindControl("pweighting"), DropDownList)

                 Dim CheckBox1 As CheckBox = CType(e.Item.FindControl("CheckBoxName"), CheckBox)


                  ddlSample.Attributes.Add("onchange", "ClientShowDataGrid(this,'" + CheckBox1.ClientID + "');")


            End If

     

    Tuesday, February 26, 2008 12:39 AM
  • User-2116278700 posted

    Hi,

    I believe you are looking for something like this

     

    <script type="text/javascript">
            function StatusGridDropdown(drpdown)
            {
                document.getElementById(drpdown).disabled = !document.getElementById(drpdown).disabled;
            }
    </script>
    
    
    
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">
            <Columns>
                <asp:TemplateField>
                    <AlternatingItemTemplate>
                        <asp:CheckBox ID="cbStatus" runat="server" />
                    </AlternatingItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbStatus" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <AlternatingItemTemplate>
                        <asp:DropDownList ID="ddl" runat="server" Enabled="False" Width="169px">
                        </asp:DropDownList>
                    </AlternatingItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddl" runat="server" Width="169px" Enabled="False">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

     

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                DirectCast(e.Row.FindControl("cbStatus"), CheckBox).Attributes.Add("onclick", "javascript:StatusGridDropdown('" & DirectCast(e.Row.FindControl("ddl"), DropDownList).ClientID & "')")
            End If
    End Sub
     
    Tuesday, February 26, 2008 12:58 AM
  • User82231285 posted

    that helped abit

    but in my javascript how can i do something like get all selectedvalue in each dropdownlist where the checkbox are checked..

    i am getting all the selected value of each dropdownlist but i only want if the respected checkbox is checked.

    following is my javascript function

      function ClientShowDataGrid(obj,obj2)
      {
               
                 var grid = document.getElementById('<%= datagrid1.ClientID %>');
                 var ddls = grid.getElementsByTagName('SELECT');    
              
                var myArray = new Array();
                var temp = 0;
                var count = 0;
              
                for(var i = 0; i < ddls.length; i++)
                {     
                    temp  =  ddls[i].value;  
                   myArray[i] = parseInt(temp);
               }
             
               for(var i =0 ; i<myArray.length; i ++)
               {
                 count += parseInt(myArray[i]);
               }
              
                    var maxnumber = 100;
                    var totalavailable = 100 - count;
                    if (count > 100)
               {
                        var lblvalue = document.getElementById('<%= availableweight.ClientID %>');
                       lblvalue.innerText = "Weighting Should Add Upto 100 Your Weighting is = " + count;
                   }
                   else if (totalavailable >= 0)
                   {
                        var lblvalue = document.getElementById('<%= availableweight.ClientID %>');
                        lblvalue.innerText = totalavailable;
                    }
      }

    Wednesday, February 27, 2008 12:10 AM
  • User82231285 posted

    no i am still not able to achieve. 

    Wednesday, February 27, 2008 6:02 PM
  • User-2116278700 posted

    Hi,

    this is the javascript and its written on the assumption that you have a checkbox placed before dropdown and there is no other dropdown placed outside gridview. this script will fulfill your basic requirements.

     

    <script type="text/javascript">
            function GetSelectedValues()
            {
                var frm = document.forms[0];
                
                for (i=0;i<frm.elements.length;i++)
                {
                    if (frm.elements[i].type =="select-one")//"checkbox")
                    {
                        if (frm.elements[i-1].type =="checkbox" && frm.elements[i-1].checked)
                            alert('Success!!!');
                    }
                }
                return false;
            }
    </SCRIPT>< pre> 

     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Me.Button1.Attributes.Add("onclick", "javascript:return GetSelectedValues()")
            End If
    End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2008 3:29 AM