How can I validation single dropdown inside grid view using JavaScript?

Ankit tewari
Ankit t...
112 Points
11 Posts

How can I validation single dropdown inside grid view using JavaScript?

I have used this code,

function Validate() {

var flag = true;
var dropdowns = new Array(); //Create array to hold all the dropdown lists.
var gridview = document.getElementById('<%=gv_accounting.ClientID%>'); //GridView1 is the id of ur gridview.
dropdowns = gridview.getElementsByTagName('select'); //Get all dropdown lists contained in GridView1.
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns.item(i).value == '0') //If dropdown has no selected value
{
flag = false;
break; //break the loop as there is no need to check further.
}
}
if (!flag) {
alert('Please select value in each dropdown');
}
return flag;

}

but problem is this. The validation working on all dropdown. But i want use this validation on single dropdown..

 

Views: 9745
Total Answered: 1
Total Marked As Answer: 0
Posted On: 13-Dec-2015 00:56

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4916 Points
27 Posts
         

Hi Ankit,

On Gridview rowdatabound event add an attribute onchange as : 

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = new DropDownList();
ddl=(DropDownList) e.Row.FindControl("ddlID");
ddl.Attributes.Add("onblur", "onchangemethodValidate(this);");
}
}

 And Aspx page: 

<script>
function onchangemethodValidate(ddl){
alert($("#ddl").val());
}
</script>
Posted On: 13-Dec-2015 03:20
 Log In to Chat