Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 27th, 2008, 11:13 AM
Authorized User
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Gridview and Checkbox

Hi Friends;

i want to select only one cell content of the row in the GridView regarding to the

CheckBox ;

(if Checkbox control is checked then select and add that cell text to the

StringBuilder )

but i couldnt do this (nothing returns to me)
help about this subject..

i've used this code

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        StringBuilder str = new StringBuilder();

        for (int i = 0; i < gv.Rows.Count; i++)
        {
            GridViewRow row = gv.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("chkSec")).Checked;

            if (isChecked)
            {
                // Column 2 is the name column
                str.Append(gv.Rows[i].Cells[6].Text);
            }
        }
        Response.Write(str.ToString());
    }

 
Old February 27th, 2008, 11:58 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

When you say nothing returns to you: do you mean that isChecked always evaluates to false or that you do not get anything returned via this statement:

gv.Rows[i].Cells[6].Text

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 27th, 2008, 01:12 PM
Authorized User
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, i mean that i do not get anything returned via this statement:


 
Old February 27th, 2008, 01:25 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Show me how you define your gridview.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 27th, 2008, 01:45 PM
Authorized User
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My GridView is here:


<asp:GridView ID="gv" runat="server" Style="z-index: 101; left: 263px; position: absolute;
            top: 15px" AllowPaging="True" HorizontalAlign="Left" SelectedIndex="0" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
            >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lblEdit" runat="server">Düzenle</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SID">
                    <EditItemTemplate>
                        <asp:Label ID="lblID" runat="server"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"SID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SESSION">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtSession" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"SESSION") %>' Width="141px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSession" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"SESSION") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CLASS">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCLASS" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CLASS") %>' Width="141px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCLASS" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CLASS") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UNIT">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtUNIT" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UNIT") %>' Width="146px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblUNIT" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UNIT") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Question">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtQuestion" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CONTENT") %>' Width="193px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblQuestion" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CONTENT") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SELECTED">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSec" runat="server"/>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

 
Old February 27th, 2008, 02:08 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

There is no text in cell six all you have is a Checkbox:

<asp:TemplateField HeaderText="SELECTED">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSec" runat="server"/>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:TemplateField>
            </Columns>

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 27th, 2008, 02:28 PM
Authorized User
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What did you mean , there must be a text in it

 
Old February 27th, 2008, 02:41 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No, there doesn't have to be. Cell six is the cell that contains your checkbox and you do NOT bind any data to that cell based on the code you have provided. As your code stands right now, you are trying to get text from the "SELECTED" column and, as I have said, there is no text there only a checkbox. Are you maybe trying to get data from a different column and typed '6' by mistake?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 27th, 2008, 03:59 PM
Authorized User
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it looks like that i've written wrong index but after all i tried another indexes ; for example , {0,1,2,3,4,5} but nothing happened

 
Old February 27th, 2008, 04:13 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by krasnokojiy
 it looks like that i've written wrong index but after all i tried another indexes ; for example , {0,1,2,3,4,5} but nothing happened

Thats because you are binding to controls. Normally when you want to get the Text value of a cell, you simply call your databind method in the cell outside of a control like this:

                <asp:TemplateField HeaderText="SID">
                    <EditItemTemplate>
                        <asp:Label ID="lblID" runat="server"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem,"SID") %> </ItemTemplate>
                </asp:TemplateField>

To do what you want without changing your code you can do something like:
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        StringBuilder str = new StringBuilder();

        for (int i = 0; i < gv.Rows.Count; i++)
        {
            GridViewRow row = gv.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("chkSec")).Checked;

            if (isChecked)
            {
                Label lbl = (Label)row.FindControl("lblID");
                // Column 2 is the name column
                str.Append(lbl.Text);
            }
        }
        Response.Write(str.ToString());
    }
hth.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Confirmation with checkbox in gridview collie Visual Studio 2005 0 October 24th, 2007 06:13 AM
Checkbox in Gridview yukijocelyn ASP.NET 2.0 Basics 0 September 24th, 2007 12:32 AM
GridView Checkbox ItemTemplate angelicagm ASP.NET 2.0 Professional 3 May 31st, 2007 04:44 AM
Checkbox in GridView aliirfan84 ASP.NET 2.0 Professional 1 May 15th, 2007 03:59 PM
Gridview checkbox problems pauliehaha ASP.NET 2.0 Professional 1 May 3rd, 2007 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.