Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css" rel="stylesheet" />


<script>
        $('#<%=DropDownList1.ClientID %>')
    </script>




<asp:GridView ID="gvchaptername" AutoGenerateColumns="false" GridLines="None"
              OnRowDeleting="gvchaptername_RowDeleting" OnRowDataBound="gvchaptername_RowDataBound" runat="server" >

<asp:TemplateField  HeaderText="">
             <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" class="dropdown" AutoPostBack="true"  OnSelectedIndexChanged="ddchapter_SelectedIndexChanged"   Width="160" DataTextField="CHAPTER" DataValueField="ID"  CssClass="form-control btn-sm"  runat="server"></asp:DropDownList>
             </ItemTemplate>
            </asp:TemplateField>

</Columns>

        </asp:GridView>


What I have tried:

I got DropDownList1 error
$('#<%=DropDownList1.ClientID %>') 
this is working outsife of gridview
Posted
Updated 27-Aug-20 5:06am

To acess of gridview ItemTemplate controle you should use code like that

add property of dropdown [DropDownList1] as clientidmode="static"

var gv = document.getElementById("<%=gvchaptername.ClientID %>");
     var dropvalue = gv.getElementsByTagName('DropDownList1');


var dropvalue = $("#ddrateplan").val();


Please check following code project link for reffrence

How to get the gridview item template element in javascript[^]

how to get selected value in javascript from dropdownlist of gridview - Stack Overflow[^]
 
Share this answer
 
Comments
F-ES Sitecore 27-Aug-20 10:51am    
What if the grid view has more than one row?
Asif 7969814 27-Aug-20 10:55am    
use loop like that


var table = document.getElementById("gvchaptername");

if (table.rows.length > 0) {
//loop the gridview table
for (var i = 1; i < table.rows.length; i++)

{

//get all the input elements

var inputs = table.rows[i].getElementsByTagName("input");

for (var j = 0; j < inputs.length; j++) {

//get the textbox1

if (inputs[j].id.indexOf("Textbox1") > -1) {
alert(inputs[j].value);
}

}

}
}

F-ES Sitecore 27-Aug-20 11:01am    
The html is still invalid, ids have to be unique. There is no point in using the id attribute if it isn't unique.
Asif 7969814 27-Aug-20 11:35am    
GridView with Itemtemplate is a collection of the same id objects. we have to use a loop row number to the identified object. Please share a screen shot where javascript has an error. I just want to know what is the target Just access dropdown value or something else.
F-ES Sitecore 27-Aug-20 11:50am    
If you make the id static then all rows will give the dropdown the same id making the html invalid and the dropdowns inaccessible by id. This code

<asp:GridView ID="gvchaptername" AutoGenerateColumns="false" GridLines="None" OnRowDataBound="gvchaptername_RowDataBound" runat="server" >
    <Columns>
        <asp:TemplateField  HeaderText="">
                <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" class="form-control" ClientIDMode="Static" runat="server"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
    </Columns>
</asp:GridView>

<script type="text/javascript">
    $("#DropDownList1").css("border-color", "red");
</script>


results in this;

https://imgur.com/a/h2VNvV4
I'd add a data attribute to your dropdowns that will allow you to identify and access them;

ASP.NET
<asp:DropDownList ID="DropDownList1" class="dropdown" data-type="ddl_chapter" AutoPostBack="true" ...


For your js code you have to remember there may be more than one dropdown so if you want to access their individual properties you will need some kind of loop;

HTML
<script type="text/javascript">
    $.each($("[data-type='ddl_chapter']"), function (index, item) {
        var ddl = $(item);
        alert('Drop down ' + index + ' has value ' + ddl.val());
    });
</script>


If you are wanting to target them as a whole using jquery selectors then do something like

JavaScript
$("[data-type='ddl_chapter']").css("border-color", "red");
 
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