Link to home
Start Free TrialLog in
Avatar of Prysson
Prysson

asked on

Select Gridview row without select button

I am looking for a solution to allow me to select a gridview row without having a select button.

I looked at this solution

https://www.experts-exchange.com/questions/21734715/Selecting-a-Gridview-row.html

but when I insert the code
e.Row.Attributes.Add("onclick", e.Row.FindControl("btnSelectRow").ClientID & ".click();");


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


Someone on a different message board suggested
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
            Label1.Text = String.Format("You selected row '{0}'.", GridView1.SelectedDataKey["ID"]);
      }

BUt I dont know how to get that event handler to fire without a "Select" button.



This is my current code for 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>


And here is the code in teh code behind

 protected void Page_Load(object sender, EventArgs e)
        {
            DataTable contdt = contbll.GetContactsData_();
            DataView contdv = new DataView(contdt);

            this.GridView2.DataSource = contdv;
            this.GridView2.DataBind();
           
        }

        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)
        {
            int colid = int.Parse(GridView2.Columns[1].ToString());
            int contid = int.Parse(GridView2.SelectedIndex.ToString());
            if (colid == contid)
            {
            }
 
        }
ASKER CERTIFIED SOLUTION
Avatar of TheMegaLoser
TheMegaLoser
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you using the '&' sign to concatenate two strings of text?  If so does changing this to '+' solve your problem?
Avatar of Prysson
Prysson

ASKER

OK I am very close here..

Code Behind
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable contdt = contbll.GetContactsData_();
                DataView contdv = new DataView(contdt);

                this.GridView2.DataSource = contdv;
                this.GridView2.DataBind();
            }
           
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Add the javascript that will do the callback when a user clicks on a row
            // Header and footer rows should not have callbacks
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "Highlight_On(this);");
                e.Row.Attributes.Add("onmouseout", "Highlight_Off(this);");
                e.Row.Style["cursor"] = "hand";
                e.Row.Attributes.Add("onClick", Page.ClientScript.GetPostBackClientHyperlink(GridView2, "Select$" + e.Row.RowIndex.ToString()));
            }
        }

        protected override void Render(HtmlTextWriter htmlTextW)
        {
            // .NET will refuse to accept "unknown" postbacks for security reasons. Because of this we have to register all possible callbacks
            // This must be done in Render, hence the override
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                Page.ClientScript.RegisterForEventValidation(new System.Web.UI.PostBackOptions(GridView2, "Select$" + i.ToString()));
            }
            // Do the standard rendering stuff
            base.Render(htmlTextW);
        }


ASPX Code for GridView

            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
                    DataKeyNames="ContactID" Width="200px"
                    onrowdatabound="GridView2_RowDataBound" AllowSorting="true" onrowcommand="GridView2_RowCommand1"  
                    >
               
            <Columns>

                <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>
                        </div>



This code does almost everything I need it to...The only thing that isnt happeneing is the SelectedIndexChanged event handler isnt firing..is there a way using the approach shown here to fire the event handler...or perhaps some other easy way to fire of a method on the row click so I can prcoess the selection of the row.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Prysson

ASKER

Thanks alot for your help..this was a bear..you pointed me in the right direction and were very helpful.