DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    1

    Question Dynamic Checkbox Issue

    i create dynamic checkboxes in coloum of table(<td id="trial" runat="server"></td>) from selecting quantity from dropdwonlist.
    well creating dynmic checkbox is not an issue..

    well the issue is tht i am not able to check whether the Checkboxes is checked or not on a Button click....

    the functiion used for generating dyamic checkbox..


    Code:
    protected void ddlList_SelectedIndexChanged(object sender, EventArgs e)  
        {
    int no=convert.toint32(ddlist.selectedvalue);  
     
     
    for(int i=1;i=<no;i++)
    {
    checkbox ch=new checkbox()
    ch.ID="chk_t"+i;
    ch.text="trl"
     
    trial.controls.add(ch);
     
    }
    }

    the function where i check whether checkboxes is checked


    Code:
    protected void btnSave_Click(object sender, EventArgs e)  
        {  
    list<string> status_tn=new list<string>();
     
    for (int j = 1; j <= no; j++)
                {
                   trialid = "chk_t" + j;
     
     
                  CheckBox  checks=(CheckBox)trial.FindControl(trialid);
     
     
     
     
                    if (checks.Checked)
                    {
     
                        status_tn.Add("Trial");
     
     
                    }
    }
              }
    in this function i get error :
    :System.NullReferenceException was unhandled by user code

    Message="Object reference not set to an instance of an object."

    Source="App_Web_pm_-zh7o"

    StackTrace:

    at btn_save_Click(Object sender, EventArgs e) in d:\Dot Net App\Talents\types_seeb.aspx.cs:line 325

    at System.Web.UI.WebControls.Button.OnClick(EventArgs e)

    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(St ring eventArgument)

    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

  2. #2
    Join Date
    Oct 2008
    Posts
    141
    only two observations:

    1. after calling FindControl, you should code
    //---------------------------------------
    if (checks != null)
    {

    if (checks.Checked)
    {

    status_tn.Add("Trial");


    }
    }
    //---------------------------------------

    2. your "no" variable is declared inside ddlList_SelectedIndexChanged. therefore, i don't think its scope goes beyong that method call, so it doesn't exist in btnSave_Click


    good luck,

    tonci korsano

  3. #3
    Join Date
    Sep 2010
    Posts
    11
    The code suggested by tkorsano is absolutely right. You can also set the default value so you for checkbox so you can code accordingly.

Similar Threads

  1. Replies: 1
    Last Post: 06-04-2010, 04:53 PM
  2. [VS03] Datagrid Checkbox Problem
    By zhixuen in forum ASP.NET
    Replies: 4
    Last Post: 02-16-2009, 08:45 AM
  3. Checkbox in gridview
    By whygh in forum ASP.NET
    Replies: 0
    Last Post: 07-04-2006, 04:38 PM
  4. They created J#, why couldn't they do VB#?
    By Thomas Eyde in forum .NET
    Replies: 290
    Last Post: 12-22-2001, 02:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links