| Author: Shivshanker Cheral 30 Jul 2008 | Member Level: Diamond | Rating: Points: 0 |
The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.
Buttons within a GridView control can also invoke some of the built-in functionality of the control. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.
The following example demonstrates how to use the RowCommand event to add the name of a customer from a GridView control to a ListBox control when a row's Add button is clicked.
Visual Basic Copy Code <%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' If multiple buttons are used in a GridView control, use the ' CommandName property to determine which button was clicked. If e.CommandName = "Add" Then
' Convert the row index stored in the CommandArgument ' property to an Integer. Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button clicked ' by the user from the Rows collection. Dim row As GridViewRow = CustomersGridView.Rows(index)
' Create a new ListItem object for the customer in the row. Dim item As New ListItem() item.Text = Server.HtmlDecode(row.Cells(2).Text)
' If the customer is not already in the ListBox, add the ListItem ' object to the Items collection of the ListBox control. If Not CustomersListBox.Items.Contains(item) Then
CustomersListBox.Items.Add(item)
End If
End If
End Sub
Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' The GridViewCommandEventArgs class does not contain a ' property that indicates which row's command button was ' clicked. To identify which row's button was clicked, use ' the button's CommandArgument property by setting it to the ' row's index. If e.Row.RowType = DataControlRowType.DataRow Then
' Retrieve the LinkButton control from the first column. Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
' Set the LinkButton's CommandArgument property with the ' row's index. addButton.CommandArgument = e.Row.RowIndex.ToString()
End If
End Sub
</script>
<html > <head runat="server"> <title>GridView RowCommand Example</title> </head> <body> <form id="form1" runat="server">
<h3>GridView RowCommand Example</h3>
<table width="100%"> <tr> <td style="width:50%">
<asp:gridview id="CustomersGridView" datasourceid="CustomersSource" allowpaging="true" autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand" onrowcreated="CustomersGridView_RowCreated" runat="server">
<columns> <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="City" headertext="City"/> </columns>
</asp:gridview>
</td>
<td style="vertical-align:top; width:50%">
Customers: <br/> <asp:listbox id="CustomersListBox" runat="server"/>
</td> </tr> </table>
<!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/>
</form> </body> </html>
C# Copy Code<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple buttons are used in a GridView control, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked // by the user from the Rows collection. GridViewRow row = CustomersGridView.Rows[index];
// Create a new ListItem object for the customer in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[2].Text);
// If the customer is not already in the ListBox, add the ListItem // object to the Items collection of the ListBox control. if (!CustomersListBox.Items.Contains(item)) { CustomersListBox.Items.Add(item); } } }
void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e) {
// The GridViewCommandEventArgs class does not contain a // property that indicates which row's command button was // clicked. To identify which row's button was clicked, use // the button's CommandArgument property by setting it to the // row's index. if(e.Row.RowType == DataControlRowType.DataRow) { // Retrieve the LinkButton control from the first column. LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Set the LinkButton's CommandArgument property with the // row's index. addButton.CommandArgument = e.Row.RowIndex.ToString(); }
}
</script>
<html > <head runat="server"> <title>GridView RowCommand Example</title> </head> <body> <form id="form1" runat="server">
<h3>GridView RowCommand Example</h3>
<table width="100%"> <tr> <td style="width:50%">
<asp:gridview id="CustomersGridView" datasourceid="CustomersSource" allowpaging="true" autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand" onrowcreated="CustomersGridView_RowCreated" runat="server">
<columns> <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="City" headertext="City"/> </columns>
</asp:gridview>
</td>
<td style="vertical-align:top; width:50%">
Customers: <br/> <asp:listbox id="CustomersListBox" runat="server"/>
</td> </tr> </table>
<!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/>
</form> </body> </html>
|
| Author: Shivshanker Cheral 30 Jul 2008 | Member Level: Diamond | Rating: Points: 0 |
for more
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
http://authors.aspalliance.com/aspxtreme/sys/web/ui/webcontrols/GridViewClassRowCommand.aspx
|
| Author: suman 30 Jul 2008 | Member Level: Gold | Rating: Points: 2 |
check this msdn link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
|
| Author: chandramohan 30 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
<%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true" codefile="ExtractVariousControlsFromRow.aspx.cs" inherits="GridView_ExtractVariousControlsFromRow" title="GridView: Extract Various Controls From Row" %>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server"> <asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" datakeynames="Text" onrowcommand="GridView1_RowCommand" style="margin-bottom: 20px;"> <columns> <asp:boundfield datafield="Text" headertext="BoundField" /> <asp:buttonfield commandname="ButtonField" datatextfield="Text" headertext="ButtonField" /> <asp:checkboxfield datafield="Checked" headertext="CheckBoxField" /> <asp:hyperlinkfield datanavigateurlfields="Url" datatextfield="Text" headertext="HyperLinkField" target="_blank" /> <asp:templatefield headertext="LinkButton"> <itemtemplate> <asp:linkbutton id="LinkButton1" runat="server" commandargument='<%# Container.DisplayIndex %>' commandname="LinkButton" text='<%# Eval("Text") %>' /> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="DataBoundLiteralControl"> <itemtemplate> <%# Eval("Text") %> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="Label"> <itemtemplate> <asp:label id="Label1" runat="server" text='<%# Eval("Text") %>' /> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="HtmlInputCheckBox"> <itemtemplate> <input id="HtmlInputCheckBox1" runat="server" type="checkbox" checked='<%# Eval("Checked") %>' /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> <asp:linkbutton id="btnExtractAll" runat="server" text="extract all controls" onclick="btnExtractAll_Click" /> <asp:panel id="pnlResults" runat="server" enableviewstate="false" /> </asp:content>CODE-BEHIND
using System; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Text;
public partial class GridView_ExtractVariousControlsFromRow : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { GridView1.DataSource = new[] { new { Checked = true, Text = "abc", Url = "http://www.abc.com" }, new { Checked = false, Text = "def", Url = "http://www.def.com" }, new { Checked = true, Text = "ghi", Url = "http://www.ghi.com" } }; GridView1.DataBind(); } }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { this.ExtractControls(GridView1.Rows[Convert.ToInt32(e.CommandArgument)]); }
private void ExtractControls(GridViewRow row) { if (row != null) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("Row {0} Controls", row.RowIndex + 1);
// DataKeys sb.AppendFormat("DataKey: Value = '{0}'", GridView1.DataKeys[row.RowIndex].Value);
// BoundField sb.AppendFormat("BoundField: Text = '{0}'", row.Cells[0].Text);
// ButtonField LinkButton btn = row.Cells[1].Controls[0] as LinkButton;
if (btn != null) { sb.AppendFormat( "ButtonField: CommandArgument = '{0}', CommandName = '{1}', Text = '{2}'", btn.CommandArgument, btn.CommandName, btn.Text); }
// CheckBoxField CheckBox chk = row.Cells[2].Controls[0] as CheckBox;
if (chk != null) { sb.AppendFormat("CheckBoxField: Checked = '{0}'", chk.Checked); }
// HyperLinkField HyperLink hyp = row.Cells[3].Controls[0] as HyperLink;
if (hyp != null) { sb.AppendFormat( "HyperLinkField: NavigateUrl = '{0}', Text = '{1}'", hyp.NavigateUrl, hyp.Text); }
// LinkButton btn = row.FindControl("LinkButton1") as LinkButton;
if (btn != null) { sb.AppendFormat( "LinkButton: CommandArgument = '{0}', CommandName = '{1}' Text = '{2}'", btn.CommandArgument, btn.CommandName, btn.Text); }
// DataBoundLiteralControl DataBoundLiteralControl lit = row.Cells[5].Controls[0] as DataBoundLiteralControl;
if (lit != null) { sb.AppendFormat("DataBoundLiteralControl: Text = '{0}'", lit.Text); }
// Label Label lbl = row.FindControl("Label1") as Label;
if (lbl != null) { sb.AppendFormat("Label: Text = '{0}'", lbl.Text); }
// HtmlInputCheckBox HtmlInputCheckBox input = row.FindControl("HtmlInputCheckBox1") as HtmlInputCheckBox;
if (input != null) { sb.AppendFormat("HtmlInputCheckBox: Checked = '{0}'", input.Checked); }
pnlResults.Controls.Add(new LiteralControl(sb.ToString())); } }
protected void btnExtractAll_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { this.ExtractControls(row); } } }
|
| Author: chandramohan 30 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" PageSize="50"> <Columns> <asp:BoundField DataField="GatewayCode" HeaderText="Gateway Code" SortExpression="GatewayCode" /> <asp:BoundField DataField="GroupCode" HeaderText="Group Code" SortExpression="GroupCode" /> <asp:BoundField DataField="DestinationCode" HeaderText="Destination Code" SortExpression="DestinationCode" /> <asp:BoundField DataField="DestinationName" HeaderText="Destination Name" SortExpression="DestinationName" /> <asp:BoundField DataField="HotelsCode" HeaderText="Hotels Code" SortExpression="HotelsCode" /> <asp:BoundField DataField="HotelsName" HeaderText="Hotels Name" SortExpression="HotelsName" /> <asp:TemplateField HeaderText="Brands"> <ItemTemplate> <asp:DropDownList ID="ddlBrands" runat="server" DataSourceID="SqlDataSource2" DataTextField="BrandName" DataValueField="BrandCode" AutoPostBack="True" style="height: 22px"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=SVSWAQAR\SQLEXPRESS;Initial Catalog=SellOffPackages;Persist Security Info=True;User ID=test;Password=anahim" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [BrandName], [BrandCode] FROM [Brands] ORDER BY [BrandName]"> </asp:SqlDataSource> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Insert"> <ItemTemplate> <asp:Button ID="btnInsert" runat="server" CommandName="Insert" Text="Insert" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
|
| Author: Pooja 30 Jul 2008 | Member Level: Silver | Rating: Points: 1 |
hi friends,
Thank you so much for all of your reply...
Key-Shortcut.txt |
| Author: chandramohan 01 Aug 2008 | Member Level: Gold | Rating: Points: 1 |
Visual Basic Copy Code<%@ Page language="VB" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) ' If multiple buttons are used in a GridView control, use the ' CommandName property to determine which button was clicked. If e.CommandName = "Add" Then ' Convert the row index stored in the CommandArgument ' property to an Integer. Dim index As Integer = Convert.ToInt32(e.CommandArgument) ' Retrieve the row that contains the button clicked ' by the user from the Rows collection. Dim row As GridViewRow = CustomersGridView.Rows(index) ' Create a new ListItem object for the customer in the row. Dim item As New ListItem() item.Text = Server.HtmlDecode(row.Cells(2).Text) ' If the customer is not already in the ListBox, add the ListItem ' object to the Items collection of the ListBox control. If Not CustomersListBox.Items.Contains(item) Then CustomersListBox.Items.Add(item) End If End If End Sub Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) ' The GridViewCommandEventArgs class does not contain a ' property that indicates which row's command button was ' clicked. To identify which row's button was clicked, use ' the button's CommandArgument property by setting it to the ' row's index. If e.Row.RowType = DataControlRowType.DataRow Then ' Retrieve the LinkButton control from the first column. Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton) ' Set the LinkButton's CommandArgument property with the ' row's index. addButton.CommandArgument = e.Row.RowIndex.ToString() End If End Sub</script><html > <head runat="server"> <title>GridView RowCommand Example</title></head><body> <form id="form1" runat="server"> <h3>GridView RowCommand Example</h3> <table width="100%"> <tr> <td style="width:50%"> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" allowpaging="true" autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand" onrowcreated="CustomersGridView_RowCreated" runat="server"> <columns> <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="City" headertext="City"/> </columns> </asp:gridview> </td> <td style="vertical-align:top; width:50%"> Customers: <br/> <asp:listbox id="CustomersListBox" runat="server"/> </td> </tr> </table> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body></html>C# Copy Code<%@ Page language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple buttons are used in a GridView control, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked // by the user from the Rows collection. GridViewRow row = CustomersGridView.Rows[index]; // Create a new ListItem object for the customer in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[2].Text); // If the customer is not already in the ListBox, add the ListItem // object to the Items collection of the ListBox control. if (!CustomersListBox.Items.Contains(item)) { CustomersListBox.Items.Add(item); } } } void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e) { // The GridViewCommandEventArgs class does not contain a // property that indicates which row's command button was // clicked. To identify which row's button was clicked, use // the button's CommandArgument property by setting it to the // row's index. if(e.Row.RowType == DataControlRowType.DataRow) { // Retrieve the LinkButton control from the first column. LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0]; // Set the LinkButton's CommandArgument property with the // row's index. addButton.CommandArgument = e.Row.RowIndex.ToString(); } }</script><html > <head runat="server"> <title>GridView RowCommand Example</title></head><body> <form id="form1" runat="server"> <h3>GridView RowCommand Example</h3> <table width="100%"> <tr> <td style="width:50%"> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" allowpaging="true" autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand" onrowcreated="CustomersGridView_RowCreated" runat="server"> <columns> <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="City" headertext="City"/> </columns> </asp:gridview> </td> <td style="vertical-align:top; width:50%"> Customers: <br/> <asp:listbox id="CustomersListBox" runat="server"/> </td> </tr> </table> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body></html>
|