Archive for the ‘GridView’ Category
Error control ”of type ‘GridView’ must be placed inside a form tag with runat=server
Posted by: admin in GridView, Uncategorized on October 18th, 2011
How to select a row in GridView
-
<asp:GridView ID="GridViewCategory" runat="server" AutoGenerateColumns="False"
-
DataKeyNames="CategoryID"
-
EmptyDataText="There are no data records to display."
-
CssClass = "GridViewStyle"
-
AlternatingRowStyle-CssClass="alt"
-
PagerStyle-CssClass="pgr"
-
SelectedRowStyle-CssClass="selected"
-
Width="100%" onrowdatabound="GridViewCategory_RowDataBound"
-
>
-
<Columns>
-
<asp:TemplateField HeaderText="ID">
-
<ItemTemplate>
-
<asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" CommandArgument='<%# Bind("CategoryID") %>' Text='<%# Bind("CategoryID") %>'></asp:LinkButton>
-
</ItemTemplate>
-
<ItemStyle Width="50px" />
-
</asp:TemplateField>
-
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
-
</Columns>
-
<PagerStyle CssClass="pgr" />
-
<AlternatingRowStyle CssClass="alt" />
-
</asp:GridView>
Code behind:
-
protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
-
{
-
if (e.Row.RowType == DataControlRowType.DataRow)
-
{
-
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[1];
-
-
e.Row.Attributes.Add("onClick", ClientScript.GetPostBackClientHyperlink(lb, ""));
-
e.Row.Attributes.Add("onMouseClick", "javascript:this.style.cursor='pointer';");
-
e.Row.Attributes.Add("onMouseOut", "javascript:this.style.cursor='normal';");
-
}
-
}
or
if you did not put a SELECT command button in the gridview, you can use the following code to mimic the Select function
-
protected void GridViewCategory_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.GridViewCategory, "Select$" + e.Row.RowIndex);
-
}
-
}