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
0
cvramesh84
5/3/2007 12:38:30 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

15 Replies
3057 Views

Similar Articles

[PageSpeed] 58

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 event handler, so add this to your gridview's source (OnRowCreated="GridView1_RowCreated") or just set up a handler through the gridviews Events Property window.

The event handler will look something like this:

    int RowCount = 0;
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        // get the row index, add onclick attribute for javascript:__doPostBack('GridView1','Select$0')
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + ((GridView)sender).ID + "','Select$" + (RowCount - 1).ToString() + "')");
            e.Row.Attributes.Add("onmouseover", "JavaScript:this.style.cursor='hand';");
            
        }
        RowCount++;
    }
 
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

http://pixelsyndicate.com/ps/
0
pixelsyndicate
5/3/2007 3:06:25 PM

Hello,

I tried the following which I found on the web.

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);
        }
    }

But got the following error:

Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

If I set pages enableEventValidation="false"

the indexchanged event won't fire.

What can I do now? 


 

0
bruceq
5/4/2007 9:38:41 PM

 Hi:

  Please have a look at this sample:

the JS:

<head>

<script type="text/javascript">
    function Clk(t)
    {
    document.getElementById("Hidden1").value=t;
    form1.submit();
    }
 </script>

</head>

controls in aspx:

 

<form id="form1" action="Default.aspx" runat="server">

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <input id="Hidden1" name="Hidden1"  value="false" type="hidden" />
  <asp:GridView AllowPaging="true" PageSize="5" ID="GridView3" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView3_RowDataBound" OnPageIndexChanging="GridView3_PageIndexChanging">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="Label5" runat="server" Text='<%# Eval("theName") %>'></asp:Label>
    </ItemTemplate></asp:TemplateField></Columns></asp:GridView>

in code behind:

  protected void Page_Load(object sender, EventArgs e)
    {
     
        this.GridView3.DataBind();

//others

}

 protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (Request.Form["Hidden1"] != null && e.Row.RowIndex == Convert.ToInt32(Request.Form["Hidden1"]))
                this.Label1.Text = ((Label)e.Row.FindControl("Label5")).Text;          
            e.Row.Attributes.Add("onclick", "Clk(" + e.Row.RowIndex + ")");
            e.Row.Style["cursor"] = "hand";
        }


    }

 

If it doesn't work, please inform us.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
5/8/2007 3:36:15 AM

Actually I changed to the following line and it works.

e.Row.Attributes.Add(

"onClick", Page.ClientScript.GetPostBackClientHyperlink(selectButton, String.Empty))

Thanks for your help! 

 

0
bruceq
5/8/2007 5:06:18 PM

Which method do you put this code in?

-smc


~ Remember To Mark The Posts Which Helped You As The ANSWER ~
0
smcirish
5/8/2007 5:30:54 PM

I got the following from web

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);
        }
    }

Replace e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);

with the following:

e.Row.Attributes.Add("onClick", Page.ClientScript.GetPostBackClientHyperlink(selectButton, String.Empty))

Please note the example is c#, my code is vb

0
bruceq
5/8/2007 6:05:20 PM

Dear All,

 Put the following line in the webconfig If you face this runtime error (( Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.  ))

 <pages enableEventValidation="false"></pages>

 

Thanks.


0
ali
10/2/2007 10:40:30 AM
how would you do this on a DataList?
0
DJVisual
10/31/2007 7:15:28 PM

Dear Br,

Could you clear waht you have submitted, please!

You can writh down your code to help you out...

 

Thanks a lot.


0
ali
11/3/2007 6:00:52 AM

Could someone please help me out.

 I have a grid in which i have displayed all the tests taken by a student. when the students clicks on a particular test he/she should be taken to a page to view the details of that test via the testid that is in the grid.

i have already implemented the selection of a row without using the select button. can someone help me out as to how to go from there.

 Any help is apprieciated. Thanks

0
theapocalypse
3/12/2008 1:23:06 PM

Hi theApocalypse,

With the row selection in your GridView, you should now have the ability to either pass the selected row's key to another page, or (as I often do), simply present a user with a formview (on the same page) who's datasource uses as it's parameter the selected value of the gridview (the datasource parameter will be of a type of Controlparameter which when using gridviews uses by default the selected row's key).

With the former method, the formview will be hidden until a row is selected in your gridview.


"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

http://pixelsyndicate.com/ps/
0
pixelsyndicate
3/12/2008 1:45:25 PM

Thank you very much for replying. 

I am currently making do with a not so clean method.

 I currently have 2 grid views on the page and am taking the value for the 2nd grid (examid) from the first grid.

 I would rather have it displayed on another page. Unfortunately i am new to this and have no clue as to how to do that. Embarrassed

you think you could tell me how to post the exam id which is the datakey on to a new page which in turn would run a select statement on page load and display it in a datagrid there.

 

0
theapocalypse
3/12/2008 1:57:19 PM

 Do any one have a working copy of this code.... Plz share

~Jagadeesh 


www.mReach.Net ...for Online Live TV Channels, Radio's and WebCams Live

Online Live TV Channels, Radio's and WebCams
0
Jagadeesh
3/12/2008 2:02:44 PM

This is how you would pass that value to another page: in the SelectedIndexChanged event handler, do the following...

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView gv = ((GridView)sender);
        Response.Redirect("someOtherPage.aspx?ID=" + gv.SelectedValue);

        // on the someOtherPage.aspx, retrieve the ID like so
        // string theSelectedValue = Request.QueryString["ID"].ToString();
    }
 
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

http://pixelsyndicate.com/ps/
0
pixelsyndicate
3/12/2008 2:43:16 PM

Thank you so much for your help. :)

0
theapocalypse
3/12/2008 3:23:42 PM
Reply:

Web resources about - How to select a row without select button - asp.net.presentation-controls

VIDEO: Rory tries out new tech at CES
The BBC's technology correspondent Rory Cellan-Jones tries out several new gadgets ahead of CES's show floors opening to view.

Ezra Klein Cannot Explain Donald Trump
On Tuesday, Ezra Klein, the editor-in-chief of Vox.com, a website that largely traffics in “explainers”—its Twitter profile commands readers, ...

Twitter appears ready to expand beyond 140-character tweets
SAN FRANCISCO (AP) — Twitter appears ready to loosen its decade-old restriction on the length of messages in a bid to make its service more appealing ...

Hasbro is adding Rey to Star Wars Monopoly after criticism from fans
Rey is coming to Star Wars Monopoly, toymaker Hasbro confirmed, after the company received sustained criticism for excluding her from its latest ...

China's Xinhua: North Korea nuclear test runs counter to goal of denuclearisation
Daily Times China's Xinhua: North Korea nuclear test runs counter to goal of denuclearisation Yahoo News BEIJING (Reuters) - China's Xinhua ...

Scott Walker Refuses To Even Consider A Pardon For ‘Making A Murderer’ Convict Steven Avery
The Netflix original documentary series Making a Murderer has been gaining a lot of attention for making a compelling case for the innocence ...

Saudi Arabia's Fragile State
WASHINGTON "Fragile" is the word that journalist Karen Elliott House used to describe Saudi Arabia in her 2012 book about the country. "Observing ...

Blanca Hernandez: 13-Year-Old Killed In Car Crash When Older Brother Distracted By Cell-Phone Ran A Red ...
A New Jersey family is mourning the loss of their 13-year-old daughter Blanca Azucena Hernandez Vargas of Toms River, who was killed in a car ...

Siddhartha Dhar was arrested before joining ISIS in Syria
Siddhartha Dhar, 32, was arrested in September 2014 and order to surrender his passport. But police only realised he had absconded when they ...

The most popular ‘Saturday Night Live’ sketches of 2015
With each season of Saturday Night Live consisting of about 20-22 episodes, we're already about half way through the show's 41st season. Since ...

Resources last updated: 1/6/2016 6:59:03 AM