Monday, March 12, 2012

How to handle Checkbox in itemtemplate and with the button in gridview

Hi All

I m having a gridview,button control on a form. In thatPatientId is a templatefiled along with a check box.Per page i m goingto display 10 records of a same patient.

My requirement is

1.user has to select only one check box if user selects more than one check box alert or message should be displayed.

2. If user checks only one checkbox i have to pick the particual row. I want to store all the values.

How to handle it.

I m able to get the values with checkbox, but i m unable with the button .

Here is my aspx page code

<asp:GridView ID="Prescgrid" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" Height="49px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Refill"
Text="Refill"></asp:LinkButton>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Patient_id" SortExpression="patient_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("patient_id") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("patient_id") %>'></asp:Label>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack=true OnCheckedChanged="Hai" />
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="prescription_date" DataFormatString="{0:d}" HeaderText="Prescription Date"
HtmlEncode="False" SortExpression="prescription_date">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="home_phone" HeaderText="Patient Home Phone" SortExpression="home_phone">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="pharmacy_phone" HeaderText="Pharmacy Phone" SortExpression="pharmacy_phone">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="medication1" HeaderText="Medication" SortExpression="medication1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="dosage1" HeaderText="Dosage" SortExpression="dosage1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="frequency1" HeaderText="Frequency" SortExpression="frequency1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="refills" HeaderText="Number Of Refills" SortExpression="refills">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="amt_tablets" HeaderText="Number Of Tablets" SortExpression="amt_tablets">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="staff_comments" HeaderText="Staff Comments" SortExpression="staff_comments">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
nbsp;

<asp:Button ID="Button1" runat="server" Text="Button" />

Waiting for valuable replies

Thank u

Baba

Hi Baba,

Now you want to capture the GridViewRow on which the CheckBox is selected on ButtonClick, right?

Try this.

<asp:GridViewID="Prescgrid"runat="server"AllowPaging="True"AllowSorting="True"

AutoGenerateColumns="False"BackColor="White"BorderColor="#3366CC"BorderStyle="None"

BorderWidth="1px"CellPadding="4"Height="49px">

<FooterStyleBackColor="#99CCCC"ForeColor="#003399"/>

<Columns>

<asp:TemplateFieldShowHeader="False">

<ItemTemplate>

<asp:LinkButtonID="LinkButton1"runat="server"CausesValidation="false"CommandName="Refill"

Text="Refill"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldHeaderText="Patient_id"SortExpression="patient_id">

<EditItemTemplate>

<asp:TextBoxID="TextBox1"runat="server"Text='<%# Bind("patient_id") %>'></asp:TextBox>

</EditItemTemplate>

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

<ItemTemplate>

<asp:LabelID="Label1"runat="server"Text='<%# Bind("patient_id") %>'></asp:Label>

<asp:CheckBoxID="CheckBox2"runat="server"AutoPostBack="true"OnCheckedChanged="CheckBox2_CheckedChanged"/>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundFieldDataField="prescription_date"DataFormatString="{0:d}"HeaderText="Prescription Date"

HtmlEncode="False"SortExpression="prescription_date">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="home_phone"HeaderText="Patient Home Phone"SortExpression="home_phone">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="pharmacy_phone"HeaderText="Pharmacy Phone"SortExpression="pharmacy_phone">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="medication1"HeaderText="Medication"SortExpression="medication1">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="dosage1"HeaderText="Dosage"SortExpression="dosage1">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="frequency1"HeaderText="Frequency"SortExpression="frequency1">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="refills"HeaderText="Number Of Refills"SortExpression="refills">

<HeaderStyleHorizontalAlign="Left"/>

</asp:BoundField>

<asp:BoundFieldDataField="amt_tablets"HeaderText="Number Of Tablets"SortExpression="amt_tablets">

<HeaderStyleHorizontalAlign="Left"/>

</asp:BoundField>

<asp:BoundFieldDataField="staff_comments"HeaderText="Staff Comments"SortExpression="staff_comments">

<HeaderStyleHorizontalAlign="Left"/>

</asp:BoundField>

</Columns>

<RowStyleBackColor="White"ForeColor="#003399"/>

<SelectedRowStyleBackColor="#009999"Font-Bold="True"ForeColor="#CCFF99"/>

<PagerStyleBackColor="#99CCCC"ForeColor="#003399"HorizontalAlign="Left"/>

<HeaderStyleBackColor="#003399"Font-Bold="True"ForeColor="#CCCCFF"/>

</asp:GridView>

<asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/>

protectedvoid CheckBox2_CheckedChanged(object sender,EventArgs e)

{

string cScript ="<script>alert('Another CheckBox already checked');</script>";

CheckBox chkCheckBox2 = (CheckBox)sender;

GridViewRow mySelectedRow = ((CheckBox)sender).Parent.ParentasGridViewRow;if(chkCheckBox2.Checked)

{

foreach (GridViewRow gvrin Prescgrid.Rows)

{

if(gvr!=mySelectedRow)

{

if(((CheckBox)gvr.FindControl("CheckBox2")).Checked)

{

ClientScript.RegisterStartupScript(typeof(Page),"clientscript",cScript);

chkCheckBox2.Checked =false;

}

}

}

}

}

protectedvoid Button1_Click(object sender,EventArgs e)

{

string cScript ="<script>alert('First Select a Row');</script>";

bool blnFlag =false;

foreach (GridViewRow gvrin Prescgrid.Rows)

{

if (((CheckBox)gvr.FindControl("CheckBox2")).Checked)

{

//access all elements using myRow.FindControl blah blah

string strPatientID = ((Label)gvr.FindControl("Label1")).Text;blnFlag =true;

}

}

if (!blnFlag)

{

ClientScript.RegisterStartupScript(typeof(Page),"clientscript", cScript);

}

}

Regards,

Naveen

Labels: , , , , , , , , , , , , , , , ,

how to handle addhandler to the dynamically added control in datagrid


Hello friends,
I have a datagrid which contain the conditional checkbox added in the second column of the
datagrid.It is done in the
datagrid's itemdatabound event... It is working fine.
But I want to handle the checkbox's checkchanged event...
As the checkbox is added dynamically, i am trying to use the

AddHandler mychkbox.CheckedChanged,AddressOfMe.mychkbox_CheckedChanged

But at any case, this event is not firing ...
In the following condition,
i.e. I am binding the datagrid in the page_load event.
within the condition as

If (Page.IsPostBack =False)Then

bindgrid()
end if
If i removed the condition above & fire the bindgrid() in the page_load event directly,
then it is firing the event:
AddHandler mychkbox.CheckedChanged,AddressOfMe.mychkbox_CheckedChanged
But for my anothere use i have to fire the binggrid event within the Page.IsPostBack =False
Plz tell me the solution how to fire the
AddHandler mychkbox.CheckedChanged,AddressOfMe.mychkbox_CheckedChanged

Thanks & Regards,
Sandeep.

master_sandy wrote:


Hello friends,
I have a datagrid which contain the conditional checkbox added in the second column of the
datagrid.It is done in the
datagrid's itemdatabound event... It is working fine.


Did you mean that the checkbox would be added to your second column conditionally?? If that is the case you may not be able to use the CheckChanged event handler for your checkbox control at all because any events related to the controls within a DataGrid Control needs to be added within the ItemCreated Event of the DataGrid but this event occurs before the ItemDataBound event and hence if you create your Checkboxes conditionally based on some value coming from the Database in your ItemDataBound event, you would not be able to add the Check Changed event handler to you Control as this event handler along with the Control Creation needs to be done in the ItemCreated event which is before ItemDataBound event chronologically. Write back, how exactly you want to create this checkbox control and I could help you further.
hth

Labels: , , , , , , , , , , , , ,