December 2009

Javascript Validation in Gridview

Make function in “Java Script” as Below… grvPowderCoat is Gridview ID

function PowderCoatValidate()
{
var result=””;
var GrvTst = document.getElementById(GetClientId(“grvPowderCoat”));
if (GrvTst != null) {

var Inputs = GrvTst.getElementsByTagName(“input”);
for (i = 0; i < Inputs.length; i++)
{
if (Inputs[i].type == 'text') {
var test1 = Inputs[i].id;
var array1 = test1.split("_");
if (array1[array1.length-1] == 'txtPowderA') {

if (Inputs[i].value == "")
{
result += "- Enter A\n";
//alert("Enter Width ");
//return false;
}
if(isNaN(Inputs[i].value))
{
result+= "- Enter Valid A\n";
}
}
if (array1[array1.length-1] == 'txtPowderValue') {

if (Inputs[i].value == "")
{
result += "- Enter Total\n";
}
if(isNaN(Inputs[i].value))
{
result+= "- Enter Valid Total\n";
}
}
}
}
var DDL = GrvTst.getElementsByTagName("select");
for (i = 0; i < DDL.length; i++)
{
if (DDL[i].type == 'select-one')
{
var test1 = DDL[i].id;
var array1 = test1.split("_");
if (array1[array1.length-1] == 'ddlPowderId')
{
if (DDL[i].selectedIndex == 0)
{
result += "- Select Section\n";
}
}
}
}
if(result!="")
{
alert(result);
return false;
}
}
}

Call this function in .CS Page

protected void grvPowderCoat_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
string id = e.Row.Cells[0].Text; // Get the id to be deleted
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[2]; //cast the ShowDeleteButton link to linkbutton
if (lb != null)
{
lb.Attributes.Add(“onclick”, “return ConfirmOnDelete(‘” + id + “‘);”); //attach the JavaScript function with the ID as the paramter
}
}
}
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
string id = e.Row.Cells[0].Text; // Get the id to be deleted
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0]; //cast the ShowDeleteButton link to linkbutton
if (lb != null)
{
lb.Attributes.Add(“onclick”, “return PowderCoatUpdate();”); //attach the JavaScript function with the ID as the paramter
}
}
}
}

Enalbe Or Disable Controls In Asp.Net

Give ID and runat=”server” attribute to Div or Table Tag for enable or disable the controls in it. Write the followin function and call them on any event.

Disable Controls

private void DisableControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
DisableControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Enabled = false;
}
else
if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Enabled = false;
}
if (_ChildControl is DropDownList)
{
((DropDownList)_ChildControl).Enabled = false;
}
if (_ChildControl is RadioButton)
{
((RadioButton)_ChildControl).Enabled = false;
}
if (_ChildControl is GridView)
{

((GridView)_ChildControl).Enabled = false;
}
if (_ChildControl is LinkButton)
{
((LinkButton)_ChildControl).Enabled = false;
}
}
}
}
Enable Contorls
private void EnableControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
EnableControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Enabled = true;
}
else
if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Enabled = true;
}
if (_ChildControl is DropDownList)
{
((DropDownList)_ChildControl).Enabled = true;
}
if (_ChildControl is RadioButton)
{
((RadioButton)_ChildControl).Enabled = true;
}
if (_ChildControl is GridView)
{
((GridView)_ChildControl).Enabled = true;
}
if (_ChildControl is LinkButton)
{
((LinkButton)_ChildControl).Enabled = true;
}
}
}

}
#endregion

#region “Update and Get Boq Approve details”
public void UpdateApproveByName()
{
dtSky = (DataTable)ViewState[“dtSky”];
SortedList sl = new SortedList();
sl.Add(“@ApproveBy”, txtApprovedName.Text);
sl.Add(“@ApproveDate”, DateTime.Today.ToShortDateString());
sl.Add(“@BoqNo”, Convert.ToString(dtSky.Rows[0][“BoqNo”]));

int rowAffected = obj.ExecuteSQL(“spUpdateBoq”, sl);
}

public void GetApproveDetails()
{
if (!string.IsNullOrEmpty(Request.QueryString[“BoqNo”]))
{
DataTable dtBoq = new DataTable();
SortedList sl = new SortedList();
sl.Add(“@BoqNo”,Convert.ToString(Request.QueryString[“BoqNo”]));
dtBoq = obj.GetDataTable(“spGetBoqId”,sl);
if (dtBoq != null)
{
if (dtBoq.Rows.Count > 0)
{
txtApprovedName.Text = Convert.ToString(dtBoq.Rows[0][“ApproveBy”]);
if (dtBoq.Rows[0][“ApproveDate”] != null)
{
lblApproveDateVal.Text = Convert.ToString(dtBoq.Rows[0][“ApproveDate”]);
}
}
}
}
}