C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Functionality in RowDatabound of Gridview Control


Posted Date: 17 Sep 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: santosh narayan poojariMember Level: Gold    
Rating: 1 out of 5Points: 10



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



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Selected Value in Dropdown in Gridview  .  Edit delete validation in Rowdatabound  .  Bind Dropdownlist in gridview  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Executing Transact-SQL DELETE statement using SQLCommand
Previous Resource: Making The Columns Visible False in Gridview
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use