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: ado, alli, along, box, button, checkbox, control, display, form, goingto, gridview, handle, itemtemplate, net, page, templatefiled, thatpatientid
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home