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
 Gridview and Checkbox

Author  Topic 

krasnokojiy
Starting Member

18 Posts

Posted - 2008-02-27 : 09:55:00
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());
}

MC

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-27 : 10:37:54
// Column 2 is the name column
str.Append(gv.Rows[i].Cells[6].Text);

You have a contradiction right there ....


As always, simply debug and step through this code line-by-line to ensure that things are happening as you expect, and variables are set as you expect and so on.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

krasnokojiy
Starting Member

18 Posts

Posted - 2008-02-27 : 12:37:22
i understood but moreover i cannot take a result ; it makes no matter which column is writed (Cells[6] or Column 2)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-27 : 12:51:09
That tells me nothing. What do you mean "you cannot take a result"? You get an error? No value? wrong value? are you sure you are looking in the right column, in the right control, and that it is definitely there in the page? Without more info, I am not sure how I can help.

There are lots of things to test by simply stepping through the code: is "IsChecked" ever returning true? is the checkbox control found? Is this loop even executed? Is the event ever raised? Are there view state issues caused by the post back? What cells have what Text properties, what controls in them? And so on. Isolate the point where things don't work as you expect and then focus on that.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

krasnokojiy
Starting Member

18 Posts

Posted - 2008-02-27 : 13:00:22
There is no error but at the same time no value. my gridview schema 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">Edit</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>


MC
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-27 : 14:02:13
If you have a label in your Cell, you need to find the label control and use the label's text property. The Cell's text property is only for literal HTML within the cell, it does not contain the text property of all server-side controls within the cell.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

krasnokojiy
Starting Member

18 Posts

Posted - 2008-02-27 : 14:52:13
What do you mean ? What's my fault to your opinion? What do i need to do to solve the problem?
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-27 : 15:49:55
Again, it appears that you have LABEL controls in your cells that contain the text you are looking for. You need to use FindControl() to find the specific LABEL that contains the TEXT property you are looking for.

Do you understand the difference between

<ItemTemplate>
<%# =SomeText %>
</ItemTemplate>

and

<ItemTemplate>
<asp:Label runat="server" id="lbl" Text="<%# =SomeText %>" />
</ItemTemplate>


? If you don't, then you really should get a good book on ASP.NET and read up on it, it is a fundamental concept that you should understand completely if you are writing ASP.NET code.

The first binds the string to the Text property of the Cell, the second does NOT: It creates a Label control within the cell, and the label's Text property contains the string.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

krasnokojiy
Starting Member

18 Posts

Posted - 2008-02-28 : 07:47:30
thanks a lot, its clear for me
Go to Top of Page
   

- Advertisement -