Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dropdownlist control in gridview item template field and i want to valdate that control on button click ,button also in gridview . so please tell me any validation method either javascript validation or server side validation
Posted

XML
<asp:TemplateField ItemStyle-VerticalAlign="Top">
    <ItemTemplate>
        <asp:dropdownList id="drp_item" runat="server"></asp:dropdownList>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField ItemStyle-VerticalAlign="Top">
    <ItemTemplate>
        <asp:LinkButton ID="btn_Add" ToolTip="ADD Product" OnClientClick="javascript:return Validate(this);" runat="server">
        </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

C#
function Validate(ths,) {
    var Cntrl_Id = ths.id.split('_');
    var drp = Cntrl_Id[0] + '_' + Cntrl_Id[1] + '_dropdown';  // here dropdown is dropdwon name.
    if ((document.getElementById(drp).value == "")
    {
        alert("Please Enter the Strength.");
        return false;
    }
    return true;
}

In below function ths will pass the actual button name and will ths.id.split you can find the your dropdown control name number of Cntrl_Id[0] can be depend on the you actual page and its control.

Thanks
Zubair
 
Share this answer
 
v3
Comments
sureshsharma123 1-Sep-14 2:11am    
hello sir
this code is not working
C#
function ValidateGridView() {

    var grid = document.getElementById('<%=GridView.ClientId=%>);

    if (grid != null) {
   
            var TargetBaseControl = grid;

            //var TargetChildControl1 = "TxtGrNo";
            //get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName("input");

            for (var n = 0; n < Inputs.length; ++n) {
                if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf('chkSelect', 0) >= 0) {
                    if (Inputs[n].checked) {
                        var RowId = (Inputs[n].parentNode.parentNode.parentNode).rowIndex;
                        var InpCtrls = grid.rows[RowId].getElementsByTagName('input');
                        for (var i = 0; i < InpCtrls.length; ++i) {
                            if (InpCtrls[i].id.indexOf('TxtGrNo', 0) >= 0 && InpCtrls[i].value == '') {
                                document.getElementById(InpCtrls[i].id).style.border = "1px solid red";
                                document.getElementById(InpCtrls[i].id).style.background = "#FFCECE";
                                document.getElementById(InpCtrls[i].id).focus();
                                alert('Plz Enter value in GRNumber...!');
                                return false;
                            }
			}
		}
	    }
	}

}


Above code is used to validate any controls inside GridView.
I have implemented above code for checkbox and textbox inside gridview by finding all input controls from Gridview.
Same you cand use for "Select" controls inside gridview.
 
Share this answer
 
Is your Button is inside the grid?
 
Share this answer
 
Comments
sureshsharma123 1-Sep-14 2:47am    
yes sir
Thanks7872 1-Sep-14 7:20am    
Use reply button at comment. Don't post multiple solutions to single post until and unless it is necessary.
Check with below code.

foreach (GridViewRow row in this.gridView.Rows)
{
DropDownList ddl = row.FindControl("ddlName") as DropDownList;
if(check drop down here..)
{

}
}
 
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