How Can I Make An Onclick Handler With A Row Index As Parameter, On A Dataview Grid Row In Asp.net?
Solution 1:
You can do it in the RowDataBound
event of the Gridview.
protectedvoidGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:alert('Hello');");
e.Row.Style.Add("cursor", "pointer");
}
}
Solution 2:
Instead, why cant u write the required functionality in SelectedIndexChanged()
event of the datagridview? And also you can give set the style of the selected index like this
<SelectedItemStyleFont-Size="10px"Font-Names="Verdana"HorizontalAlign="Left"ForeColor="Black"VerticalAlign="Middle"BackColor="Gold"></SelectedItemStyle>
Solution 3:
this onclick event.. is this to point to a custom javascript function? meaning... is update(1)
a client side javascript function?
I you are looking to perform some server side operation when a row of the datagridview is clicked.. this is the method...
- add a select command column to the datagridview: This will enable the user to select a row.
- Write whatever you want done in the onSelectedIndexChanged event handler of the datagridview.
Following is the aspx of the gridview.
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="Id"DataSourceID="SqlDataSource1"EmptyDataText="There are no data records to display."BackColor="White"BorderColor="#999999"BorderStyle="Solid"BorderWidth="1px"CellPadding="3"ForeColor="Black"GridLines="Vertical"onselectedindexchanged="GridView1_SelectedIndexChanged"><Columns><asp:CommandFieldShowSelectButton="True" /><asp:BoundFieldDataField="Id"HeaderText="Id"ReadOnly="True"SortExpression="Id" /><asp:BoundFieldDataField="UserId"HeaderText="UserId"SortExpression="UserId" /><asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name" /><asp:BoundFieldDataField="Date"HeaderText="Date"SortExpression="Date" /></Columns><FooterStyleBackColor="#CCCCCC" /><PagerStyleBackColor="#999999"ForeColor="Black"HorizontalAlign="Center" /><SelectedRowStyleBackColor="#000099"Font-Bold="True"ForeColor="White" /><HeaderStyleBackColor="Black"Font-Bold="True"ForeColor="White" /><AlternatingRowStyleBackColor="#CCCCCC" /></asp:GridView>
Pay attention to the in the columns
tag
Your event handler will be like...
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//Write whatever you want here
}
Solution 4:
You can access the tr using e.Row and the index using e.Row.RowIndex in the RowDataBound event. The bound data is accessible using e.Row.DataItem in case you want to access the real Id of the object or the old values.
protectedvoidGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow )
{
var theItem = e.Row.DataItem as MyItemType;
e.Row.Attributes.Add("onclick","update('" + e.Row.RowIndex + "')");
e.Row.Style.Add("cursor", "pointer");
}
}
Index starts at 0 for the row collection, so in case you want index to start at 1 you need to add 1 to RowIndex.
Post a Comment for "How Can I Make An Onclick Handler With A Row Index As Parameter, On A Dataview Grid Row In Asp.net?"