Archive for the ‘GridView’ Category

Error control ”of type ‘GridView’ must be placed inside a form tag with runat=server

Go here

No Comments


How to select a row in GridView
  1. <asp:GridView ID="GridViewCategory" runat="server" AutoGenerateColumns="False"
  2.  DataKeyNames="CategoryID"
  3.  EmptyDataText="There are no data records to display."
  4.  CssClass = "GridViewStyle"
  5.  AlternatingRowStyle-CssClass="alt"
  6.  PagerStyle-CssClass="pgr"
  7.  SelectedRowStyle-CssClass="selected"
  8.  Width="100%" onrowdatabound="GridViewCategory_RowDataBound"
  9.  >
  10.  <Columns>
  11.   <asp:TemplateField HeaderText="ID">
  12.    <ItemTemplate>
  13.     <asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" CommandArgument='<%# Bind("CategoryID") %>' Text='<%# Bind("CategoryID") %>'></asp:LinkButton>
  14.    </ItemTemplate>
  15.      <ItemStyle Width="50px" />
  16.   </asp:TemplateField>
  17.   <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
  18.  </Columns>
  19.  <PagerStyle CssClass="pgr" />
  20.  <AlternatingRowStyle CssClass="alt" />
  21. </asp:GridView>

Code behind:

  1. protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3.  if (e.Row.RowType == DataControlRowType.DataRow)
  4.  {
  5.   LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[1];
  6.  
  7.   e.Row.Attributes.Add("onClick", ClientScript.GetPostBackClientHyperlink(lb, ""));
  8.   e.Row.Attributes.Add("onMouseClick", "javascript:this.style.cursor='pointer';");
  9.   e.Row.Attributes.Add("onMouseOut", "javascript:this.style.cursor='normal';");
  10.  }
  11. }

or

if you did not put a SELECT command button in the gridview, you can use the following code to mimic the Select function

  1. protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3.  if (e.Row.RowType == DataControlRowType.DataRow)
  4.  {
  5.   e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
  6.   e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
  7.  
  8.   e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridViewCategory, "Select$" + e.Row.RowIndex);
  9.  }
  10. }

, , ,

No Comments



SetPageWidth