minamorsali
سه شنبه 19 آذر 1392, 16:29 عصر
سلام دوستان
من یک صفحه دارم که گرید ویو آن گروه مقالات رو نشون میده و یک تکس باکس و دکمه سرچ هم دارم. اگر تکس باکسم خالی باشه فقط گروه های اصلی که پدرشون 0 هست رو نشون میده در غیراینصورت گروه هایی در هر سطر که اسمشون شامل متن تکس باکسه.
مشکل من اینه که وقتی کاربر متنی رو جستجو میکنه و بعد اقدام به ویرایش یک سطر از اون میکنه ، گرید ویو دیتا سورسش رو از دست میده وارور زیر رو میده:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
سورس صفحه ام:
<asp:Label ID="Label1" runat="server" CssClass="txt" Text="searchgroups " ForeColor="#68a2d7"></asp:Label>
<asp:Label ID="Label2" runat="server" CssClass="txt" Text="group name" ForeColor="#68a2d7"></asp:Label>
<asp:TextBox ID="txtname" runat="server" CssClass="txt" Width="300px"></asp:TextBox> <br/>
<asp:ImageButton ID="Ibtnsearch" runat="server" ImageAlign="Left" ImageUrl="../Icon/resize/search.gif" OnClick="Ibtnsearch_Click" /> <br/>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" CssClass="txt" AllowPaging="True" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand1" PageSize="15" AllowSorting="True" OnSorting="GridView1_Sorting"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns>
<asp:BoundField DataField="chid" SortExpression="chid" HeaderText="ID" /> <asp:BoundField DataField="chname" SortExpression="chname" HeaderText="Name" /> <asp:HyperLinkField DataNavigateUrlFields="chid,cLanguage" DataNavigateUrlFormatString="../default.aspx?pnl=lstcatChat&nParentid_fk={0}&lang={1}" Text="Sub Groups" HeaderText="Show Sub Grups"> <ControlStyle CssClass="link" /> </asp:HyperLinkField> <asp:CommandField CausesValidation="false" ButtonType="Image" EditImageUrl="~/Icon/silk/application_edit.gif" ShowEditButton="True" CancelImageUrl="~/Icon/silk/arrow_undo.gif" UpdateImageUrl="~/Icon/silk/accept.gif" EditText="Edit" HeaderText="Edit" /> </Columns>
<RowStyle BackColor="#e8edf2" ForeColor="#333333" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <PagerStyle BackColor="White" ForeColor="#333333" HorizontalAlign="Center" BorderColor="White" Font-Bold="True" Font-Names="Tahoma" Font-Overline="False" Font-Size="X-Small" Font-Underline="False" /> <HeaderStyle BackColor="#68a2d7" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" /> <AlternatingRowStyle BackColor="White" /> <PagerSettings Mode="NumericFirstLast" /> </asp:GridView>
و کد من:
private DataTable Search_groups() { if (Request["nParentid_fk"] != null) parent_fk = Convert.ToInt32(Request["nParentid_fk"]); else parent_fk = 0; using (_Category nc = new _Category()) { if (Request["lang"] == "" || Request["lang"] == null) nc.Language = "fa"; else nc.Language = Request["lang"]; if (txtname.Text != "") return nc.search_allcategories(txtname.Text); else return nc.Select_parentid_fk(parent_fk); } }
public string GridViewSortDirection { get { //if (ViewState["sortDirection"] == null) // ViewState["sortDirection"] = SortDirection.Ascending; //return (SortDirection)ViewState["sortDirection"]; if (SortDirection.Value == null) SortDirection.Value = "asc"; return SortDirection.Value; } set { SortDirection.Value = value; } }
public string GridViewSortExpresion { get { //if (ViewState["SortExpresion"] == null) // ViewState["SortExpresion"] = ""; //if (ViewState["SortExpresion"] != null) // return ViewState["SortExpresion"].ToString(); //else // return null; if (SortExpresion.Value != null) return SortExpresion.Value.ToString(); else return null; } set { SortExpresion.Value = value; } }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { //DataTable dataTable = GridView1.DataSource as DataTable; GridViewSortExpresion = e.SortExpression; if (GridViewSortDirection == "asc") { GridViewSortDirection = "desc"; SortGridView(GridViewSortExpresion, GridViewSortDirection); } else { GridViewSortDirection = "asc"; SortGridView(GridViewSortExpresion, GridViewSortDirection); } }
private void SortGridView(string sortExpression, string direction) { // You can cache the DataTable for improving performance //DataTable dt = GridView1.DataSource as DataTable; DataTable dt = Search_groups(); DataView dv = new DataView(dt); dv.Sort = sortExpression + " " + direction; GridView1.DataSource = dv; GridView1.DataBind(); }
البته این صفحه یک صفحه ascx است و درون صفحه aspx بصورت پویا لود میشه.آیا راه حلی برای این مشکل وجود داره؟ لطفا منو راهنمایی کنید.
من یک صفحه دارم که گرید ویو آن گروه مقالات رو نشون میده و یک تکس باکس و دکمه سرچ هم دارم. اگر تکس باکسم خالی باشه فقط گروه های اصلی که پدرشون 0 هست رو نشون میده در غیراینصورت گروه هایی در هر سطر که اسمشون شامل متن تکس باکسه.
مشکل من اینه که وقتی کاربر متنی رو جستجو میکنه و بعد اقدام به ویرایش یک سطر از اون میکنه ، گرید ویو دیتا سورسش رو از دست میده وارور زیر رو میده:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
سورس صفحه ام:
<asp:Label ID="Label1" runat="server" CssClass="txt" Text="searchgroups " ForeColor="#68a2d7"></asp:Label>
<asp:Label ID="Label2" runat="server" CssClass="txt" Text="group name" ForeColor="#68a2d7"></asp:Label>
<asp:TextBox ID="txtname" runat="server" CssClass="txt" Width="300px"></asp:TextBox> <br/>
<asp:ImageButton ID="Ibtnsearch" runat="server" ImageAlign="Left" ImageUrl="../Icon/resize/search.gif" OnClick="Ibtnsearch_Click" /> <br/>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" CssClass="txt" AllowPaging="True" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand1" PageSize="15" AllowSorting="True" OnSorting="GridView1_Sorting"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns>
<asp:BoundField DataField="chid" SortExpression="chid" HeaderText="ID" /> <asp:BoundField DataField="chname" SortExpression="chname" HeaderText="Name" /> <asp:HyperLinkField DataNavigateUrlFields="chid,cLanguage" DataNavigateUrlFormatString="../default.aspx?pnl=lstcatChat&nParentid_fk={0}&lang={1}" Text="Sub Groups" HeaderText="Show Sub Grups"> <ControlStyle CssClass="link" /> </asp:HyperLinkField> <asp:CommandField CausesValidation="false" ButtonType="Image" EditImageUrl="~/Icon/silk/application_edit.gif" ShowEditButton="True" CancelImageUrl="~/Icon/silk/arrow_undo.gif" UpdateImageUrl="~/Icon/silk/accept.gif" EditText="Edit" HeaderText="Edit" /> </Columns>
<RowStyle BackColor="#e8edf2" ForeColor="#333333" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <PagerStyle BackColor="White" ForeColor="#333333" HorizontalAlign="Center" BorderColor="White" Font-Bold="True" Font-Names="Tahoma" Font-Overline="False" Font-Size="X-Small" Font-Underline="False" /> <HeaderStyle BackColor="#68a2d7" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" /> <AlternatingRowStyle BackColor="White" /> <PagerSettings Mode="NumericFirstLast" /> </asp:GridView>
و کد من:
private DataTable Search_groups() { if (Request["nParentid_fk"] != null) parent_fk = Convert.ToInt32(Request["nParentid_fk"]); else parent_fk = 0; using (_Category nc = new _Category()) { if (Request["lang"] == "" || Request["lang"] == null) nc.Language = "fa"; else nc.Language = Request["lang"]; if (txtname.Text != "") return nc.search_allcategories(txtname.Text); else return nc.Select_parentid_fk(parent_fk); } }
public string GridViewSortDirection { get { //if (ViewState["sortDirection"] == null) // ViewState["sortDirection"] = SortDirection.Ascending; //return (SortDirection)ViewState["sortDirection"]; if (SortDirection.Value == null) SortDirection.Value = "asc"; return SortDirection.Value; } set { SortDirection.Value = value; } }
public string GridViewSortExpresion { get { //if (ViewState["SortExpresion"] == null) // ViewState["SortExpresion"] = ""; //if (ViewState["SortExpresion"] != null) // return ViewState["SortExpresion"].ToString(); //else // return null; if (SortExpresion.Value != null) return SortExpresion.Value.ToString(); else return null; } set { SortExpresion.Value = value; } }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { //DataTable dataTable = GridView1.DataSource as DataTable; GridViewSortExpresion = e.SortExpression; if (GridViewSortDirection == "asc") { GridViewSortDirection = "desc"; SortGridView(GridViewSortExpresion, GridViewSortDirection); } else { GridViewSortDirection = "asc"; SortGridView(GridViewSortExpresion, GridViewSortDirection); } }
private void SortGridView(string sortExpression, string direction) { // You can cache the DataTable for improving performance //DataTable dt = GridView1.DataSource as DataTable; DataTable dt = Search_groups(); DataView dv = new DataView(dt); dv.Sort = sortExpression + " " + direction; GridView1.DataSource = dv; GridView1.DataBind(); }
البته این صفحه یک صفحه ascx است و درون صفحه aspx بصورت پویا لود میشه.آیا راه حلی برای این مشکل وجود داره؟ لطفا منو راهنمایی کنید.