locked
using check boxes to determine 'Yes' or 'No' RRS feed

  • Question

  • User154499744 posted

    http://www.asp.net/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/form-basics

    The code from the example is listed below. In this example, you put in a search value and that gets passed in to a SQL select statement as the WHERE clause value.

    I would like to do something very similar, but use a checkbox to set a parameter value for a stored procedure. I don't really know what the final code for this will look like, but I am thinking these are the things I need to do.

    (1) In the <form> section use input type = "checkbox". Not sure what other properties I need to specify for the checkbox. I suppose name? Whatelse?

    (2) I suppose the value of a checkbox is a boolean (true/false). How can take this and turn it into a string value of "Yes" or "No"? I need to turn the check box result into a "Yes" or "No" because that's what my stored procedure takes. I suppose I can change my stored procedure to take a boolean, but as an exercise, I would like to see how a checkbox value is turned into a "Yes" or "No" string value.

    I think if someone can get me on the right track with these two questions, then I can follow the example in the tutorial to do what I need.

    @{
        var db = Database.Open("WebPagesMovies") ;
        var selectCommand = "SELECT * FROM Movies";
        var searchTerm = "";
    
        if(!Request.QueryString["searchGenre"].IsEmpty() ) {
            selectCommand = "SELECT * FROM Movies WHERE Genre = @0";
            searchTerm = Request.QueryString["searchGenre"];
        }
    
        if(!Request.QueryString["searchTitle"].IsEmpty() ) {
            selectCommand = "SELECT * FROM Movies WHERE Title LIKE @0";
            searchTerm = "%" + Request["searchTitle"] + "%";
        }
    
        var selectedData = db.Query(selectCommand, searchTerm);
        var grid = new WebGrid(source: selectedData, defaultSort: "Genre", rowsPerPage:3);
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <title>Movies</title>
        <style type="text/css">
          .grid { margin: 4px; border-collapse: collapse; width: 600px; }
          .grid th, .grid td { border: 1px solid #C0C0C0; padding: 5px; }
          .head { background-color: #E8E8E8; font-weight: bold; color: #FFF; }
          .alt { background-color: #E8E8E8; color: #000; }
        </style>
      </head>
      <body>
        <h1>Movies</h1>
          <form method="get">
            <div>
            <label for="searchGenre">Genre to look for:</label>
            <input type="text" name="searchGenre" value="@Request.QueryString["searchGenre"]" />
            <input type="Submit" value="Search Genre" /><br/>
            (Leave blank to list all movies.)<br/>
            </div>
    
            <div>
              <label for="SearchTitle">Movie title contains the following:</label>
              <input type="text" name="searchTitle" value="@Request.QueryString["searchTitle"]" />
              <input type="Submit" value="Search Title" /><br/>
            </div>
          </form>
    
        <div>
          @grid.GetHtml(
            tableStyle: "grid",
            headerStyle: "head",
            alternatingRowStyle: "alt",
            columns: grid.Columns(
              grid.Column("Title"),
              grid.Column("Genre"),
              grid.Column("Year")
            )
          )
        </div>
      </body>
    </html>



    Thursday, February 18, 2016 4:35 PM

Answers

  • User-157580668 posted

    (1) Yes. the 'name' property. by name property you can get the checkbox value

    (2) well... below is my code sample. You can manipulate string variation by if clause

    @section Scripts {
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    }
    
    @{
        var photoId = UrlData[0].AsInt();
    
        WebSecurity.RequireAuthenticatedUser();
    
        var db = Database.Open("LawMi");
    
        var photo = db.QuerySingle("SELECT * FROM UserProfilePhoto WHERE Id = @0", photoId);
        if (photo == null) 
        {
            Response.SetStatus(HttpStatusCode.NotFound);
            return;        
        }
    
        WebSecurity.RequireUser(photo.UserId);
    
        var check = photo.representative;
    
        Page.Title = "사진 편집 - " + photo.FileTitle;
    
        Validation.RequireField("fileTitle", "이름을 지정해야 합니다.");
        Validation.RequireField("설명", "설명을 지정해야 합니다.");
    
        if (IsPost) 
        {
    
            var fileTitle = Request["FileTitle"];
            var description = Request["Description"];
            var represent = Request["Represent"];
    
            if (represent == "true") {
              db.Execute("UPDATE UserProfilePhoto SET Representative = '0' WHERE UserID = @0", photo.userID);  
            }
            
            db.Execute("UPDATE UserProfilePhoto SET FileTitle = @0, Description= @1, Representative = '1' WHERE Id = @2", fileTitle, description, photoId);
          
            // Response.Redirect(Href("~User/ViewPhoto", photo.Id));
                    Response.Redirect(Href("~/User/View", photo.userID));
        }
    }
    
    <h1>Edit @photo.FileTitle</h1>
    
    <section id="sidebar">
        <form method="post">
            <fieldset class="no-legend">
                <legend>Edit @photo.FileTitle</legend>
                <ol>
                    <li>
                        <label for="representative">대표이미지</label>
                        <input type="checkbox" name="Represent" title="대표이미지" value="true"  checked="@check"/>
                    </li>
                    <li>
                        <label for="fileTitle">이름:</label>
                        <input type="text" name="fileTitle" title="사진 이름" value="@photo.FileTitle" placeholder="Photo Name" @Validation.For("fileTitle") />
                        @Html.ValidationMessage("FileTitle")
                    </li>
                    <li>
                        <label for="description">설명:</label>
                        <textarea name="description" title="사진 설명" rows="4" cols="30" placeholder="Photo description" @Validation.For("description") >@photo.Description</textarea>
                        @Html.ValidationMessage("Description")
                    </li>
                </ol>
                <p class="form-actions" data-role="controlgroup" data-type="horizontal">
                    <input type="submit" value="변경 내용 저장" title="변경 내용 저장" />
                    <a data-role="button" href="~/User/View/@photoId" title="Cancel changes">취소</a>
                </p>
            </fieldset>
        </form>
    </section>
    
    <img alt="@Html.AttributeEncode(photo.FileTitle)" src="~/User/Thumbnail/@photo.Id?size=large" />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2016 3:24 AM
  • User-2057865890 posted

    Hi NewKid1nTown,

    You could use the following method to determine wheter a checkbox property is selected or not.

    <input type="checkbox" name="checkboxName" />
    
    if (Request.Form["checkboxName"] != null && Request.Form["checkboxName"] == "on")
    {
        //checkbox is selected
    }
    else
    {
        //checkbox is not selected
    }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2016 6:09 AM

All replies

  • User-157580668 posted

    (1) Yes. the 'name' property. by name property you can get the checkbox value

    (2) well... below is my code sample. You can manipulate string variation by if clause

    @section Scripts {
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    }
    
    @{
        var photoId = UrlData[0].AsInt();
    
        WebSecurity.RequireAuthenticatedUser();
    
        var db = Database.Open("LawMi");
    
        var photo = db.QuerySingle("SELECT * FROM UserProfilePhoto WHERE Id = @0", photoId);
        if (photo == null) 
        {
            Response.SetStatus(HttpStatusCode.NotFound);
            return;        
        }
    
        WebSecurity.RequireUser(photo.UserId);
    
        var check = photo.representative;
    
        Page.Title = "사진 편집 - " + photo.FileTitle;
    
        Validation.RequireField("fileTitle", "이름을 지정해야 합니다.");
        Validation.RequireField("설명", "설명을 지정해야 합니다.");
    
        if (IsPost) 
        {
    
            var fileTitle = Request["FileTitle"];
            var description = Request["Description"];
            var represent = Request["Represent"];
    
            if (represent == "true") {
              db.Execute("UPDATE UserProfilePhoto SET Representative = '0' WHERE UserID = @0", photo.userID);  
            }
            
            db.Execute("UPDATE UserProfilePhoto SET FileTitle = @0, Description= @1, Representative = '1' WHERE Id = @2", fileTitle, description, photoId);
          
            // Response.Redirect(Href("~User/ViewPhoto", photo.Id));
                    Response.Redirect(Href("~/User/View", photo.userID));
        }
    }
    
    <h1>Edit @photo.FileTitle</h1>
    
    <section id="sidebar">
        <form method="post">
            <fieldset class="no-legend">
                <legend>Edit @photo.FileTitle</legend>
                <ol>
                    <li>
                        <label for="representative">대표이미지</label>
                        <input type="checkbox" name="Represent" title="대표이미지" value="true"  checked="@check"/>
                    </li>
                    <li>
                        <label for="fileTitle">이름:</label>
                        <input type="text" name="fileTitle" title="사진 이름" value="@photo.FileTitle" placeholder="Photo Name" @Validation.For("fileTitle") />
                        @Html.ValidationMessage("FileTitle")
                    </li>
                    <li>
                        <label for="description">설명:</label>
                        <textarea name="description" title="사진 설명" rows="4" cols="30" placeholder="Photo description" @Validation.For("description") >@photo.Description</textarea>
                        @Html.ValidationMessage("Description")
                    </li>
                </ol>
                <p class="form-actions" data-role="controlgroup" data-type="horizontal">
                    <input type="submit" value="변경 내용 저장" title="변경 내용 저장" />
                    <a data-role="button" href="~/User/View/@photoId" title="Cancel changes">취소</a>
                </p>
            </fieldset>
        </form>
    </section>
    
    <img alt="@Html.AttributeEncode(photo.FileTitle)" src="~/User/Thumbnail/@photo.Id?size=large" />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2016 3:24 AM
  • User-2057865890 posted

    Hi NewKid1nTown,

    You could use the following method to determine wheter a checkbox property is selected or not.

    <input type="checkbox" name="checkboxName" />
    
    if (Request.Form["checkboxName"] != null && Request.Form["checkboxName"] == "on")
    {
        //checkbox is selected
    }
    else
    {
        //checkbox is not selected
    }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2016 6:09 AM