This is a migrated thread and some comments may be shown as answers.

[Solved] View Columns via Checkbox WebFrom

2 Answers 32 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Clark
Top achievements
Rank 1
Clark asked on 20 Sep 2011, 08:21 PM
I have a database that has a huge number of columns so I created checkboxes for the user to select exactly what columns they want to view.  It works well enough in that when I first go to the page it shows the checkboxes at top with the Apply button and the grid view underneath.  Because nothing has been submitted from the WebForm, the grid view shows all the columns.  Once the user hits apply it shows the columns they have selected.  However, if the user then tries to sort or filter one of these columns, it reverts back to showing all of the columns.  Here is a brief explanation of how I have everything coded:

Index View

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Index", "Order"))
   { %>
    <p>
        <%= Html.CheckBox("osurr", true, "Ad Number")%>Ad Number        //I set this and a few other columns to default to true
        <%= Html.CheckBox("csurr", false, "Customer Number")%>Customer Number
        <%= Html.CheckBox("rhosurr", false, "RHO Number")%>RHO Number
        <%= Html.CheckBox("lockid", false, "Lock ID")%>Lock ID
                                //And several more
    </p>
    <input type="submit" value="Apply" />
<% } %>
<%
    Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
                     {
                         columns.Template(o =>
                                              {
%>
                      <%=Html.ActionLink("Detail", "Edit", new { id = o.osurr })%>
                  <%
                                              }).Width(25);
 
 
                         if (ViewData["osurr"].Equals(true)) { columns.Bound(o => o.osurr).Title("Ad No."); }
                         if (ViewData["csurr"].Equals(true)) { columns.Bound(o => o.csurr).Title("Cust. No."); }
                         if (ViewData["rhosurr"].Equals(true)) { columns.Bound(o => o.rhosurr).Title("RHO No."); }
                         if (ViewData["lockid"].Equals(true)) { columns.Bound(o => o.lockid).Title("Lock ID"); }
                                                                                                    //And again, several more.
                     })
        .Groupable(grouping => grouping.Enabled(true))
        .Resizable(resizing => resizing.Columns(true))
        .Filterable(filter => filter.Enabled(true))
        .Sortable(sorting => sorting.Enabled(true))
        .Pageable(paging => paging.Enabled(true).PageSize(25))
        .Render();   
%>
 
 
</asp:Content>

Controller

public ActionResult Index(bool? osurr, bool? csurr, bool? rhosurr, bool? lockid /* And Several More */)
{
ViewData["osurr"] = osurr ?? true;
ViewData["csurr"] = csurr ?? true;
ViewData["rhosurr"] = rhosurr ?? true;
ViewData["lockid"] = lockid ?? true;
//And Several more
                          
var db = new MillieOrderDB();
var listView = from m in db.vw_cadords
orderby m.createstamp descending
select m;
return View(listView);
}


I am working just in the Index ActionResult for now to keep things in one place while I figure out how to get this all to work.  I've also tried making my if (ViewData["osurr"].Equals(true)) { columns.Bound(o => o.osurr).Title("Ad No."); } statement have an else { columns.Bound(o => o.osurr).Hidden(true); } without success. . . . I've also tried testing that the ViewData is not null ex: if (ViewData["osurr"] != null) { if (ViewData["osurr"].Equals(true)) { columns.Bound(o => o.osurr).Title("Ad No."); } } and this didn't help either.

It seems that the ViewData is refreshed along with the view any time any Action is called.  Does anyone have any ideas on how to get this to work?

2 Answers, 1 is accepted

Sort by
0
Clark
Top achievements
Rank 1
answered on 30 Sep 2011, 06:27 PM
I decided to use cookies and it is working fine except that I am needing to press the apply button twice for the changes to take effect.  Also, if I use more than one filter, the grid view will change to include all columns instead of just the ones selected from the check boxes.

I changed the if statements in the index view for the columns as such:
Index View
if (Request.Cookies["DBCols"]["csurr"] != null)
{
   if (Request.Cookies["DBCols"].Values["csurr"].Equals("True")) { columns.Bound(o => o.csurr).Title("Cust. No."); }
}

I added the following to my controller:
Controller
HttpCookie DBCols = new HttpCookie("DBCols");
if ((bool)ViewData["osurr"]) { DBCols.Values["osurr"] = (ViewData["osurr"].ToString()); }
else { DBCols.Values["osurr"] = "False"; }
if ((bool)ViewData["csurr"]) { DBCols.Values["csurr"] = (ViewData["csurr"].ToString()); }
else { DBCols.Values["csurr"] = "False"; }
DBCols.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(DBCols);


Anyone have any ideas why I am having to press apply twice and why the filters are not working the way I would like?
0
Clark
Top achievements
Rank 1
answered on 04 Oct 2011, 05:19 PM
I got this figured out . . . Once I moved all the form data into it's own ActionResult and had that RedirecttoAction("Index"), everything worked great!
Tags
Grid
Asked by
Clark
Top achievements
Rank 1
Answers by
Clark
Top achievements
Rank 1
Share this question
or