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("oncl ick", e.Row.FindControl("btnSele ctRow").Cl ientID & ".click();");
Operator & cannot be applied to operands of type string and string.
Someone on a different message board suggested
protected void GridView1_SelectedIndexCha nged(objec t 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_ RowDataBou nd" AllowSorting="true" onselectedindexchanged="Gr idView2_Se lectedInde xChanged"
>
<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>
</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(obj ect sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
//e.Row.Attributes.Add("on click", e.Row.FindControl("btnSele ctRow").Cl ientID & ".click();");
e.Row.Attributes.Add("onmo useover", "Highlight_On(this);");
e.Row.Attributes.Add("onmo useout", "Highlight_Off(this);");
e.Row.Attributes["OnClick" ] = ClientScript.GetPostBackEv entReferen ce(this, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
}
}
protected void GridView2_SelectedIndexCha nged(objec t sender, EventArgs e)
{
int colid = int.Parse(GridView2.Column s[1].ToStr ing());
int contid = int.Parse(GridView2.Select edIndex.To String());
if (colid == contid)
{
}
}
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("oncl
Operator & cannot be applied to operands of type string and string.
Someone on a different message board suggested
protected void GridView1_SelectedIndexCha
{
Label1.Text = String.Format("You selected row '{0}'.", GridView1.SelectedDataKey[
}
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_
>
<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>
</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(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
//e.Row.Attributes.Add("on
e.Row.Attributes.Add("onmo
e.Row.Attributes.Add("onmo
e.Row.Attributes["OnClick"
e.Row.Style["cursor"] = "hand";
}
}
protected void GridView2_SelectedIndexCha
{
int colid = int.Parse(GridView2.Column
int contid = int.Parse(GridView2.Select
if (colid == contid)
{
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you using the '&' sign to concatenate two strings of text? If so does changing this to '+' solve your problem?
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(obj ect 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("onmo useover", "Highlight_On(this);");
e.Row.Attributes.Add("onmo useout", "Highlight_Off(this);");
e.Row.Style["cursor"] = "hand";
e.Row.Attributes.Add("onCl ick", Page.ClientScript.GetPostB ackClientH yperlink(G ridView2, "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.Register ForEventVa lidation(n ew System.Web.UI.PostBackOpti ons(GridVi ew2, "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_ RowDataBou nd" AllowSorting="true" onrowcommand="GridView2_Ro wCommand1"
>
<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>
</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.
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(obj
{
// 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("onmo
e.Row.Attributes.Add("onmo
e.Row.Style["cursor"] = "hand";
e.Row.Attributes.Add("onCl
}
}
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.Register
}
// 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_
>
<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>
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks alot for your help..this was a bear..you pointed me in the right direction and were very helpful.