Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i had placed a gridview in asp.net and in template item field i had placed a check box.there is also a button in my asp web form.checked row of the grid should be updated in to oracle database(table) when i click the button.i am kind of new to asp.net .so please help me..thanks in advance...
Posted

take one hiddenfield and put besides checkbox and assign rowindex to it.

<asp:checkbox id="yourcheckbox/" xmlns:asp="#unknown"><asp:hiddenfield runat="server" id="hidRowIndex" value="<%# Container.DataItemIndex %>" /></asp:checkbox>


and retrieve using


protected void btnLink_Click(object sender, EventArgs e)        {            foreach (GridViewRow row in gvAllocated.Rows)            {                if (((CheckBox)row.FindControl("chkLink")).Checked)                {
                        int index = (((HiddenField)row.FindControl("hidRowIndex")).Value;

                       // here you can acess your other values using rowidnex
               }            }        }
 
Share this answer
 
Try this one:

C#
CheckBox chk;
int ID = 0;

foreach(GridViewRow row in GridView1.Rows)
{
   chk = (CheckBox)row.Cells[0].FindControl("chkboxid"); //chkboxid is the ID of the checkbox control in asp.net inside template field
   if(chk.Selected == true)
   {
      ID = Convert.ToInt32(row.Cells[1].Text);
   }
}

Wherein Cells[0] is the checkbox and Cells[1] is the ID of the selected item.
Then use ID as an input parameter for your query to update...

Please mark as answer if this solved your problem

Regards,
Eduard
 
Share this answer
 
v5
Comments
narutoluffy01 28-Nov-11 5:31am    
foreach (GridViewRow row in GridView1.Rows)
{
chk = (CheckBox)row.Cells[0].FindControl("CheckBox1"); //chkboxid is the ID of the checkbox control in asp.net inside template field
if (chk.Checked == true)
{
ID = Convert.ToInt32(row.Cells[1].Text);
}
}
when i put debug point and checked i had found that if condition is getting false
[no name] 28-Nov-11 5:41am    
this means that the checkbox on the current row isn't checked
narutoluffy01 28-Nov-11 7:11am    
i had selected the check box...but if condition is getting false
fjdiewornncalwe 28-Nov-11 12:30pm    
A quick fix. To check for the checked state of a checkbox, use .Checked, not .Selected in the condition.
[no name] 28-Nov-11 19:50pm    
I think .Checked is used only for windows app and not for web app?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900