checkbox in the grid view

checkbox in the grid view is always showing false

Page 1 of 1

11 Replies - 11063 Views - Last Post: 19 September 2011 - 01:04 PM Rate Topic: -----

#1 Guest_tripti*


Reputation:

checkbox in the grid view

Posted 23 June 2010 - 09:22 AM

I ran my code through the debugger and found that even if I check the checkbox, chk.Checked shows as false. Why??? Pleeeease help.


<asp:GridView ID="gvPatientHistory" runat="server"
           EmptyDataText="No Patient History" AutoGenerateColumns="false"  >
        <Columns>
            <asp:TemplateField HeaderText="Merge">
                <ItemTemplate>
                        <asp:CheckBox ID="CheckBoxMerge" runat="server" Enabled="true" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Case No.">
                <ItemTemplate>
                <asp:Label ID="LblCaseNo" runat="server" Text='<%# Eval("CaseNo") %>' visible="false"></asp:Label>  
                <a href='details.aspx?caseno=<%# Eval("CaseNo") %>&ai=<%# Eval("AccessionID") %>&pi=<%# Request.QueryString["pi"] %>'>
                <%# Eval("CaseNo") %>
                </a>
                </ItemTemplate>
        </asp:TemplateField>
            <asp:BoundField HeaderText="Case Type" datafield="CaseTypeName" />
            <asp:TemplateField HeaderText="Accession Date" >
            <ItemTemplate>
                <%# completedDateAndStep(DataBinder.Eval(Container.DataItem, "CreatedDate").ToString(), Eval("StepName").ToString())%>
               
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Completed Date" >
            <ItemTemplate>
                <%# completedDateAndStep(DataBinder.Eval(Container.DataItem, "CompletedDate").ToString(), Eval("StepName").ToString())%>
            </ItemTemplate>
        </asp:TemplateField><asp:BoundField HeaderText="Note" />
        </Columns>
    </asp:GridView><br> <br />
    <asp:Label ID="Label1" runat="server" ></asp:Label><br />
    <asp:Button ID="MergeReports" runat="server" Text="Batch Save Reports" onclick="MergeReportFiles" />


protected void MergeReportFiles(object sender, EventArgs e)
    {
        String Val = "Hi ";
        {
            foreach (GridViewRow gvr in gvPatientHistory.Rows)
            {
                CheckBox chk = (CheckBox)gvr.FindControl("CheckBoxMerge") as CheckBox;
                Label LblCaseNo = (Label)gvr.FindControl("LblCaseNo");

                if ((chk != null)&& chk.Checked == true){
                    Val = Val + LblCaseNo.Text;         //never reached because chk.Checked always shows false              
                }
            }
        }

        Label1.Text = Val;
    }


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0

Replies To: checkbox in the grid view

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: checkbox in the grid view

Posted 23 June 2010 - 09:23 AM

Moved to ASP.NET
Was This Post Helpful? 0
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: checkbox in the grid view

Posted 23 June 2010 - 09:55 AM

Are you calling a DataBind() method in the Page Load event? ;)

-Frinny
Was This Post Helpful? 1
  • +
  • -

#4 Guest_Tripti*


Reputation:

Re: checkbox in the grid view

Posted 23 June 2010 - 10:36 AM

View PostFrinavale, on 23 June 2010 - 08:55 AM, said:

Are you calling a DataBind() method in the Page Load event? ;)

-Frinny


yes I am calling the following in page load and in post back.

protected void Page_Load(object sender, EventArgs e)
{

if (User.Identity.Name == "")
{
Response.Redirect("../Login/");
}

_username = User.Identity.Name;
loggedUser.Text = getRealName(_username);

_caseno = Request.QueryString["caseNo"];
_accesionID = Request.QueryString["ai"];
_patientID = Request.QueryString["pi"];

if (_caseno.IndexOf("WHC", 0) == 0)
{
WHCButton.Visible = true;
}



getPatientInfo();
getReports();
getConcurrentCases();
getPatientHistory();
getSpecimensTest();
getNotations();
getWorkFlowStep();

if (Page.IsPostBack)
{
getPatientHistory();
}

}

protected void getPatientHistory()
{
gvPatientHistory.DataSource = csiDelegate.DataTableProcedure("Case_GetByPatientID",_patientID, _caseno);
gvPatientHistory.DataBind();
}
Was This Post Helpful? 0

#5 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: checkbox in the grid view

Posted 23 June 2010 - 10:44 AM

Move the call to the getPatientHistory() method from the Page Load event to the Page PreRender event.

When you call DataBind() you are re-binding the data to the control. When this happens all of the user provided data is erased.

To prevent data loss, wait until the last possible time to do call the DataBind. That way you don't lose anything you need for processing.

The Page PreRender event is the last possible moment that you can access the controls; which is why I recommend that you move the method that binds your data to the control to this page event. After this point all of the controls on the page are rendered into HTML and sent to the browser (so you can't really do anything with them).

-Frinny

This post has been edited by Frinavale: 23 June 2010 - 10:44 AM

Was This Post Helpful? 1
  • +
  • -

#6 Guest_Tripti*


Reputation:

Re: checkbox in the grid view

Posted 23 June 2010 - 11:45 AM

View PostFrinavale, on 23 June 2010 - 09:44 AM, said:

Move the call to the getPatientHistory() method from the Page Load event to the Page PreRender event.

When you call DataBind() you are re-binding the data to the control. When this happens all of the user provided data is erased.

To prevent data loss, wait until the last possible time to do call the DataBind. That way you don't lose anything you need for processing.

The Page PreRender event is the last possible moment that you can access the controls; which is why I recommend that you move the method that binds your data to the control to this page event. After this point all of the controls on the page are rendered into HTML and sent to the browser (so you can't really do anything with them).

-Frinny


Thanks.
That worked! :bananaman:
Was This Post Helpful? 0

#7 Guest_Tripti*


Reputation:

Re: checkbox in the grid view

Posted 23 June 2010 - 12:28 PM

Only one more thing. After I click the button 'Batch Save Reports' the page reloads and the checkboxes get unchecked. How do I keep them checked based on user selection before clicking the button.

One way would be to make Val string global for the page and use that in page load event to set chk.checked=true for all cases in Val string.

Is there another better way?

Thank You Sooo much.
Was This Post Helpful? 0

#8 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: checkbox in the grid view

Posted 23 June 2010 - 12:53 PM

I'm not sure...you could keep a list of which rows were checked stored in session or something and then during the GridView Row Binding Event set the checkboxes accordingly.

Or you could try enabling the ViewState for the GridView...(then you wouldn't always have to bind to the DataSource every page request).

-Frinny
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Tripti*


Reputation:

Re: checkbox in the grid view

Posted 23 June 2010 - 03:06 PM

View PostFrinavale, on 23 June 2010 - 11:53 AM, said:

I'm not sure...you could keep a list of which rows were checked stored in session or something and then during the GridView Row Binding Event set the checkboxes accordingly.

Or you could try enabling the ViewState for the GridView...(then you wouldn't always have to bind to the DataSource every page request).

-Frinny


Enabling ViewState for Grid View did not work for this. So I did it by keeping track of checked cases in the val as a session variable and setting chk.checked=true again in Page PreRender event. It works fine that way, but I am not sure if that is the right way. :bigsmile:
Was This Post Helpful? 0

#10 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: checkbox in the grid view

Posted 23 June 2010 - 06:47 PM

Look into doing this in the GridView.RowDataBound Event instead of in the PreRender event :)

-Frinny
Was This Post Helpful? 0
  • +
  • -

#11 llbtommy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-July 11

Re: checkbox in the grid view

Posted 08 July 2011 - 02:20 PM

THANKS FRINNY!!!
i was searching for hours for this simple solution that i was overlooking.

View PostFrinavale, on 23 June 2010 - 09:55 AM, said:

Are you calling a DataBind() method in the Page Load event? ;)

-Frinny

Was This Post Helpful? 0
  • +
  • -

#12 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: checkbox in the grid view

Posted 19 September 2011 - 01:04 PM

I'm glad this helped you :)
It's a pretty common mistake that people make.


-Frinny
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1