Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Get value of a checkbox in a gridview. using ajax

Author  Topic 

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-10-28 : 11:53:52
Hello Guys

i have the even on the button2 click. Its an auto postback to an ajax script to write the value of a gridview to a label. However it only writes the value of the last returned table row only

Basically, i want to to get the value of the checkbox of a gridview written to a label control. Using ajax and .net 2.0

what is wrong ?

below is my gridview

protected void Button2_Click(object sender, EventArgs e)
{
bool result = false;
string phonenumber = String.Empty;
string existing ="";
foreach (GridViewRow row in GridView_PhoneGroups0.Rows)
{
result = (row.FindControl("chkboxphone") as CheckBox).Checked;

if (result)
{

phonenumber = (row.FindControl("Phone_Number") as Label).Text;
//existing = existing + "," + phonenumber.ToString;
Recepient.Text = phonenumber+',';
Label3.Text = phonenumber.ToString();

}
else
{

phonenumber = (row.FindControl("Phone_Number") as Label).Text;
//existing = existing + "," + phonenumber.ToString;
Recepient.Text = phonenumber + ',';
Label3.Text = phonenumber.ToString();

}
}
}






<asp:GridView ID="GridView_PhoneGroups0" runat="server"
AutoGenerateColumns="False" DataSourceID="ObjectDataSource_Phonebook"
onselectedindexchanged="GridView_PhoneGroups_SelectedIndexChanged"
AllowPaging="True" PageSize="20" BorderStyle="None" BorderWidth="0px"
CellPadding="0">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:CheckBox ID="chkboxphone" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PhoneNumber">
<ItemTemplate>
<asp:LinkButton ID="Phone_Number" runat="server" Text='<%# Eval("number")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-10-29 : 01:15:28
Couple of issues -

1. You are looping through each and every row in Grid view and assigning the phonenumber value to a label. Phonenumber variable is re-initialized each time and NOT appended. So you'll be getting only the last rows value. Shouldn't you do something like this -

phonenumber = phonenumber + (row.FindControl("Phone_Number") as Label).Text;


2. Your trying to get the check box value into label. But why are you assigning the phone numbers to label?
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-10-29 : 05:21:32
thanks. Found my mistake
Was declaring a label, when i had a linkbutton below

Aggghhhh I hate programming

thanks

quote:
Originally posted by afrika

Hello Guys

i have the even on the button2 click. Its an auto postback to an ajax script to write the value of a gridview to a label. However it only writes the value of the last returned table row only

Basically, i want to to get the value of the checkbox of a gridview written to a label control. Using ajax and .net 2.0

what is wrong ?

below is my gridview

protected void Button2_Click(object sender, EventArgs e)
{
bool result = false;
string phonenumber = String.Empty;
string existing ="";
foreach (GridViewRow row in GridView_PhoneGroups0.Rows)
{
result = (row.FindControl("chkboxphone") as CheckBox).Checked;

if (result)
{

phonenumber = (row.FindControl("Phone_Number") as Label).Text;
//existing = existing + "," + phonenumber.ToString;
Recepient.Text = phonenumber+',';
Label3.Text = phonenumber.ToString();

}
else
{

phonenumber = (row.FindControl("Phone_Number") as Label).Text;
//existing = existing + "," + phonenumber.ToString;
Recepient.Text = phonenumber + ',';
Label3.Text = phonenumber.ToString();

}
}
}






<asp:GridView ID="GridView_PhoneGroups0" runat="server"
AutoGenerateColumns="False" DataSourceID="ObjectDataSource_Phonebook"
onselectedindexchanged="GridView_PhoneGroups_SelectedIndexChanged"
AllowPaging="True" PageSize="20" BorderStyle="None" BorderWidth="0px"
CellPadding="0">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:CheckBox ID="chkboxphone" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PhoneNumber">
<ItemTemplate>
<asp:LinkButton ID="Phone_Number" runat="server" Text='<%# Eval("number")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-10-29 : 06:38:07
quote:
Originally posted by cvraghu

Couple of issues -

1. You are looping through each and every row in Grid view and assigning the phonenumber value to a label. Phonenumber variable is re-initialized each time and NOT appended. So you'll be getting only the last rows value. Shouldn't you do something like this -

phonenumber = phonenumber + (row.FindControl("Phone_Number") as Label).Text;


2. Your trying to get the check box value into label. But why are you assigning the phone numbers to label?



Now that, I found the bug. This code worked for me. Thanks a lot
Go to Top of Page
   

- Advertisement -