want to Find Control in gridview on RowCommand event in asp.net

Posted by Navalemanoj0405 under ASP.NET on 5/10/2011 | Points: 10 | Views : 66974 | Status : [Member] | Replies : 9
Dear All

I want to Find Control in gridview on RowCommand event in asp.net
if it Is possible Please revert me




Responses

Posted by: Prabhakar on: 5/10/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi avalemanoj0405


i am Provide Code For Find Control ... Check it now. . . it's a use full for you . . on RowDataBound


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblPrice = (Label)e.Row.FindControl("lblPrice");
Label lblUnitsInStock = (Label)e.Row.FindControl("lblUnitsInStock");

decimal price = Decimal.Parse(lblPrice.Text);
decimal stock = Decimal.Parse(lblUnitsInStock.Text);

totalPrice += price;
totalStock += stock;

totalItems += 1;
}

if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice");
Label lblTotalUnitsInStock = (Label)e.Row.FindControl("lblTotalUnitsInStock");

lblTotalPrice.Text = totalPrice.ToString();
lblTotalUnitsInStock.Text = totalStock.ToString();

lblAveragePrice.Text = (totalPrice / totalItems).ToString("F");
}
}


Best Regard's
Prabhakar

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Navalemanoj0405 on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
thanks for reply prabhakar
but i want to find control on RowCommand of gridview.

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 5/10/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
ok Navalemanoj0405

check this code. . if u agree mark as Answer . .

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName.Equals("Update"))
{
int state = 0;
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = GridView1.Rows[index];
DropDownList lstState = (DropDownList)row.FindControl("StateID");
state = int.Parse(lstState.SelectedValue.ToString());
ObjectDataSource1.UpdateParameters.Add("StateID", state.ToString());
}
}


Best Regard's
Prabhakar

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Navalemanoj0405 on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
hey prabhakar but what should be there in CommandArgument in gridview ?


Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 5/10/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi

here the Command-argument To determine the index of the row that raised the event, use the CommandArgument property of the event argument that is passed to the event.

Best Regard's
Prabhakar

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Navalemanoj0405 on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
CommandArgument giving me empty string
What i should do?

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 5/10/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi..

if u click on Add or update button so CommandArgument given not empty . . Show ur code else . . Check this link . .

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx#Y2793

Best Regard's
Prabhakar

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
You can find it this way

void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Control ctrl = e.CommandSource as Control;
if (ctrl != null)
{
GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
//Now you can find control on the row where event is raised by using FindControl method.
//Control findme=_currenrtrow.FindControl("ControlID to Find in that row");
}
}

Thanks,
Debata


Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 5/19/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Refer this link,

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.findcontrol.aspx

Regards,
Susan

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response