How to select a row without using OnSelectedIndexchanged in asp.net - asp.net

In an ASP.NET GridView control
Is it possible to select a row without using the OnSelectedIndexChanged event?
e.g. suppose I want to have a custom button on the GridView called "ProcessThis" for every row in the grid, and I want an OnClick method to then access the selected row values.
If that is not possible, is it then possible to alter the text and position of the select button generated by OnSelectedIndexChanged?

You could simply add a custom button to the GridView whose CommandName property is Select. Then use the SelectedIndexChanged event as usual.
Why do you say you don't want the SelectedIndexChanged event to fire? Note that this event will fire even if you change the selected index in code by calling the GridView.SelectRow method.

Yes it is possible to select a row without using the
OnSelectedIndexChanged event.
You have to use RowCommand event of GridView like
C#
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ProcessThis")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = Gridview1.Rows[index];
// Do what ever you want....
}
}
.aspx
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
onrowcommand="Gridview1_RowCommand">
<Columns>
<asp:ButtonField Text="Process This" CommandName="ProcessThis" />
</Columns>
</asp:GridView>
You have to assign CommandName in the button that you are using inside your GridView.
Hope you understand and works for you.

You can use the RowCommand event on the gridview for that purpose. It is triggered every time a button is clicked in the gridview.
If you use the DataKeys property in your markup you can fetch a specific ID for instance from the row of the button, in order to process a specific row. Examples are available in the docs.

Related

Determine the datatable index of the row clicked in GridView

I have a GridView in ASP.NET page. The GridView is bound to a dataset/datatabe. One of the columns of the grid is a command button and the gridview has method OnRowCommand (e.g. OnRowCommand="GridView_RowCommand") specified.
When user clicks on the button in the grid, the method GridView_RowCommand fires. I would like to find the index to the DataTable for the row where button was clicked. Note, that I am not looking for index to the GridView row but rather the index to the DataTable bound to the GridView.
Thank you in advance for your help.
You have a few options and it depends on the amount of data you are binding to the gridview, one you could save the dataset/datatable to Session[""] as you bind or you could retrieve the data from the database again once you have the unique id of the row. You could create the following on your gridview:
<asp:GridView ID="gvCustomer" runat="server"
AutoGenerateColumns="False" DataKeyNames="yourId" onrowcommand="gvCustomer_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkAction" runat="server" Text="Do Something" CommandName="yourEvent" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
then in the code behind for the RowCommand event have:
protected void gvCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "yourEvent")
{
var row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int rowId = Convert.ToInt32(gvCustomer.DataKeys[row.RowIndex]["yourId"]);
}
}
At this point you have the id which you could either query the database again or access the data source object that you save in to session before binding to the gridview
Or another alternative is:
<asp:LinkButton CommandArgument='<%#Eval("PrimaryKey")%>' />
Then you can get the arg with e.CommandArgument in the gvCustomer_RowCommand method

Event that fires when user clicks on the "New" button of an ASP.Net DetailsView

In an ASP.Net VB.Net code-behind file, can you tell me what event fires that I can use to trap when the user clicks on the "New" button of a DetailsView at the time the DetailsView shows blank items such as DropDownLists, TextBoxes etc?
I need use this event handler to pre-select a value in a DropDownList obtained from a variable.
Try the ItemCommand event. This is how to use it. I only know C# , but I think you get the hang of it.
Code behind:
protected void DetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "New")
{
// Your code here
}
}
Markup:
<asp:DetailsView ID="DetailsView" runat="server" OnItemCommand="DetailsView_ItemCommand" ...

Getting an Instance of Dropdown list when clicking update on GridView in Asp.Net

I have a GridView with more than 30 columns. Most are plain controls but for some I have added a template control (DropDownList, Calendar and CheckBox control). Here is the aspx code for the control in question
<asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
<ItemTemplate>
<asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
I wanted a dropdown to be shown on the column when a user clicked on Edit. So I add this code (and the above EditItemTemplate)
protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
{
string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);
gvData.SelectedIndex = e.NewEditIndex;
gvData.EditIndex = e.NewEditIndex;
gvData.DataBind();
BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
}
In the above code I am getting the current available and passing it to another method so that when the dropdown is displayed the selected index can be shown accurately. After this I do a change on the dropdownlist and click on the "Update button" on the GridView and the following event is triggered
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowEditIndex = e.RowIndex;
GridViewRow gRow = gvData.Rows[rowEditIndex];
DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work
ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work
ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
gvData.EditIndex = -1;//this works and the text boxes disappear
gvData.DataBind();//this works and the old data shows up on the gridview
}
I am curious as to how to do an update on a Grid where I have the binding is runtime.
Actually in the markup, you have given ID of dropdownlist as ddlField1 and in codebehind, you are reffering it as ddlConstructionArea. Is this is what causing the update not to function?
The problem was with the way the grid was being bound. I had written code to refresh the grid on page load and anytime I clicked on the Edit button the page was being refreshed and the grid binding was getting triggered instead of the updating event. I removed the code refreshing the grid on page load and put it in the places where it is needed and the order of events were getting triggered the way I would want it to and the update worked perfectly without a problem.

Button's cause validation property is set to false but still it fires validation

I have a button in EmptyTempletField of gridview for which causevalidation property is set to false.
But when I try to add row in gridview from empty template field by clicking on that button, the button is not firing row command event. Moreover, it fires the validation. I have few validations which are grouped. But this button fires all validations irrespective of group. If I click on the button second time, then it fires the row command event. I can't understand what is happening..
Why button fires validation which it is not supposed to fire...???
It is not clear what is exaclty happening with your code. Anyway, it should be something similar to this
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDeletePicture" runat="server" CommandName="YOURCOMMAND" Text="command" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "YOURCOMMAND")
{
//your code
}
}
And read this article about GridView.RowCommand, which is helpful
Hope this helps

writing code in button's click event in gridview

i have added a button control in gridview and i want to redirect
to the another page on button's click event,
and also i want to access the values of selected row in a gridview and show that values on other page on button
click event which is inside the gridview,, can anybody plz help me out in this .
this should be helpful: http://msdn.microsoft.com/en-us/library/bb907626.aspx
for accessing values inside RowCommand event find your row first (by index):
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = MyGridView.Rows[index];
and then controls inside it which holds the values you need:
Label MyLabel= (Label)row.FindControl(MyLabel);
after that all you need to do is to transfer to your page and send values from your controls with it (do that with querystring):
Server.Transfer("MyPage.aspx?value="+MyLabel.Text)
here you can read more about gridview RowCommand event: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
You need to add either a ButtonField or a TemplateField with a button inside and then you can add your code to the RowCommand and use the e.CommandName and e.CommandArgument
To redirect you can use Response.Redirect(address)
To see the values it depends where in the program you want to do it. If during databinding you can use the dataitem("column"). Outside of databinding you can go through the Gridview.Rows collection for the row you want and then look at the row.Cells(columnNumber).Text to see the value.
we have an event in gridview called, gridview rowcommand event, in that you can write the code for redirecting to another page using button. First you have to bind the value of which you want to pass in button property called commandargument.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Redirect("abc.aspx?id=" + e.CommandArgument);
//here id means passing the value using querystring to another page.
}
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Name" DataField="fullname" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" Text="Delete" OnClick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
// This focuses the current row where the button lies. you can perform all events of server-controlls
//instead of Button e.g LinkButton, Dropdownlist index changes...
protected void btnDelete_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvRow = (GridViewRow)btn.NamingContainer;
string id = gvRow.Cells[0].Text;
string name = gvRow.Cells[1].Text;
//use these values in query string or any logic you prefer for cross page posting
Response.Redirect("your URL ?id=" + id + "&name=" + name);
}

Resources