Solved

get the specific checkbox value when the button is clicked on gridview

Posted on 2012-03-19
2
398 Views
Last Modified: 2012-08-14
Hi EE,
I have GridView which has 3 checkboxes column and one button column. some records have all checkboxes are checked, some are just one or two are checked according to the database's check record.
suppose if i just check on the one checkbox and hit the button from the first record then it gets other checkboxes value too on my following code.

How can I get only the same record's selected checkbox value which is hitted.

[.aspx]
<asp:CheckBox ID="CheckBox0" runat="server" title="All" Checked='<%# HasRecord(Convert.ToInt32(Eval("art_id")),0) %>' />
<asp:CheckBox ID="CheckBox1" runat="server" title="All" Checked='<%# HasRecord(Convert.ToInt32(Eval("art_id")),1) %>' />
.
.
<asp:CheckBox ID="CheckBox5" runat="server" title="All" Checked='<%# HasRecord(Convert.ToInt32(Eval("art_id")),5) %>' />

<asp:Button ID="Button1" runat="server" Text="View" CommandName="view" CommandArgument='<%# Eval("art_id")%>'

[.aspx.cs]
   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "view") {
 foreach (GridViewRow row in GridView1.Rows)
                {
                    artid = e.CommandArgument.ToString();
                    CheckBox cb0 = (CheckBox)row.FindControl("checkBox0");
                    CheckBox cb1 = (CheckBox)row.FindControl("checkBox1");
                    CheckBox cb2 = (CheckBox)row.FindControl("checkBox2");
                    CheckBox cb3 = (CheckBox)row.FindControl("checkBox3");
                    CheckBox cb4 = (CheckBox)row.FindControl("checkBox4");
                    CheckBox cb5 = (CheckBox)row.FindControl("checkBox5");  
                 if (cb0.Checked)
                    {
                        displayID = "0";
                    }
                    if (cb1.Checked)
                    {
                        displayID = "1";
                    }              
..........
            }
}
0
Comment
Question by:saloj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37737186
0
 
LVL 16

Accepted Solution

by:
Swapnil Piparia earned 500 total points
ID: 37738255
Hi Saloj,

Please find below modified code which gives you specific checkbox value on which row you have clicked.
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "view")
        {
            int index = Convert.ToInt32(e.CommandArgument); 
            GridViewRow row = GridView1.Rows[index];
                     artid = e.CommandArgument.ToString();
                    CheckBox cb0 = (CheckBox)row.FindControl("checkBox0");
                    CheckBox cb1 = (CheckBox)row.FindControl("checkBox1");
                    CheckBox cb2 = (CheckBox)row.FindControl("checkBox2");
                    CheckBox cb3 = (CheckBox)row.FindControl("checkBox3");
                    CheckBox cb4 = (CheckBox)row.FindControl("checkBox4");
                    CheckBox cb5 = (CheckBox)row.FindControl("checkBox5");  
                 if (cb0.Checked)
                    {
                        displayID = "0";
                    }
                    if (cb1.Checked)
                    {
                        displayID = "1";
                    }              
..........
            }
}

Open in new window


Thanks,
Netswap.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question