Checkbox postback in DataGrid

Discussion in 'ASP .Net' started by TPS, Aug 9, 2004.

  1. TPS

    TPS Guest

    I have a checkbox column in my datagrid. This checkbox triggers an
    autopostback.

    When I postback, I need to tell what row the user is on so I can tell what
    check box was checked or unchecked.

    Can't seem to find that property.

    Thanks.

    TPS.
     
    TPS, Aug 9, 2004
    #1
    1. Advertising

  2. TPS

    Scott Allen Guest

    Hi TPS:

    In the event handler, the sender parameter will represent the checkbox
    that initiated the event. By inspecting the Parent property you can
    eventually get to the DataGridItem that represents the row you are in.
    DataGridItem has an ItemIndex property if you need to index into a
    DataSource of some sort.

    My article might shed some light on the hierarchy:
    http://odetocode.com/Articles/116.aspx

    --
    Scott
    http://www.OdeToCode.com

    On Mon, 9 Aug 2004 09:53:49 -0500, "TPS" <> wrote:

    >I have a checkbox column in my datagrid. This checkbox triggers an
    >autopostback.
    >
    >When I postback, I need to tell what row the user is on so I can tell what
    >check box was checked or unchecked.
    >
    >Can't seem to find that property.
    >
    >Thanks.
    >
    >TPS.
    >
     
    Scott Allen, Aug 9, 2004
    #2
    1. Advertising

  3. TPS

    TPS Guest

    Hey Scott,

    Thanks for your reply.

    I will inspect the sender. And check out your article.

    Thanks.

    TPS.
     
    TPS, Aug 9, 2004
    #3
  4. Hi TPS,

    Yes, as Scott has mentioned, you can get the reference to the checkbox in
    its postback event and use the Parent property to upper referece its parent
    and parent control which is DataGridItem. Here is a simple test page, you
    can have a look:

    ===============aspx================
    <HTML>
    <HEAD>
    <title>CheckGrid</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table width="100%" align="center">
    <tr>
    <td>
    <asp:DataGrid id="dgCheck" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="index"
    HeaderText="Index"></asp:BoundColumn>
    <asp:BoundColumn DataField="name"
    HeaderText="Name"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Selected">
    <ItemTemplate>
    <asp:CheckBox ID="chkSelected" Runat="server" AutoPostBack="True"
    OnCheckedChanged="chkSelected_CheckedChanged" Checked='<%#
    ((System.Data.DataRowView)Container.DataItem)["selected"] %>' >
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </td>
    </tr>
    <tr>
    <td></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
    ==============code behind========
    public class CheckGrid : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid dgCheck;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    Bind_Data();
    }
    }

    protected void Bind_Data()
    {
    DataTable tb = new DataTable();
    tb.Columns.Add("index",typeof(int));
    tb.Columns.Add("name",typeof(string));
    tb.Columns.Add("selected",typeof(bool));

    DataRow row = null;
    for(int i=1;i<=15;i++)
    {
    row = tb.NewRow();
    row["index"] = i;
    row["name"] = "Name_" + i;

    if(i%3==0)
    {
    row["selected"] = true;
    }
    else
    {
    row["selected"] = false;
    }

    tb.Rows.Add(row);

    }

    dgCheck.DataSource = tb;
    dgCheck.DataBind();


    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    protected void chkSelected_CheckedChanged(object sender, System.EventArgs
    e)
    {
    CheckBox chk = (CheckBox)sender;

    DataGridItem dgi = (DataGridItem)chk.Parent.Parent;
    Response.Write("<br>" + dgi.ItemIndex);

    }
    }
    =============================

    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Aug 10, 2004
    #4
  5. TPS

    TPS Guest

    Hey Scott, thanks for that tip. It helped.

    TPS.

    --
     
    TPS, Aug 11, 2004
    #5
  6. TPS

    TPS Guest

    Stephen,

    Thank you for your comments and your CHECKBOX example was very helpful.

    TPS
     
    TPS, Aug 17, 2004
    #6
  7. TPS

    eliza81

    Joined:
    Feb 10, 2010
    Messages:
    12
    Checkbox Column In Datagrid

    When we add a customized checkbox column to a datagrid in .net (windows application) , the default property allows to check or uncheck the column using a double click. On the first click it selects the column and on the second click the column is either checked or unchecked.

    To change this default property, we need to handle the click event on grid and modify the selected cell value. Here is the sample code to achieve this.

    [C#.NET VS 2003 , Code to add checkbox column to grid using table style property]

    dgItemDetails.TableStyles.Clear(); // clears the tablestyle (dgItemDetails is the name of grid)
    DataGridTableStyle dgt = new DataGridTableStyle();
    dgt.MappingName = "ItemDetails";

    DataGridBoolColumn dgbCol = new DataGridBoolColumn(); // creates the checkbox column
    dgbCol.MappingName = "Select";
    dgbCol.HeaderText = "";
    dgbCol.Width = 40;
    dgbCol.AllowNull = false;
    dgbCol.Alignment = HorizontalAlignment.Left;
    dgt.GridColumnStyles.Add(dgbCol);

    dgItemDetails.TableStyles.Add(dgt); // add
     
    eliza81, Feb 19, 2010
    #7
  8. TPS

    Leah

    Joined:
    Jun 2, 2011
    Messages:
    2
    Trying to find a Checkbox within a Datagrid ....

    I have read through this post and it seems very helpful, the problem is that I am still doing something wrong because it is not working.

    I have a template with a checkbox within a datagrid, so each row of data has 1 checkbox. I need to find the checkbox that is checked/unchecked on the postback.

    I have this in my .aspx page:
    <asp:TemplateColumn HeaderText="Archived" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:CheckBox ID="chkArchived" runat="server" CommandName="Archive"
    CommandArgument='<%# Eval("QuestionId") %>' AutoPostBack="true" OnCheckedChanged="chkArchived_CheckedChanged" />
    </ItemTemplate>
    </asp:TemplateColumn>

    Which I believe is working alright, but the VB page is not working for me. I created a CheckedChanged routine as in the example:
    Protected Sub chkArchived_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'This is where I am not able to find what row I am on to save the changes to the db.
    End Sub

    The article posted states about finding the control through the datagrid selection change, but I only got compile errors when I tried to rewrite the code into VB.

    Any ideas?
     
    Leah, Jun 2, 2011
    #8
  9. TPS

    Leah

    Joined:
    Jun 2, 2011
    Messages:
    2
    This is what worked for me ....

    This is the VB code that ended up making the checkboxes in my datagrid saveable on a postback, one at a time.

    Protected Sub chkArchived_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim c As Control = CType(sender, Control)
    Dim item As DataGridItem = CType(c.Parent.Parent, DataGridItem)
    Dim id As Integer = CType(dgQuestions.DataKeys.Item(item.ItemIndex), Integer)
    Dim question As BOBJ.SurveyQuestionObj = CType(BLL.SessionData.SurveyQuestions.ItemById(id), BOBJ.SurveyQuestionObj)

    question.Archived = True
    BLL.Processes.Administrative.UpdateSurveyQuestion(question)
    End Sub
     
    Leah, Jun 2, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    864
    David Wier
    Oct 6, 2006
  2. DotNetDev
    Replies:
    1
    Views:
    487
    Angel
    Dec 13, 2007
  3. Carmen

    CheckBox in a datagrid without postback

    Carmen, Aug 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    146
    Carmen
    Aug 26, 2004
  4. Norman Katz

    web Datagrid checkbox column postback problem

    Norman Katz, Oct 15, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    469
    Norman Katz
    Oct 15, 2004
  5. Joey Pang
    Replies:
    4
    Views:
    563
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page