Help selecting geridview row without select button.

I am trying to get a gridview row to be selected without havigna select button.

 I have looked all over and come up with this notion.

 In the Gridview I do the following.

                 <asp:TemplateField HeaderText="ContactID" Visible="False">
                    <ItemTemplate>
                        <asp:Button style="display:none" ID="btnSelectRow" runat="server" Text="Select"></asp:Button>
                    </ItemTemplate>
                </asp:TemplateField>

 The I try to add this to the RowDataBound Event

e.Row.Attributes.Add("onclick", e.Row.FindControl("btnSelectRow").ClientID & ".click();");

 

But I get an error

 

Operator & cannot be applied to operands of type string and string.

 

Any ideas what I need to change here.

0
Prysson
8/5/2008 3:25:00 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
723 Views

Similar Articles

[PageSpeed] 14

Here's an example for you:

ASPX 

<%@ page autoeventwireup="true" codefile="SelectRowWithoutSelectCommand.aspx.cs"
	inherits="GridView_SelectRowWithoutSelectCommand" language="C#" masterpagefile="~/MasterPages/Default.master"
	title="GridView: Select Row Without Select Command" %>

<asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
	<asp:label id="Label1" runat="server" />
	<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" datakeynames="ProductID,ProductName"
		datasourceid="SqlDataSource1" onselectedindexchanged="GridView1_SelectedIndexChanged"
		style="margin-top: 12px;">
		<columns>
			<asp:boundfield datafield="ProductID" headertext="ProductID" readonly="True" sortexpression="ProductID" />
			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
		</columns>
	</asp:gridview>
	<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
		selectcommand="SELECT * FROM [Products]">
	</asp:sqldatasource>
</asp:content>

CODE-BEHIND 

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GridView_SelectRowWithoutSelectCommand : Page
{
	protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
	{
		Label1.Text = String.Format("You selected product '{0}'.", GridView1.SelectedDataKey["ProductName"]);
	}

	protected override void Render(HtmlTextWriter writer)
	{
		const string onMouseOverStyle = "this.className='GridViewMouseOver';";
		const string onMouseOutStyle = "this.className='{0}';";
		
		foreach (GridViewRow gvr in GridView1.Rows)
		{
			gvr.Attributes["onmouseover"] = onMouseOverStyle;
			gvr.Attributes["onmouseout"] = String.Format(
				onMouseOutStyle,
				this.GetRowStyleCssClass(gvr.RowState));
			gvr.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(
				GridView1, 
				String.Concat("Select$", gvr.RowIndex),
				true);
		}
		
		base.Render(writer);
	}

	private string GetRowStyleCssClass(DataControlRowState state)
	{
		if ((state & DataControlRowState.Edit) > 0)
		{
			return GridView1.EditRowStyle.CssClass;
		}
		else if ((state & DataControlRowState.Selected) > 0)
		{
			return GridView1.SelectedRowStyle.CssClass;
		}
		else if ((state & DataControlRowState.Alternate) > 0)
		{
			return GridView1.AlternatingRowStyle.CssClass;
		}
		else
		{
			return GridView1.RowStyle.CssClass;
		}
	}
}

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
8/5/2008 3:30:07 PM

I dont use sqldatasources. INstead I bind my gridview programmatically...This is how I set up my Gridview.

 

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"

DataKeyNames="ContactID" Width="200px"

onrowdatabound="GridView2_RowDataBound" AllowSorting="true" onselectedindexchanged="GridView2_SelectedIndexChanged"

>

 

<Columns>

<asp:CommandField SelectText ="Select" ShowSelectButton="true" ItemStyle-CssClass = "HiddenColumn" HeaderStyle-CssClass ="HiddenColumn" />

<asp:TemplateField HeaderText="ContactID" Visible="False">

<ItemTemplate>

<asp:Label ID="lblContactID" runat="server" Text='<%# Bind("ContactID")%>' ></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="LastName">

<ItemTemplate>

<asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName") %>' ></asp:Label>

</ItemTemplate>

<HeaderStyle Font-Underline="True" HorizontalAlign="Left" />

</asp:TemplateField>

<asp:TemplateField HeaderText="FirstName">

<ItemTemplate>

<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName")%>' ></asp:Label>

&nbsp;&nbsp;

</ItemTemplate>

<HeaderStyle Font-Underline="True" HorizontalAlign="Left" />

</asp:TemplateField>

<asp:TemplateField HeaderText="MI">

<ItemTemplate>

<asp:Label ID="lblMiddleInit" runat="server" Text='<%# Bind("MiddleName") %>'></asp:Label>

</ItemTemplate>

<ControlStyle Width="100px" />

<HeaderStyle Font-Underline="True" HorizontalAlign="Left" />

</asp:TemplateField>

</Columns>

</asp:GridView>

 

 

 

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

//e.Row.Attributes.Add("onclick", e.Row.FindControl("btnSelectRow").ClientID & ".click();");

e.Row.Attributes.Add("onmouseover", "Highlight_On(this);");

e.Row.Attributes.Add("onmouseout", "Highlight_Off(this);");

e.Row.Attributes["OnClick"] = ClientScript.GetPostBackEventReference(this, "Select$" + e.Row.RowIndex);e.Row.Style["cursor"] = "hand";

}

}

 

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)

{

Label1.Text = String.Format("You selected user '{0}'.", GridView1.SelectedDataKey["ContactID"]);

{

}

 

 

However the SelectedIndexChanged event handler isnt firing.

 

}

0
Prysson
8/5/2008 4:44:00 PM

Just had to wire the event handler to the control..

0
Prysson
8/6/2008 12:59:21 PM
Reply:

Similar Artilces:

How to select a row without select button
Hi. I am use one Dataview in My Web site Now i Have 2 boubts , 1.How to get a Datagrid item values(All row values)in pageLoad event 2.How to select a row without using Select Button, i want to select a row on mouse click event Please give me a soulation TO SELECT A ROW ONCLICK: Firstly, you will need to enable row selections for your datagrid. this will place the proper javascript in your page for what is to come.  This will place a select button on your page, but you can convert that column into a template and make the linkbutton visible=false. you need a OnRowCreated ev...

Select a gridview row without a select button
Hi Does anyone have a vb code sample of how to select a gridview row without having to click on a select button?   Thanks    ASPX <asp:label id="Label1" runat="server" /> <asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" datakeynames="ProductID,ProductName" datasourceid="SqlDataSource1" style="margin-top: 12px;"> <columns> <asp:boundfield datafield="ProductID" headertext="ProductID" readonly="True" sort...

Selectable Row in a DataGrid without the SELECT button
Does anyone know how to code the DataGrid so that when click on the data row, that row  must be selected just like a SELECT button does, but without the button?   Thanks, Check this out: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwebteam/html/webteam11062001.asp (Section: The Works and Days of Hands)Remove "_no_spam" for sending email...

Iwant to select a row and column in gridview with select radiobutton. but No checked change event.below put a button .if click that button to seleced row values will select
I want to select a row and column in gridview using radiobutton in gridview . if which radio button is select that row will select and i want that row column values in outside individual textboxes of next page.  but in that only single radiobutton will select and no post back="true" bcz iam not using checked change property. i inserted button below gridview. if i click the that selected row and columns values are stored into textboxes of next page im using this code .but im getting one column in that row. but i want all columns in that selected row so plz repla...

Problem in GridView Row Selection without using Select button
Hi All, I want to Select a Gridview Row without using Select button, So i have used following Javascript in Row DataBound Event.... e.Row.Attributes["onmousedown"] = ClientScript.GetPostBackClientHyperlink(this.grdDocs, "Select$" + e.Row.RowIndex,false); It is working fine. But the page is refreshing every time when i select a row. how can i avoid that....? Using an UpdatePanel is one option, off the top of my head,C# <---> VB.Net Translator I am not using Ajax here. Can't i do with Javascript...? Row selection in a Grid is only handled server-side...

How can I select a row without clicking "Select" button
Is it possible to select a row by clicking anywhere in the row of the data grid. What I would like to achieve is to put the datagrid in edit mode by cliking anywhere in the datagrid row. Thanks for any help, Remzi Hi! This link to MSDN should help ya out. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp mingz p/s the very useful link above was originally posted by colt in a different thread so thank him and not me :) http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=3383...

how to update a row using a seperate update button not command button without selecting the row?
How to update a row using a seperate update button not command button without selecting a row? Simple question but dificult to do. The reason I ask this is that when a user clicks 'Update' I want each row to update the database. Please note I can't use findcontrol as their are no headers This is my code so far: protected void UpdateButton_Click(object sender, EventArgs e){    foreach (GridViewRow row in CellGridView.Rows)     {          string stext = CellGridView.Rows.ToString();     &nb...

How do I select a row using a button that isnt the select command button
I have an 'UP' and 'DOWN' button by the side of a gridview. When i click 'UP' or 'DOWN' the row swaps places with the row above or below which is correct. However i want the row that moved to continue being selected so that i can keep pressing the 'UP' button to move the row rather than having to select the row click 'UP' then select the row then click 'UP' and so on. I need a line of code that changes the selected index up or down when i press the 'UP' or 'DOWN' button. But the events available to me when clicking on...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

How to display the more than one row with items and buttons in gridview control. when i select three items at same time from categories(Fruits, Apples, Gifts). The selected items are displayed in g
Hi,when i select three items at same time from categories(Fruits, Apples, Gifts). The selected items are displayed in gridview.Here i would like to change this way "when i click more than one category at same time. it should be display selected item and button."For example: if i have to select Fruits, Apples from categories.  it should be display like 1st "row have Fruits and In 2nd row have Apples and button" in gridview.the button have some event.How can i do it? Please can anyone answer me.With kind requestThanks your query is very raw..can u details it out...Pl...

Gridview paging enabled. Has checkbox to select the row . If user select the checkbox on one page and move to 2nd page to select the rows there also to export the selected rows to excel. But doing so
HiI am using Gridview and that has paging enabled. Using checkbox to select the particular row of the gridview to export the selected rows to the excel . Everything is working fine except for one case.Let user selected the "check" box for, five rows which user would like to export to excel (two of rows on page 1 of results and 3 of them on page 2 of results), however it only exports the 3 row details from page 2, because it seems to de-select the 2 rows from page 1 when I move to page 2 and vice versa. The code i am using is as :protected void BtnExcel_Click(object sender, EventArg...

Selecting records in GRIDVIEW without SELECT button ??
Hi , i was wondering, how to make the SELECT link button that comes along with the GRIDVIEW when we enable SELECTION , disappear and make the records selectable ?, so that insted of CLICKING on the SELECT button for selecting the records , i could directly select the records !. Now i get somthing like this.. for selection.. Col 1   COL2Select nameSelect AgeSelect salary What i want is..this for selection... COL1nameagesalary Thanks in advance ! Convert your data column to a template field. Replace the label with a LinkButton, bind the text and set the commandname o...

how can i select a row in a gridview without the select link
how can i select a row in a gridview without the select link, i mean, by click in the row no in the select link?i know that it is not a custom control but i couldnt do that yet. Hi, it is a custom control. ;-) Please see my blog post: Developing a row-clickable GridViewhttp://aspadvice.com/blogs/joteke/archive/2006/01/07/14576.aspxThanks,Teemu KeiskiFinland, EU...

Select multiple rows in GridView without using a Select Command ?
Hello, Does anyone knows a way to select multiple rows in GridView without using a Select command, control button or a checkbox control? Thank you very much. olaamigos:Does anyone knows a way to select multiple rows in GridView without using a Select command, control button or a checkbox control?  I believe that this is what you are looking for:GridView Clickable Rows Regards,Vinz"Code, Beer and Music" that's my way of being a programmer!How to get your Forum Question Answered | Blog | CodeASP.NET Thanks Vinz for replying, The  sample in the "GridView Cl...

Web resources about - Help selecting geridview row without select button. - asp.net.presentation-controls

Consultation Document on the Methods for Selecting the Chief Executive and for Forming the LegCo in 2012 ...
The " Consultation Document on the Methods for Selecting the Chief Executive and for Forming the LegCo in 2012 " was a document published on ...

Voting Machine Caught on Camera Casting Ballot for Democrat when Selecting Republican - YouTube
**UPDATE.... I've heard many reports that this is "just a calibration issue." I will say this... when I went to page 2 and began voting for city ...

Selecting The Right Jeans
Fit, style, cut, colour and wash all factor into the pursuit for great denim.

Clock ticking on selecting successor to Leigh Clifford at Qantas
With Qantas on course to chalk up its largest annual profit since 2008, the question now is whether chairman Leigh Clifford is looking to hang ...

Why are the reserves selecting the team?
Backbenchers are the reserves of the parliamentary system. They are newcomers.

Jackie French: Why selecting the right place for your plants is important
The secret of growing anything easily is to choose the best spot for what you want to grow.

Liberals begin 'super weekend' of selecting delegates
A race for the top job in the federal Liberal party gained momentum on Friday as rank and file party members began the selection process for ...

Boston Marathon bombing trial: Judge, lawyers face challenges in selecting jurors - CTV News
As the second phase of jury selection began in the federal death penalty trial of Boston Marathon bombing suspect Dzhokhar Tsarnaev, it immediately ...

Selecting cabinet one of first orders of business for Trudeau
One of the first orders of business for any new prime minister is picking a cabinet. Who is likely to make the cut?

Agile Analysis, Self-Selecting Teams, TDD & BDD in Methods & Tools Winter 2014 issue
... for software developers, testers and project managers – has just published its Winter 2014 issue that discusses Agile Analysis, Self-Selecting ...

Resources last updated: 11/28/2015 5:50:43 PM