|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
<asp:Repeater>, checkbox values and ASP.NET 2.0machine 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 =- Check and see if you are re-binding when IsPostBack = True. You may not
want to. 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 =- Show quoteHide quote On Nov 16, 7:07 pm, "MikeS" <michael.spen***@gmail.com> wrote: > Check and see if you are re-binding when IsPostBack = True. You may not > want to. 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! 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 =- Show quoteHide quote On Nov 17, 12:57 pm, "MikeS" <michael.spen***@gmail.com> 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! 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.ConnectionStrings["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> 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 =- Show quoteHide quote On Nov 17, 3:13 pm, "MikeS" <michael.spen***@gmail.com> 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.ConnectionStrings["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> 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. 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 =- Show quoteHide quote On Nov 20, 7:07 pm, "MikeS" <michael.spen***@gmail.com> 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.
Login control - image button with text over it
how to get selectedvalue of radiobuttonlist in Javascript? List Box : help preventing duplicate items GridView EnableSortingAndPagingCallBacks Select command. Searching for a powerfull calendar addon User Control used in Repeater, but no output Accessing USER CONTROL which is inside Masterpage through Another USER Control inside normal page. Use CSS in themes Session sharing between ASP and ASP.NET StyleSheetTheme not working |
|||||||||||||||||||||||