Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > <asp:Repeater>, checkbox values and ASP.NET 2.0

Reply
Thread Tools

<asp:Repeater>, checkbox values and ASP.NET 2.0

 
 
deathtospam@gmail.com
Guest
Posts: n/a
 
      11-15-2006
A few weeks ago, I created a Classic ASP page that connects to a
machine with SQL Server installed on it, prompts the user to select a
database on that server, then lists all of user-created stored
procedures in that database. The user can select any of those stored
procedures by clicking on checkboxes; after submitting their
selections, a script to delete and then re-create each stored procedure
will be generated. Now I'm trying to do the same thing using ASP.NET.

I wanted to render the checkboxes inside an HTML table column, so I
avoided using the <asp:CheckBoxList> control. Instead, I opted to use
an <asp:Repeater> control with an HTML <input type="checkbox"
runat="server"> tag inside the Repeater's <ItemTemplate> server-side
element. Here's an snippet of my ASPX code:

================================================== ====
<asp:Repeater runat="server" ID="StoredProcedureCheckboxList"
Visible="false">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="3" width="100%">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:30px;"><input type="checkbox"
name="SelectedStoredProcedureIds" value="<%#
DataBinder.Eval(Container.DataItem, "StoredProcedureID") %>"
title="[<%# DataBinder.Eval(Container.DataItem, "StoredProcedureID")
%>] <%# DataBinder.Eval(Container.DataItem, "StoredProcedureName")
%>"></td>
<td><%# DataBinder.Eval(Container.DataItem,
"StoredProcedureName") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table><br>
</FooterTemplate>
</asp:Repeater>
================================================== ====


The above ASPX code works, and I had no problem retrieving the selected
checkbox string values (via the Request.Form collection) after
postback. However, I also wanted all of the checkboxes the user
selected to remain selected after postback. And that's when I realized
that I didn't know how to get a reference (via C# code-behind page) to
the collection of checkboxes inside the <asp:Repeater> control.

I know I could probably use the page's ViewState to do this, but I
wanted to start simple. I've also posted a barebones version of my
project ( http://tinyurl.com/yztohf ), to help illustrate my problem:
the project is comprised of three small files (ASPX, CS & Web.config)
authored in Visual Studio .NET 2005. All you need to do is modify
Web.config so that the [MyDatabase] connection string points to your
database, and it should work just fine. Any advice or tips would be
greatly appreciated... thanks in advance!


-= Tek Boy =-

 
Reply With Quote
 
 
 
 
MikeS
Guest
Posts: n/a
 
      11-17-2006
Check and see if you are re-binding when IsPostBack = True. You may not
want to.

 
Reply With Quote
 
 
 
 
deathtospam@gmail.com
Guest
Posts: n/a
 
      11-17-2006
MikeS --

First, I would like to note that I have disabled ViewState on my web
form (<%@ Page EnableViewState="false"). Second, I do currently
populate the checkboxes every time, regardless of what the value of
Page.IsPostBack is. If I don't rebind the checkboxes after the form
postback, the checkboxes don't show up.

My code is still up at http://tinyurl.com/yztohf , if you would like to
see my code. I appreciate you responding to my post -- I've read
through scores of blogs, forum posts, Usenet posts and website FAQs,
and have yet to find a solution to this problem. It's been an
extremely frustrating experience, to say the least.


-= Tek Boy =-


On Nov 16, 7:07 pm, "MikeS" <(E-Mail Removed)> wrote:
> Check and see if you are re-binding when IsPostBack = True. You may not
> want to.


 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      11-17-2006
Sorry to say...

Short of re-writing that code from scratch, I wouldn't know where to
start.
I think the issues is that all of the code is in Page_Load instead of
using some well defined sub routines and there is a mix of ASP.NET and
html controls which is negating the benefits of using ASP.NET and
causing unnecessary complexity. If I get around to it, I will re-write
it and paste it here.

BTW, C# is broken in the IDE as far as I am concerned. What, no
template editor!

 
Reply With Quote
 
deathtospam@gmail.com
Guest
Posts: n/a
 
      11-17-2006
MikeS --

As long as 1) I don't have to rely on ViewState and 2) I'm able to
retain control over how my checkboxes are formatted (i.e. inside an
HTML table) from within the ASPX page, I have no problem at all
starting from scratch. And if you can provide some code samples of how
to address this problem, I would really appreciate it!

Not sure what you mean about C# being broke in the IDE (you mean Visual
Studio 2005?). But coming from Classic ASP, where all "robust" IDEs
were essentially glorified versions of Notepad, I'm not sure I'd notice
it being absent. =)


-= Tek Boy =-



On Nov 17, 12:57 pm, "MikeS" <(E-Mail Removed)> wrote:
> Sorry to say...
>
> Short of re-writing that code from scratch, I wouldn't know where to
> start.
> I think the issues is that all of the code is in Page_Load instead of
> using some well defined sub routines and there is a mix of ASP.NET and
> html controls which is negating the benefits of using ASP.NET and
> causing unnecessary complexity. If I get around to it, I will re-write
> it and paste it here.
>
> BTW, C# is broken in the IDE as far as I am concerned. What, no
> template editor!


 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      11-17-2006
This doesn't do all you were doing but it does show how to keep the
checked items in an arraylist and feed that back to the bound items.


<%@ Page Language="C#" EnableViewState="false" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private ArrayList _checked = new ArrayList();
protected void Page_Load(object sender, EventArgs e) {
say("");
if (IsPostBack) ProcessForm();
BindForm();
}
void ProcessForm() {
_checked.Clear();
String[] s = Request.Form.GetValues("cbProc");
if (s != null)
_checked.AddRange(s);
foreach (string v in _checked)
Response.Write(v + "<br>");
}
void BindForm() {
try
{
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrin gs["LocalSqlServer"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT DISTINCT TOP 10
syscomments.[id] AS [StoredProcedureID], OBJECT_NAME(syscomments.id) AS
[StoredProcedureName] FROM syscomments INNER JOIN sysobjects ON
syscomments.[id] = sysobjects.[id] WHERE
(OBJECTPROPERTY(syscomments.[id], 'IsProcedure') = 1) AND
(sysobjects.category <> 2) ORDER BY OBJECT_NAME(syscomments.id) ASC");
conn.Open();
cmd.Connection = conn;
Repeater1.DataSource = cmd.ExecuteReader();
Repeater1.DataBind();
conn.Close();
}
catch (Exception e) {
say(e.Message);
}
}
bool isChecked(string s) {
return _checked.Contains(s);
}
void say(String s) {
lblMsg.Text = s;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td colspan="2" align="center">
Stored Procedure List
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<input type="checkbox" name="cbProc"
value="<%# Eval("StoredProcedureID") %>"
<%#
isChecked(Eval("StoredProcedureID").ToString()) ? "checked" : "" %> >
</td>
<td>
<span>
<%# Eval("StoredProcedureName") %>
</span>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan="2" align="center">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="OK" />
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Label runat="server" ID="lblMsg"></asp:Label>
</div>
</form>
</body>
</html>

 
Reply With Quote
 
deathtospam@gmail.com
Guest
Posts: n/a
 
      11-20-2006
MikeS --

OUTSTANDING! You just made my day, and on a Monday morning to boot. I
notice that you didn't actually use C# to iterate through the
collection to re-check the checkboxes. Is there truly no way to access
the collection of checkboxes via C# ? The fact that it's no easier to
do this with C# & ASP.NET 2.0 than it was with VBScript & Classic ASP
doesn't impress me at all. But at least I can start moving forward
again...

I really appreciate you taking the time to write the code for this,
Mike. Really, really, Really, REALLY, (ad infinitum)...


-= Tek Boy =-



On Nov 17, 3:13 pm, "MikeS" <(E-Mail Removed)> wrote:
> This doesn't do all you were doing but it does show how to keep the
> checked items in an arraylist and feed that back to the bound items.
>
> <%@ Page Language="C#" EnableViewState="false" %>
> <%@ Import Namespace="System.Data.SqlClient" %>
> <%@ Import Namespace="System.Configuration" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <script runat="server">
> private ArrayList _checked = new ArrayList();
> protected void Page_Load(object sender, EventArgs e) {
> say("");
> if (IsPostBack) ProcessForm();
> BindForm();
> }
> void ProcessForm() {
> _checked.Clear();
> String[] s = Request.Form.GetValues("cbProc");
> if (s != null)
> _checked.AddRange(s);
> foreach (string v in _checked)
> Response.Write(v + "<br>");
> }
> void BindForm() {
> try
> {
> SqlConnection conn = new
> SqlConnection(ConfigurationManager.ConnectionStrin gs["LocalSqlServer"].ConnectionString);
> SqlCommand cmd = new SqlCommand("SELECT DISTINCT TOP 10
> syscomments.[id] AS [StoredProcedureID], OBJECT_NAME(syscomments.id) AS
> [StoredProcedureName] FROM syscomments INNER JOIN sysobjects ON
> syscomments.[id] = sysobjects.[id] WHERE
> (OBJECTPROPERTY(syscomments.[id], 'IsProcedure') = 1) AND
> (sysobjects.category <> 2) ORDER BY OBJECT_NAME(syscomments.id) ASC");
> conn.Open();
> cmd.Connection = conn;
> Repeater1.DataSource = cmd.ExecuteReader();
> Repeater1.DataBind();
> conn.Close();
> }
> catch (Exception e) {
> say(e.Message);
> }
> }
> bool isChecked(string s) {
> return _checked.Contains(s);
> }
> void say(String s) {
> lblMsg.Text = s;
> }
> </script>
>
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:Repeater ID="Repeater1" runat="server">
> <HeaderTemplate>
> <table>
> <tr>
> <td colspan="2" align="center">
> Stored Procedure List
> </td>
> </tr>
> </HeaderTemplate>
> <ItemTemplate>
> <tr>
> <td>
> <input type="checkbox" name="cbProc"
> value="<%# Eval("StoredProcedureID") %>"
> <%#
> isChecked(Eval("StoredProcedureID").ToString()) ? "checked" : "" %> >
> </td>
> <td>
> <span>
> <%# Eval("StoredProcedureName") %>
> </span>
> </td>
> </tr>
> </ItemTemplate>
> <FooterTemplate>
> <tr>
> <td colspan="2" align="center">
> </td>
> </tr>
> <tr>
> <td colspan="2" align="center">
> <input type="submit" value="OK" />
> </td>
> </tr>
> </table>
> </FooterTemplate>
> </asp:Repeater>
> <br />
> <asp:Label runat="server" ID="lblMsg"></asp:Label>
> </div>
> </form>
> </body>
> </html>


 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      11-21-2006
This is C#...

bool isChecked(string s) {
return _checked.Contains(s);
}

Though I am not iterating, ASP.NET is, so I am using the data binding
expression.
You might be able to iterate all of the check boxes in the grid after
it is built in its DataBound event.

 
Reply With Quote
 
deathtospam@gmail.com
Guest
Posts: n/a
 
      11-21-2006
Thank you for this follow-up info -- I hadn't seen the Contains()
method before. There's no way to perform this check from inside the
<%# %> tags in the ASPX page, is there? It doesn't seem like it, but
I figured I'd ask...


-= Tek Boy =-


On Nov 20, 7:07 pm, "MikeS" <(E-Mail Removed)> wrote:
> This is C#...
>
> bool isChecked(string s) {
> return _checked.Contains(s);
> }
>
> Though I am not iterating, ASP.NET is, so I am using the data binding
> expression.
> You might be able to iterate all of the check boxes in the grid after
> it is built in its DataBound event.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPNET CheckBox and a CheckBox in a TemplateField Jason Huang ASP .Net 0 06-29-2007 12:35 AM
DataGrid and embeded Checkbox..How to find if checkbox clicked =?Utf-8?B?RG90TmV0RGV2?= ASP .Net 1 10-06-2006 04:11 PM
disable checkbox list checkbox Vikram ASP .Net 1 01-25-2006 02:59 PM
Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked. Joey Pang ASP .Net Datagrid Control 4 06-13-2005 02:29 AM
Text on Checkbox below the checkbox tshad ASP .Net 0 04-14-2005 11:26 PM



Advertisments