CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Threaded View

  1. #12
    Join Date
    Jan 2009
    Location
    Cochin, India
    Posts
    40

    Smile Re: <asp:checkbox> vs <input type="checkbox">

    Hey Tarun,

    That makes life even simpler. I give below a sample program again. Once again open up a new default web site in VS 2005. It will have a web page in it called Default.aspx. I give below the code for Default.aspx and Default.aspx.cs. Simply substitute it.

    Code for Default.aspx

    Code:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Repeater EnableViewState="true" ID="Repeater1" runat="server">
            <HeaderTemplate><table width= "100%"></HeaderTemplate>
            <ItemTemplate>
               <tr><td>
                <input type="checkbox" id="mycheckbox" value='<%#DataBinder.Eval(Container.DataItem, "field1") %>' runat="server" />
                </td></tr>
            </ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
            </asp:Repeater>
            <asp:Button ID="mybutton" runat="server" value="Submit" onclick="mybutton_Click" />            
        </div>
        </form>
    </body>
    </html>
    And here is the code for Default.aspx.cs

    Code:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SqlConnection con = new SqlConnection("Your connection string here");
                string sql = "select field1 from table1";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                ad.Fill(ds, "MyTable");
                Repeater1.DataSource = ds;
                Repeater1.DataBind();
    
            }
        }
        protected void mybutton_Click(object sender, EventArgs e)
        {
            string msg = "";
            var filename = "filename";
            for (int i = 0, j = 0; i < Repeater1.Controls.Count; i++)
            {
                try
                {
                    HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Controls[i].FindControl("mycheckbox");
                    if (chk.Checked)
                    {
                        msg += filename + j.ToString() + "=" + chk.Value + "&";
                        j++;
                    }
                }
                catch
                {
                }
    
            }
            if (msg != "")
            {
                int pos = msg.LastIndexOf('&');
                msg = msg.Substring(0, msg.Length - 1);
                string url = "targetpage.aspx?" + msg;
                string openpopup = "<script language=\"javascript\">window.open('" + url + "', 'target', 'Height=400,width=400');</script>";
                Response.Write(openpopup);
            }
        }
    }
    After substituting the code for Default.aspx and Default.aspx.cs go to solution explorer and add a new page called targetpage.aspx. And here is the code for targetpage.aspx and targetpage.aspx.cs. You can once again substitute it.

    Code for targetpage.aspx

    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="targetpage.aspx.cs" Inherits="targetpage" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
        </form>
    </body>
    </html>
    And here is the code for targetpage.aspx.cs

    Code:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class targetpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = Request.QueryString["filename0"] + " " +  Request.QueryString["filename1"];
        }
    }
    I have used the database table name of table1 and field name of field1. One thing you will have to do is to again put in your appropriate connection string and field and table names in Default.aspx and Default.aspx.cs. After doing that you set Defaut.aspx as the start up page and run the application. You will see a row of check boxes and a button at the bottom.

    Now if you check any one check boxes and click on the button, targetpage.aspx will open up in a pop up window and it will show the value associated with the check box you had checked. If you check two check boxes and click on the button the pop up window will list the values associated with the 2 check boxes that were checked. But I haven't coded for listing more than values of 2 checked check boxes in targetpage. But you can always do that.

    You see what is happening is, say there are 10 check boxes displayed in 10 rows in the repeater and you check any 5 of them and click on the button. Then the values associated with the 5 checked check boxes are being passed to targetpage.aspx as query string parameters filename0, filename1, filename2, filename3 and filename4.

    The number of parameters passed depends on the number of check boxes that you have checked before clicking on the button. I have only unraveled a up to 2 values in targetpage. You can always handle that end of it.

    Hope this helps.

    Warm Regards.
    Jay

    ps - Tarun, on second thoughts I thought I might as well attach the sample application. Find attached to this post a file called WebSite3.zip. You can simply unzip it and you will get a directory WebSite3. This is the sample web application I developed. It is developed in Visual web developer 2008. But I am sure it will open in VS 2005 also because there is absolutely no code change. You can use this for your purposes after putting in the appropriate connection string, field names and table names in Default.aspx and Default.aspx.cs. If it does not open in VS 2005 then you may have to cut and paste the code snippets I have given above in a VS 2005 web site.
    Attached Files Attached Files
    Last edited by sr_jay; March 14th, 2009 at 11:00 PM. Reason: I created an attachment
    Jay
    Support Resort
    http://www.supportresort.com
    Bringing offshore expertise to the world

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured