Learning
The functionality that can be coded in gv_RowDataBound of Gridview control are:
1)To bind dropdownlist in Gridview 2)Show Selected value field of dropdownlist in Gridview with respect to RowTemplate field value. 3)To popup confirmation dialoq box on delete operation 4)To invoke javascript Client side validation on Edit operation
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { try { if ((e.Row.RowType == DataControlRowType.DataRow)) { if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) |
e.Row.RowState == DataControlRowState.Edit) {
DropDownList ddlControl = new DropDownList(); ddlControl = (DropDownList)e.Row.FindControl("ddl"); //***Feature 1:Populate the dropdownlist IDictionary<int, string> countryList = new Dictionary<int, string>(); countryList.Add(00, "INDIA"); countryList.Add(01, "SRILANKA"); countryList.Add(02, "NEPAL"); countryList.Add(03, "BUTAN");
//*** This is important step that decide what should appear in Dropdownlist ddlControl .DataValueField = "key"; ddlControl .DataTextField = "value"; ddlControl .DataSource = countryList; ddlControl .DataBind(); //***END
//***Feature 2 In ItemTemplate the default value in readonly view mode is mapped to
dropdownlist Key //value so as to make this selected when data is populated into it. ListItem listItem = ddlControl.Items.FindByValue(DataBinder.Eval(e.Row.DataItem,
"key").ToString()); if (listItem != null) { listItem.Selected = true; } //***END } } //***Feature 3: To popup confirmation dialoq box on delete operation if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lnkbtn = (LinkButton)e.Row.FindControl("lnkDelete"); lnkbtn.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to
delete this record :" +DataBinder.Eval(e.Row.DataItem, "CitizenID") + "')");
} //***END //***Feature 4: To provide javascript Client side validation on Edit operation if ((e.Row.RowType == DataControlRowType.DataRow & e.Row.RowState == DataControlRowState.Edit)||
e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate )) { LinkButton lnkUpdate = (LinkButton)e.Row.FindControl("lnkUpdate"); lnkUpdate.OnClientClick = "javascript:return ValidateForEditOperation('" +
((TextBox)e.Row.FindControl("txtCitizenName")).ClientID + "','" +
((TextBox)e.Row.FindControl("txtAddress")).ClientID + "');"; } //***END } catch (Exception ex) { } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|