How to Handle Check box with Button Control
Hi All
I m having a gridview,button control on a form. In that PatientId is a templatefiled along with a check box.Per page i m going to 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
You may do better with a DataGrid if you are allowing multiple selections.
http://www.dotnetjohn.com/articles.aspx?articleid=83
Alternately, set up an array as a session variable and populate it with the DataKey for each row on the checkbox_checked event. You should be able to get that out of the event arguments. Then when you do whatever you do, iterate throught the array and pass those datakeys to the SQL statement or whatever you are using as a criteria class.
Hi Baba,
Try this.
http://forums.asp.net/t/1185169.aspx
Regards,
Naveen
Hisrijaya_ramaraju ,
srijaya_ramaraju@.yahoo.com:
1.user has to select only one check box if user selects more than one check box alert or message should be displayed.
You can use javascript to allow user to select only one check box.
srijaya_ramaraju@.yahoo.com:
2. If user checks only one checkbox i have to pick the particual row. I want to store all the values
You can use findcontrol method to get any controls in the row then retrieve it's value.
Have a look at this sample ,
1<div>2 <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">3 <Columns>4 <asp:TemplateField>5 <ItemTemplate>6 <asp:CheckBox ID="CheckBox1" runat="server" />7 </ItemTemplate>8 </asp:TemplateField>9 </Columns>10 </asp:GridView>1112 </div>13 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
1function check(self)2 {34 var items = document.getElementsByTagName('input');56for(var i =0;i7 {8if(items(i).type =='checkbox' && items(i).id != self.id && items(i).checked ==true)9 items(i).checked =false;10 }11121314 }
1protected void Page_Load(object sender, EventArgs e)2 {3if (!IsPostBack)4 {5 DataTable table =new DataTable();6 table.Columns.Add("age");7 table.Columns.Add("name");8 DataRow dr = table.NewRow();9 dr["age"] = 1;10 dr["name"] ="zz";11 table.Rows.Add(dr);1213 DataRow dr2 = table.NewRow();14 dr2["age"] = 2;15 dr2["name"] ="vv";16 table.Rows.Add(dr2);1718this.GridView1.DataSource = table;19 GridView1.DataBind();20 }212223//this.Button1.Attributes.Add("onclick","check(); return false;");24 }2526protected void Button1_Click(object sender, EventArgs e)27 {28for (int i = 0; i < GridView1.Rows.Count; i++)29 {30 CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");31if (chk.Checked ==true)32 {33string name = GridView1.Rows[i].Cells[2].Text;//get value here34 }35 }36 }3738protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)39 {40if (e.Row.RowType == DataControlRowType.DataRow)41 {42 CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");43 chk.Attributes.Add("onclick","check(this)");44 }45 }
Labels: ado, alli, along, box, button, control, display, form, gridview, handle, net, page, patientid, templatefiled
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home